gs.sleep() in business rule?

Kiddy
Tera Guru

I am using gs.sleep(5000) in after business rule ,when i submit form it is waiting for 5 seconds to submit ,does gs.sleep haluts form submission if we use it in after business rule??

 

 

1 ACCEPTED SOLUTION

Adrian Ubeda
Mega Sage
Mega Sage

Hi, 

Instead of using gs.sleep() use events for triggering that BR, it's best practice. Here's an example of delay applied on business rule and trigger event:

var when = new GlideDateTime();

var delay = new GlideTime();

delay.setValue("00:00:05");

when.add(delay);

gs.eventQueueScheduled("event_name", current, "", "", when);

If it was helpful, please give positive feedback.

Thanks, 

If it was helpful, please give positive feedback! ✔
☆ Community Rising Star 22, 23 & 24 ☆

View solution in original post

11 REPLIES 11

hi ,

 

How can i trigger a business rule from an event??

 

i have created an after business rule do i need to paste the above code in there?

Hi, 

Check this thread: https://community.servicenow.com/community?id=community_question&sys_id=86ca476ddb5cdbc01dcaf3231f96...

Thanks,

If it was helpful, please give positive feedback! ✔
☆ Community Rising Star 22, 23 & 24 ☆

Whatever you are doing is not recommended at all.

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 for more information-
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(5000);
gs.eventQueueScheduled("your_event_name", current, "previous_value_to_be_set", "", dateTime);

*Dont miss to replace the previous_value_to_be_set with the required value (should be something previous.field_name).

 

Create a script action and select this event on it. Copy paste the script you have in your BR right now and use the previous_value_to_be_set from the event in the script (using event.parm1).
https://docs.servicenow.com/bundle/orlando-platform-administration/page/administer/platform-events/r...

 

-Tanaji

Please mark response correct/helpful if applicable.

I spent an incredible amount of time trying to work out why your solution wasn't "working". Ended up the amount of seconds you are setting 1hour and 23 minutes! You mean 5 = 5 seconds not 5000, that's gs.sleep() 🙂

exactly like Ankur mentioned it is bad to use sleep(), the platform handles the processing queue 
your design is wrong if you need to use sleep(), probably the Workflow is mixed up with business rules and you can not figure out what is happening when. bad solutions will only grow the technical debt.