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

Background script to update closed date and time for incidents

nagaraj_mk
Kilo Contributor

We have around 4lach inactive incidents which is not updated closed date and time stamp.

Planning to use back ground script to update the resolved date and time stamp to closed option as well. Is there any impact if we update all together in one shot ?

How we can analyse the impact if we perform in one go or any idea how we can proceed further. Need expert advise to take action to update for 4lach incidents.

Note: having below code to update the incident closed date and time currently.

 

var gr = new GlideRecord('incident');
gr.addEncodedQuery("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"); (I will paste the query link which shows the incident list here.)
gr.query();
var count = 0;
while(gr.next()){
count++;
gr.closed_at=gr.resolved_at;
gr.update();
}

//gr.setWorkflow(false);
gs.print(count);

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

Please do this

1) use fix script to update the records in background

2) also divide the fix script into chunks if possible

3) use setWorkflow(false) if you wish to avoid the BR from running on INC when INC is closed/resolved

4) also use try catch block to handle exception

5) wrap the code inside function

updateINC();

function updateINC(){

	try{
		var gr = new GlideRecord('incident');
		gr.addEncodedQuery("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"); //(I will paste the query link which shows the incident list here.)
		gr.query();
		var count = 0;
		while(gr.next()){
			count++;
			gr.closed_at=gr.resolved_at;
			gr.setWorkflow(false);
			gr.setUseEngines(false); // to prevent data policy from blocking the update
			gr.update();
		}
		gs.info('Total INC updated' + count);
	}
	catch(ex){
		gs.info('exception' + ex);
	}
}

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

View solution in original post

4 REPLIES 4

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

Please do this

1) use fix script to update the records in background

2) also divide the fix script into chunks if possible

3) use setWorkflow(false) if you wish to avoid the BR from running on INC when INC is closed/resolved

4) also use try catch block to handle exception

5) wrap the code inside function

updateINC();

function updateINC(){

	try{
		var gr = new GlideRecord('incident');
		gr.addEncodedQuery("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"); //(I will paste the query link which shows the incident list here.)
		gr.query();
		var count = 0;
		while(gr.next()){
			count++;
			gr.closed_at=gr.resolved_at;
			gr.setWorkflow(false);
			gr.setUseEngines(false); // to prevent data policy from blocking the update
			gr.update();
		}
		gs.info('Total INC updated' + count);
	}
	catch(ex){
		gs.info('exception' + ex);
	}
}

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Hi Ankur,

 

Thanks for the above steps.

Is there any performance impact if we ran 4lachs incident update in single go ?, Please advise how we can proceed further.

 

Regards,

Nagaraj

Hi,

please run it in non-business hours for less impact.

if possible please break the records in chunks and then run

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Community Alums
Not applicable

This article, using Ankur's code, will help you run this with no impact, and means you can run it during business hours too without causing any performance issues. 

Article link