The Zurich release has arrived! Interested in new features and functionalities? Click here for more

Using Business rule to run script include in the background

huntj06
Tera Guru

I'm creating outage records based on the affected CI's in my incident. The BR will only create the outage records if a few parameters are met.

1) It's a P1 or P2

2) Category is not equal to " <cat name> "

I currently have the code running when a incident is "restored".

My problem is, some affected CI's have more than one outage record and the code takes a while to run. Sometimes upto 2 mins, which creates a bad user experience...

My business rule (see attachment for conditions).

[JavaScript] BR - Outage Record - Pastebin.com

I'm then calling a script include to execute the code..however, during this time the user is waiting for these code execution to finish and it creates a bad user experience.

[JavaScript] ScriptInclude - Outage Record - Pastebin.com

Any ideas to have this code execute in the background so the user can continue on and not worry about this? The end user setting the incident to restore doesn't work outage records so they don't need them immediately available.

1 ACCEPTED SOLUTION

Hi Jeffrey,



You're going to have one challenge with async rules and that's because there is no "previous" object so conditions like "changesTo()" are not available because it doesn't know what it WAS. When scripting, the previous object is unavailable.



One solution we came up with today was to have your business rule trigger an event and have a script action respond to the event. Script actions run in the background. That means you need to pass everything you need through the arguments to gs.eventQueue() which may mean stringifying (using JSON) some of your objects. It can be done, but it's a bit more "roll your own" when it comes to processing things in the background.



Script Actions - ServiceNow Wiki


GlideSystem - ServiceNow Wiki (section 3.1)


Script Actions - Business Rules' cousin


View solution in original post

4 REPLIES 4

Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

Hi Jeffrey,



You can use an async business rule in place of an after business rule. Async business rules are similar to after rules in that they run after the database commits a change. Unlikeafter rules, async rules run in the background simultaneously with other processes. Async business rules allow the system to return control to the user sooner but may take longer to update related objects.


Business Rules Best Practices - ServiceNow Wiki


Thanks for the reply Pradeep.



Since the ASYNC business rule actually takes away the conditions, do I just build them out here instead?



(see attached)


Hi Jeffrey,



You're going to have one challenge with async rules and that's because there is no "previous" object so conditions like "changesTo()" are not available because it doesn't know what it WAS. When scripting, the previous object is unavailable.



One solution we came up with today was to have your business rule trigger an event and have a script action respond to the event. Script actions run in the background. That means you need to pass everything you need through the arguments to gs.eventQueue() which may mean stringifying (using JSON) some of your objects. It can be done, but it's a bit more "roll your own" when it comes to processing things in the background.



Script Actions - ServiceNow Wiki


GlideSystem - ServiceNow Wiki (section 3.1)


Script Actions - Business Rules' cousin


Hi Jeffrey,



Yes, please adjust your conditions over condition field or in the script section by using if statement. The business rule will run as long as it matches the condition specified. Async business rules run just like an after BR rule except they wait until the system gets to them.


If it doesn't work set the order field to high number and test once.



Please let us know if you are blocked.