Use PDIs? Take our 5-minute survey to help shape the PDI roadmap.

Blog:Execute a too long running Script in Background

mugi-san
Kilo Sage

Hello, Everyone.

If you need to update a massive number of records urgently or do something, you might consider using a background script. In this feature, unchecking the 'Cancel after 4 hours' checkbox removes the guardrail, allowing long-running operations.(If you really need to execute it.)

Once you understand its behavior, you can consider implementing it using other ServiceNow features as needed.

 

Here is an example of the script:

doingTooMany();

function doingTooMany(){
	gs.log("START:Execute a too long running.");
	var lgr = new GlideRecord("cmdb_ci");
	lgr.addEncodeQuery('sys_class_name=cmdb_ci^EQ');
	lgr.query();
	gs.log("BEFORE:Execute a too long running.");
	gs.log("COUNT:" + lgr.getRowCount());
	while(lgr.next()){
		var nowlgr = new GlideRecord("cmdb_ci");
		if(nowlgr.get(lgr.getUniqueValue())){
			// update something or script (use "nowlgr").
            /* nowlgr.foobar = piyopiyo */

			// if you need
			nowlgr.setWorkflow(false);
			nowlgr.autoSysFields(false);
			nowlgr.update();

			// consider system performance
			gs.sleep(1000);
		}
	}
	gs.log("END:Execute a too long running.");
}

 

Execution History Table

    sys_script_execution_history.list

 

mugisan_2-1786071107770.png

 

Terminate

    v_transaction.list

mugisan_1-1786071064453.png

 

Transaction(Search from All node)

    v_cluster_transaction.list

mugisan_0-1786602005553.png

 

 

 

in v_transaction, select record and use menus as below:    

mugisan_0-1785986917901.png

 

regards,

 

1 ACCEPTED SOLUTION

mugi-san
Kilo Sage

Hi,Everyone.

From the perspectives of security and availability, we recommend implementing this using a Fix Script (or similar features).

For instance, with a Fix Script, you can explicitly select "Proceed in Background" via the button on the screen. The execution is then processed through the sys_trigger queue. Furthermore, logs generated within the script (e.g., gs.info) are automatically linked to the related list of that specific Fix Script record.

However, it is not possible to temporarily disable guardrails just for a one-time execution like the approach described in this post.

As mentioned in other posts, you should either narrow down the target batch size so that the script completes within 4 hours, or modify the timeout setting in the sysrule_quota record for the "Fix Script Processor", which can be extended up to a maximum of 30 days.

mugisan_1-1786204325002.png

 

mugisan_0-1786204239274.png

 

If you would also like to enable the rollback feature as with background scripts, please refer to the knowledge base article below.

https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB1588181

 

Regards.

 

View solution in original post

2 REPLIES 2

Mark Manders
Giga Patron

'Urgently' update a massive number of records, should not take over 4 hours. Update your script instead, or run it in batches. This is really terrible advice. You run the risk of blocking your instance entirely.


Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark

mugi-san
Kilo Sage

Hi,Everyone.

From the perspectives of security and availability, we recommend implementing this using a Fix Script (or similar features).

For instance, with a Fix Script, you can explicitly select "Proceed in Background" via the button on the screen. The execution is then processed through the sys_trigger queue. Furthermore, logs generated within the script (e.g., gs.info) are automatically linked to the related list of that specific Fix Script record.

However, it is not possible to temporarily disable guardrails just for a one-time execution like the approach described in this post.

As mentioned in other posts, you should either narrow down the target batch size so that the script completes within 4 hours, or modify the timeout setting in the sysrule_quota record for the "Fix Script Processor", which can be extended up to a maximum of 30 days.

mugisan_1-1786204325002.png

 

mugisan_0-1786204239274.png

 

If you would also like to enable the rollback feature as with background scripts, please refer to the knowledge base article below.

https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB1588181

 

Regards.