Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

How to update field on all RITMs using background script

Russell Abbott
Kilo Sage

I had a requirement to copy a field on a Variable Set, to a new field on the RITM record. This is the Before Insert,Update BR i created

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

    //STRY0011219 - Update the Location value on RITM from the value selected on catalog item/requestor details variable set.
	current.location = current.variables.location_address;
	
})(current, previous);

This seems to copy what i need. For reporting purposes, we need to copy this field for all historical RITM's

Is there a way I can run this BR as a background script?

I tried to use a force update background script, but i must be missing something here. The script seems to run, but when i go to the record, the field has not been updated.

var ritmGR = new GlideRecord('sc_req_item');
ritmGR.setLimit(2);
ritmGR.setWorkflow(false);
ritmGR.autoSysFields(false);
ritmGR.addEncodedQuery('active=true^locationISEMPTY');
ritmGR.query();

while (ritmGR.next()) {
   ritmGR.setForceUpdate(true);
   ritmGR.update();
}

 

 

1 ACCEPTED SOLUTION

Harish KM
Kilo Patron
Kilo Patron

Hi Sample script:

gr = new GlideRecord('sc_req_item');
gr.addQuery('active','true');
gr.query();
while (gr.next()){
gr.location = gr.variables.location_address;
gr.setWorkflow(false); //Do not run business rules
gr.autoSysFields(false); //Do not update system field
gr.update();
}

Regards
Harish

View solution in original post

3 REPLIES 3

Harish KM
Kilo Patron
Kilo Patron

Hi Sample script:

gr = new GlideRecord('sc_req_item');
gr.addQuery('active','true');
gr.query();
while (gr.next()){
gr.location = gr.variables.location_address;
gr.setWorkflow(false); //Do not run business rules
gr.autoSysFields(false); //Do not update system field
gr.update();
}

Regards
Harish

That got it, i did run this as a fix script once, and it stopped after about 14,000 records. Ran it again and it completed all the way.

 

Thanks

Bhaba
Tera Expert

Hi @Russell Abbott 

Try the below code.

(function executeRule(current, previous /*null when async*/ ) {
	
	var ritmGR = new GlideRecord('sc_req_item');
         ritmGR.addEncodedQuery('active=true^locationISEMPTY');
	 ritmGR.query();
	   while (ritmGR.next()) {
		ritmGR.variables.location_address = current.variables.location_address;
		ritmGR.setWorkflow(false);
		ritmGR.update();
		
	}
	
})(current, previous);

Hope this helps you.

Thanks