- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-03-2022 09:00 PM

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-04-2022 02:08 AM
You need to add some query if you want to update existing record.
(function executeRule(current, previous /*null when async*/) {
// Add your code here
var gr = new GlideRecord('u_table_1');
gr.addQuery(); // eg. gr.addQuery('fieldName', 'value');
//gr.addQuery('active', true); // eg
gr.query();
//gr.insert(); // use this line if you want to create new record
while(gr.next()){
gs.log(gr.u_table_2); // here use gr.fieldName
gr.fieldName = 'value';
gr.update(); // to update the record matching your query.
}
})(current, previous);
Note: replace fieldName as per your column/field names in your tables.
Thanks,
Anil Lande
Thanks
Anil Lande
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-04-2022 03:05 AM
thank you sir......
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-07-2022 03:42 AM
Update Short Description of a particular record of Table2 whenever a record is updated in Table1.
this is my code is this right or not?
(function executeRule(current, previous /*null when async*/ ) {
// Add your code here
var rec = new GlideRecord('u_table2');
//rec.addQuery('u_string_3',current.sys_id);
rec.query();
while(rec.next())
{
rec.u_string_3 = current.u_table_1;
rec.update();
}
})(current, previous);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-04-2022 02:15 AM
Hi Sachin,
For your first scenario, you can create after insert BR on table1 and write a script to insert record in table 2
For your second scenario, you can create after update BR on table1 and write a script to update short_description in table2.
Let me know if you have any further queries.
Please mark this as Correct or helpful if it helps.
Thanks and Regards,
Abhijit
Regards,
Abhijit
ServiceNow MVP
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-04-2022 03:07 AM
sorry sir, but I can't understand what I do?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-04-2022 03:15 AM
Hi,
Before proceeding with your script, when you are creating both the tables you need to ensure that both the tables are related to each other.
So please follow the steps below to achieve your requirement:
1) Create Table 1 with fields you need like Short Description.
2) Create Table 2 with fields you need again of your choice like for example Short Description. Also in this table make sure to create another Reference field say name it as "Parent Linked" and refer it to Table 1 so that when a record is created in this Table 2 you can link it to Table 1.
3) This will ensure that both tables are linked to each other and when you are updating Short Description or any other field on table 1, you can fetch the respective record of table 2 which need to be updated with similar short description using a After Update Business Rule written on Parent table.
For example, I have tried replicating your scenario and have listed the steps below:
1) Table 1 Created as shown below with Short Description Field:
2) Similarly I have created another Table as table 2 with below two fields for now:
a) Short Description
b) Parent Linked referring to table 1 as shown below:
3) Now what I have done is created a Relationship so that .child Table in this case it is "Table2" gets shown as a Related list on table 1.
4) Navigate to "Relationship" module under System Definition as shown below:
5) Now configure the form as shown below and select your respective tables:
Script for Relationship:
(function refineQuery(current, parent) {
// Add your code here, such as current.addQuery(field, value);
parent.addQuery('u_parent_linked',current.sys_id);
})(current, parent);
Select the Table Names as shown above and now Navigate to your Table 1 Form and then Right click on the header of the form and select Configure--> Related List as shown below:
Now select the Relationship which we have created in above steps as shown below and click on Save button:
Output:
Table 2 Form screenshot with Table 1 linked as shown below:
Now in order to Update Short Description when ever Short Description gets updated in Table 1 and also create Record in Table 2 when ever a record is created in Table1 , you need to write a After Insert and Update Business Rule on Table 1 and use the script below:
Script:
(function executeRule(current, previous /*null when async*/) {
// Add your code here
if(current.operation() == 'insert'){
createRecordTable2(current);
}else if(current.operation() == 'update'){
updateRecordtable2(current);
}
function createRecordTable2(current){
var gr = new GlideRecord('u_table_2');
gr.initialize();
gr.TABLE2_FIELDNAME= current.TABLE1_FIELDNAME;
gr.u_parent_linked = current.sys_id; // Replace "u_parent_linked" with the field present on your Table 2 which is referring to Table 1
gr.insert();
}
function updateRecordtable2(current){
var gr1 = new GlideRecord('u_table_2');
gr1.addQuery('u_parent_linked',current.sys_id);// Replace "u_parent_linked" with the field present on your Table 2 which is referring to Table 1
gr1.query();
while(gr1.next()){
gr1.TABLE2_FIELDNAME= current.TABLE1_FIELDNAME;
gr1.update();
}
}
})(current, previous);
Let me know if you are facing an issue here.
Hope this helps. Please mark the answer as correct/helpful based on impact.
Regards,
Shloke
Regards,
Shloke