Delay processing of business rule

etabalon
Mega Expert

Is there some kind of function or code that I can use to delay the processing of a business rule?

By default, once a record is updated and the BR condition is met, the script runs immediately.

What I want to do is delay the running of the script for at least 15 seconds.

This BR sends the ticket info to another system (web service/SOAP).

Thank you!
Enrique

 

1 ACCEPTED SOLUTION

Elijah Aromola
Mega Sage

There is a method available in global apps called gs.sleep() where you pass the number of milliseconds you want it to wait. You can also write your own method and call it using something like:

//available in global scope
gs.sleep(5000); //5 seconds


// Custom function if you can't use gs.sleep
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;  
  }  
},

Both of these would have an impact on your system since you are calling it on the server. Is there any reason you're unable to send it right away?

Please mark my answer as helpful/correct if it resolved your issue!

View solution in original post

4 REPLIES 4

Elijah Aromola
Mega Sage

There is a method available in global apps called gs.sleep() where you pass the number of milliseconds you want it to wait. You can also write your own method and call it using something like:

//available in global scope
gs.sleep(5000); //5 seconds


// Custom function if you can't use gs.sleep
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;  
  }  
},

Both of these would have an impact on your system since you are calling it on the server. Is there any reason you're unable to send it right away?

Please mark my answer as helpful/correct if it resolved your issue!

Thanks Elijah for your response. I'll try your suggestion and will mark as correct answer after testing. 

The reason why we don't want to send it right away, it's because after the record is inserted (we upsert XML to Planview), the user can immediately approved the ticket (SNOW sends another update to Planview) and what i'm hearing is in this case, the upsert code was still in progress and so we want some kind of delay.

 

Enrique

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

you can either use some sleep in the script or use async business rule which is processed after some time

use below code to make the script wait for 10 seconds; you can use your own time

this would work in global and scope app as well

var seconds = parseInt(10, 10) * 1000;

var start = parseInt(new Date().getTime()) + seconds;

while(start>parseInt(new Date().getTime())){

// do nothing

}

Mark Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Ankur

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

Switching the BR to async solved my issue, thanks 🙂