The CreatorCon Call for Content is officially open! Get started here.

Fix script to update multiple records

RudhraKAM
Tera Guru

We have an issue when a request is create and if it has only one RITM and if the RITM is closed incomplete and set to active false but the parent request is still open and active true , We fixed the Bug but data need to be fixed , can some one help me with the script

find_real_file.png

8 REPLIES 8

this is not working 

The relationship between sc_request and sc_req_item is one to many and the above script does not allow for sc_request with more than 1 sc_req_item, but as a ServiceNow developer you already know that - right?

 

Prasant Kumar 1
Kilo Sage

 

Hi,

Find the below code :-

var gr = new GlideRecord('sc_req_item');
gr.addEncodedQuery('state=4^active=false');
gr.setLimit(5);//this will update only 5 records only for testing purpose, please remove this line so that all the records will be updated at once
gr.query();
while(gr.next()){
	var gr2=new GlideRecord('sc_request');
	gr2.addActiveQuery();
	gr2.query();
	if(gr2.next()){
		gr2.setValue('state','value_of_state_you_want_to_set');//4 is the value for closed incomplete and 3 for complete
		gr2.update();
	}
}

If i was able to solve your query then mark my answer correct and helpful.

Thanks & Regards,

Prasant kumar sahu

Balaji Jagannat
Kilo Guru

You can use this code. First run with commit = false to check the list of records that will be updated and then run the query with commit = 'true' to actually update it. 

 

var commit = 'false'

var grRequest = new GlideRecord("sc_request");
grRequest.addActiveQuery();
grRequest.query();
while (grRequest.next()) {
var grRequestedItem = new GlideRecord("sc_req_item");
grRequestedItem.addQuery("request",grRequest.sys_id);
grRequestedItem.addQuery('active', false);
grRequestedItem.query();
if(grRequestedItem.getRowCount() == 0) {
gs.print(grRequest.number + " | " + grRequest.openedby.name + " | " + grRequest.syscreated_on + " -This request will be closed by the script");
if(commit =='true'){
grRequest.active = false;
grRequest.update();
}
}
}