set value of a reference field through a fix script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-15-2022 10:10 PM - edited 11-15-2022 10:21 PM
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();
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-15-2022 10:21 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-15-2022 10:27 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-16-2022 12:04 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-15-2022 10:59 PM
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.