May 28, 2023

Async Business Rules in ServiceNow

 There are four types of Business Rules in ServiceNow

  1. Before business rules
  2. After business rules
  3. Display business rules
  4. Async business rules

    Business rules in ServiceNow are executed synchronously, meaning they run in the same order as they are defined and block the execution until they complete. However, ServiceNow provides other mechanisms for asynchronous processing and handling long-running tasks. Async Business Rules are best used when you don't want the result to be immediately displayed and do not want to halter the user experience since the control is with the client and in background it is processing. 

    Like after Business Rules, async Business Rules execute their logic after a database operation occurs. Unlike after Business Rules, async Business Rules execute asynchronously. Async Business Rules execute on a different processing thread than before or after Business Rules. 

    When the record is modified and the async rule is triggered, a record is inserted to the sys_trigger table. That record holds a reference to the business rule that we need to run, and to the particular record to use as current within that business rule. They are queued by a scheduler to be run as soon as possible. This allows the current transaction to complete without waiting for the Business Rules execution to finish and prevents freezing a user's screen. When the worker picks up the job, a scripting environment is created. The record is loaded from the database as it is at the time the script runs and is put into the scripting environment as the variable current. You can then interact with that variable as you normally would.

    To see async Business Rules queued up for execution, use the All menu in the main ServiceNow window (not Studio) to open System Scheduler > Scheduled Jobs > Scheduled Jobs. Look for Scheduled Job names starting with ASYNC. They go in and out of the queue very quickly and can be hard to catch on the schedule.


Here are some key points about "async" business rules in ServiceNow:

  •  Asynchronous business rules do not have access to the previous version of a record. 
  • Therefore, the changes(), changesTo(), and changesFrom() GlideElement methods do not work with async rule script. However, the condition builder and condition field (advanced view) both support the changes(), changesTo(), and changesFrom() methods.




 

 

0 comments: