With business rules Is it possible to delay the execution of the script by about 30 seconds?

SS64
Tera Contributor

With business rules Is it possible to delay the execution of the script by about 30 seconds?

20 REPLIES 20

Hitoshi Ozawa
Giga Sage
Giga Sage

Instead of putting a delay in the business rule, create a scheduled job that will create an event after 30 seconds. Create a flow with a script to execute on a trigger.

Thankyou.

Could you tell me more how to do?

 

Gaurang Soni Aa
Kilo Expert

Hi,

For a similar requirement I created a function to pause execution for a while,

but I wouldn't recommend to use it for longer duration like 30 seconds, but still just in case you need it.

	function pause(time){  
		var t1 = new GlideDateTime().getNumericValue();  
		var t2 = new GlideDateTime().getNumericValue();  
		var duration = t2 - t1;  
		while(duration < time){  
			t2 = new GlideDateTime().getNumericValue();  
			duration = t2 - t1;  
		}  
	}

 

Please mark as helpful if it helped.

 

Gaurang Soni

| https://aavenir.com |

 

 

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

Can you please explain your business use-case/requirement around this

You can delay it by 30seconds something like this

But better way is to use eventQueue() approach

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

    // Add your code here

var ms = 30000; // 30 seconds to milliseconds

var endSleep = new GlideDuration().getNumericValue() + ms;
  while ( new GlideDuration().getNumericValue() < endSleep) {
   //wait
  }

// then your logic

})(current, previous);

EventQueue approach:

Instead trigger a scheduled event from your BR which in turn triggers a script action having script to update the metric instance. You can schedule an event to run in future at any given time.

You need to register a new event in event registry. Check this URL
https://developer.servicenow.com/app.do#!/document/content/app_store_learnv2_automatingapps_newyork_...

Put this code in your BR-

var dateTime = new GlideDateTime();
dateTime.addSeconds(30);
gs.eventQueueScheduled("your_event_name", current, "previous_value_to_be_set", "", dateTime);

Regards
Ankur

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