How to add a field value from one table into a filed on another table?

Alex D Great
Tera Contributor

Hi

I want to add the number field of a record (if it exists) into a field in another table. Is it best to run a business rule, or to specify this in the dictionary entry for the reference field? Then next questions is, how do I do this?

All help is highly appreciated šŸ™‚

1 ACCEPTED SOLUTION

AnubhavRitolia
Mega Sage
Mega Sage

Hi @Alex D Great 

 

Best way is to use Business Rule only if you want to achieve it when any Changes or Update happened in your record.

 

And if you want to achieve it for existing record, you should go with Fix Script.

 

So based on your requirement you can implement by GlideRecord() another table and update the field.

 

NOTE: For Reference field, sys_id is stored in the backend.

 

 

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

11 REPLIES 11

AnubhavRitolia
Mega Sage
Mega Sage

Hi @Alex D Great 

 

Best way is to use Business Rule only if you want to achieve it when any Changes or Update happened in your record.

 

And if you want to achieve it for existing record, you should go with Fix Script.

 

So based on your requirement you can implement by GlideRecord() another table and update the field.

 

NOTE: For Reference field, sys_id is stored in the backend.

 

 

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

ok, thanks for your reply @AnubhavRitolia 

can't get the br to work with the current settings:

table: sn_customerservice_case
when: before insert and update
conditions: u_request is empty

script:

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

var caseGr= new GlideRecord('sn_customerservice_case');
    caseGr.addEncodedQuery("u_requestISEMPTY");
    caseGr.query();

    while(caseGr.next())
    {
    var req = new GlideRecord('sc_request');
    req.addQuery('parent', caseGr.sys_id); 
    req.query();
    }  

    if(req.next())
    {
    caseGr.u_request = req.sys_id;
    req.parent = caseGr.sys_id;
    caseGr.update();
    }

})(current, previous);

AnubhavRitolia
Mega Sage
Mega Sage

Hi @Alex D Great 

 

Please find the updated BR below:

 

table: sn_customerservice_case
when: After insert and update
conditions: u_request is empty

 

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

    var req = new GlideRecord('sc_request');
    req.addQuery('parent', current.sys_id);        //or try 'parent.sys_id' if 'parent' does not work.
    req.query();

    if(req.next())
    {
    current.u_request = req.sys_id;
    current.update();
    }
})(current, previous);

 

What I understand from your code is that When record in created on 'sn_customerservice_case' table and Request field is empty, you search for Request whose parent is that Customer Service Case. Than map that Request to your Custom Request field on Case table.

 

As BR is already on 'sn_customerservice_case' table, no need to query it again in script. Also as already you are checking for parent of Request so why to again update that field.

 

Please try above solution if my understanding was right.

 

 

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

Thank you very much @AnubhavRitolia 

Somehow this don't seem to work for me..I tried both parent and parent.sys_id in the add query. It looks very correct to me, but I suspect it is something in the "Customer service request integration" blocking me from doing this. But that is just a general assumption