current.update in the async Business rule

MA16
Tera Expert

I am using an asynchronous BR where I am unable to update the current field value directly; instead, I have to use current. Update to achieve it. Is there any other way to accomplish this? Additionally, I have included a REST call in the BR.

1 ACCEPTED SOLUTION

Sumanth16
Kilo Patron

Hi @MA16 ,

 

Description

When you have a business rule that runs asynchronously, the actual execution of that script is delayed until one of the workers on your instance processes ASYNC: Background jobs from the sys_trigger table.

When the record is modified and the async rule is triggered, we add a record 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. 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.

Recommendations

While you can modify the current record using the normal current.setValue('field_name', 'value') method or via direct assignment using current.field_name = 'value';, and you can update the record using current.update(), this is strongly discouraged. It is not good practice to modify the current record outside of a before business rule.

If the architecture of your application calls for current.update() in a business rule, this indicates your application needs a redesign. It would point to a potential problem or incident waiting to happen, which could be avoided by only modifying the current record before it is written to the database

 

In the rare case in which a current.update() cannot be avoided and must be used as no other method can be found to accomplish the update as needed, in order to prevent the recursion that can cause the performance issues, the current.update() should be used in conjunction with the setWorkflow(false) function.  As an example, the following code snippet shows a very basic update to a record and use of the setWorkflow() function:

current.status = 1;
current.setWorkflow(false);
current.update();
current.setWorkflow(true);

 

Please refer to below articles:

https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB0715782

https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB0687840

 

If I could help you with your Query then, please hit the Thumb Icon and mark it as Correct !!

 

Thanks & Regards,

Sumanth Meda

 

 

 

 

 

View solution in original post

1 REPLY 1

Sumanth16
Kilo Patron

Hi @MA16 ,

 

Description

When you have a business rule that runs asynchronously, the actual execution of that script is delayed until one of the workers on your instance processes ASYNC: Background jobs from the sys_trigger table.

When the record is modified and the async rule is triggered, we add a record 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. 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.

Recommendations

While you can modify the current record using the normal current.setValue('field_name', 'value') method or via direct assignment using current.field_name = 'value';, and you can update the record using current.update(), this is strongly discouraged. It is not good practice to modify the current record outside of a before business rule.

If the architecture of your application calls for current.update() in a business rule, this indicates your application needs a redesign. It would point to a potential problem or incident waiting to happen, which could be avoided by only modifying the current record before it is written to the database

 

In the rare case in which a current.update() cannot be avoided and must be used as no other method can be found to accomplish the update as needed, in order to prevent the recursion that can cause the performance issues, the current.update() should be used in conjunction with the setWorkflow(false) function.  As an example, the following code snippet shows a very basic update to a record and use of the setWorkflow() function:

current.status = 1;
current.setWorkflow(false);
current.update();
current.setWorkflow(true);

 

Please refer to below articles:

https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB0715782

https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB0687840

 

If I could help you with your Query then, please hit the Thumb Icon and mark it as Correct !!

 

Thanks & Regards,

Sumanth Meda