The Zurich release has arrived! Interested in new features and functionalities? Click here for more

set value of a reference field through a fix script

Prakash Pareek
Tera Expert

1. I wanted to set value for reference field 'abc' through a fix script.

2. I have a reference field 'xyz' in my table that is inherited from another table .. So wanted to set value for that reference field also through the fix script.

 

var requestedGR = new GlideRecord('sc_t');
var queryString = "sys_id=2f6df0ffde6fd00000ca8ebd576543e";
requestedGR.addEncodedQuery(queryString);
requestedGR.query();
while (requestedGR.next()) {
requestedGR.setValue('u_abc','rty');
requestedGR.setValue('u_xyz','lmno');
requestedGR.updateWithReferences();
}

 

6 REPLIES 6

Voona Rohila
Kilo Patron
Kilo Patron

Hi Prakash

Below is a sample logic.

var gr = new GlideRecord('table_name');
gr.addEncodedQuery(''); //add your encoded query here
gr.query();
while(gr.next())
{
gr.abc = 'give value here'; //modify field name
gr.update();
}

 


Mark it helpful if this helps you to understand. Accept solution if this give you the answer you're looking for
Kind Regards,
Rohila V
2022-25 ServiceNow Community MVP

in gr.abc i need to set value for a reference field one is reference to the user table ... and the other is reference to business unit and the business unit is actually inherited from another table .. without actually passing the hard coded sys id .. how can i update the fields through fix script

Hi Prakash

Can you provide table details and from which field you need the hardcoded value so that it will be easy to give sample code.


Mark it helpful if this helps you to understand. Accept solution if this give you the answer you're looking for
Kind Regards,
Rohila V
2022-25 ServiceNow Community MVP

Muhammad Khan
Mega Sage
Mega Sage

You might have to dot-walk to actual field you want to update in referenced records. Something like below should work.

var requestedGR = new GlideRecord('sc_t');
if (requestedGR.get('2f6df0ffde6fd00000ca8ebd576543e')) {
    requestedGR.u_abc.<field_name> =  'rty';
    requestedGR.u_xyz.<field_name> =  'lmno';
    requestedGR.updateWithReferences();
}

 

Refer to below link for a working example of updateWithReference API.

updateWithReferences(Object reason)