Need to update reference field by using background script

Arun91
Tera Contributor

Hi Team,

I have a field called location in cmdb_ci table which is OOTB. I have created a custom table and create a field in cmdb_ci referencing to custom table. Based on the OOTB location field need to update custom reference field in servicenow. How we can achieve through background script in servicenow

5 REPLIES 5

Harish KM
Kilo Patron
Kilo Patron

Hi @Arun91 If i understand you want to update location value in your custom table based on cmdb location value?

Regards
Harish

Arun91
Tera Contributor

For Example cmdb_ci table location field is having value xyz. 

Custom table is xyz and field name is ddd. In cmdb_ci table location name is GGG then the custom table field should populate YYY.

 

How we can achieve this. 

Note: Two tables have different data we no need to copy the same data to another field.

shloke04
Kilo Patron

HI @Arun91 ,

 

Please use below background script to achieve the same:

var gr = new GlideRecord('cmdb_ci'); // Replace for which table of CMDB you want to do this for
gr.addEncodedQuery('locationISNOTEMPTY');
gr.query();
while(gr.next()){
gr.u_location = gr.location;  //This wll copy from OOTB location to your custom attribute
gr.setWorkflow(false);
gr.update();
}

 

Hope this helps. Please mark the answer as correct/helpful based on impact.

Regards,
Shloke

@Arun91 

Also, would like to mention that technically you can achieve your requirement using the script which I have shared above, but why are you maintaining a custom table for location and why can't OOTB location can be leveraged? Just curious?

 

This is going to add technical maintenance debt on your platform with every upgrade you do.

Hope this helps. Please mark the answer as correct/helpful based on impact.

Regards,
Shloke