how we get 1. Create a record in Table2 whenever a record is inserted into Table1. 2. Update Short Description of a particular record of Table2 whenever a record is updated in Table1.

sachin dhek
Kilo Contributor

i am a fresher i am stuck in this problem last 3 days 

1 ACCEPTED SOLUTION

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

 

Please appreciate the efforts of community contributors by marking appropriate response as correct answer and helpful, this may help other community users to follow correct solution in future.
Thanks
Anil Lande

View solution in original post

20 REPLIES 20

Anil Lande
Kilo Patron

Hi,

There are two ways to achieve this.

1. Using Business rule

2. Using flow designer.

 

You can try using After business rule and use script with GlideRecord query to update another record.

 

Note: As being fresher I would recommend not to request script, first give a try by yourself and share your script. We would be more happy to correct your script.

 

Happy Learning 🙂

Anil

Please appreciate the efforts of community contributors by marking appropriate response as correct answer and helpful, this may help other community users to follow correct solution in future.
Thanks
Anil Lande

(function executeRule(current, previous /*null when async*/) {

// Add your code here
var gr = new GlideRecord('u_table_1');
gr.addQuery();
gr.query();
gr.insert();
while(gr.next()){
gs.log(gr.u_table_2);
}

})(current, previous);

 

is this right or not?

 

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

 

Please appreciate the efforts of community contributors by marking appropriate response as correct answer and helpful, this may help other community users to follow correct solution in future.
Thanks
Anil Lande

You can find some examples on below links:

https://docs.servicenow.com/bundle/rome-application-development/page/script/server-scripting/concept...

https://servicenowguru.com/scripting/gliderecord-query-cheat-sheet/

https://www.servicenowelite.com/blog/2019/9/29/gliderecord-scripting

 

Thanks,
Anil Lande

 

Please appreciate the efforts of community contributors by marking appropriate response as correct answer and helpful, this may help other community users to follow correct solution in future.
Thanks
Anil Lande