How to Populate reference fields from one table to another table via businessrule?

HARI KISHAN GVS
Mega Sage

Hi Team,

I have two tables, one is 'sn_customerservice_setup' another one is 'sn_customerservice_codes'.

In setup table i have a filed called u_codes which refers to sn_customerservice_codes table.

and in codes table i have a field called u_setup which refers to sn_customerservice_setup table.

My requirement is when setup table is updated (u_codes field is filled and saved/updated), i need to autopopulate the u_setup field in sn_customerservice_codes table.

 

i wrote a after insert Business rule on sn_customerservice_setup table with no luck.

 

Please correct it.

 

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

var gr = new GlideRecord('sn_customerservice_codes');
gr.addQuery('sys_id', current.u_codes);
gr.query();
if(gr.next()){
gr.u_setup == current.sys_id;
gr.update();
}

})(current, previous);

 

 

Thanks in advance,

Hari Kishan.

1 ACCEPTED SOLUTION

AnubhavRitolia
Mega Sage
Mega Sage

Hi @HARI KISHAN GVS 

 

Please find updated code below:

 

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

var gr = new GlideRecord('sn_customerservice_codes');
gr.addQuery('sys_id', current.u_codes);
gr.query();
if(gr.next()){
gr.u_setup = current.getUniqueValue();  //current.sys_id id this does not works
gr.update();
}

})(current, previous);

 

 

Please mark this as correct answer and helpful if it resolved, or mark this helpful if this help you to reach towards solution.

Thanks
Anubhav Ritolia
ServiceNow Rising Star 2023

View solution in original post

2 REPLIES 2

AnubhavRitolia
Mega Sage
Mega Sage

Hi @HARI KISHAN GVS 

 

Please find updated code below:

 

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

var gr = new GlideRecord('sn_customerservice_codes');
gr.addQuery('sys_id', current.u_codes);
gr.query();
if(gr.next()){
gr.u_setup = current.getUniqueValue();  //current.sys_id id this does not works
gr.update();
}

})(current, previous);

 

 

Please mark this as correct answer and helpful if it resolved, or mark this helpful if this help you to reach towards solution.

Thanks
Anubhav Ritolia
ServiceNow Rising Star 2023

SanjivMeher
Kilo Patron
Kilo Patron

Your Business rule should be an after insert/update business rule with condition, when setup field changes with below script

 

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

var gr = new GlideRecord('sn_customerservice_codes');
gr.addQuery('sys_id', current.getValue('u_codes'));
gr.query();
if(gr.next()){
gr.u_setup = current.getValue('sys_id');
gr.update();
}

})(current, previous);

Please mark this response as correct or helpful if it assisted you with your question.