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

After Business Rule

Mark Wood
Tera Contributor

Hello Experts,

I want to set the field value for the work note field on the incident form when the incident state is updated to resolve. I am trying to use after business rule. but it's not working when I try with the before business rule it's working. please tell me why I am not able to set a value on record to get an update. thank you.

 

//Before Update Business Rule Code
(function executeRule(current, previous /*null when async*/) {
	var arr_ci=[];
	var gr=new GlideRecord('task_ci');
	gr.addQuery('task',current.sys_id);
	gr.query();
	while(gr.next())
	{
arr_ci.push(gr.getDisplayValue('ci_item'));

	}

	current.work_notes = 'Task is currently assigned to InfoSec team'+arr_ci;

})(current, previous);
5 REPLIES 5

Aniket Chavan
Tera Sage
Tera Sage

Hello @Mark Wood ,

The reason why the work notes were not added initially is because the business rule is set to 'after update,' and by the time it is executed, the transaction has already been saved. To make additional changes to the record in an 'after' business rule, it's necessary to use the following syntax:

 

    // Update the current record after the transaction
    current.update();

 

This line ensures that the changes made within the 'after' business rule, such as updating the work notes, are applied to the record after the transaction has been completed.


Please try with the modified script below and let me know how it works for you.

(function executeRule(current, previous /*null when async*/) {
    var arr_ci = [];
    var gr = new GlideRecord('task_ci');
    gr.addQuery('task', current.sys_id);
    gr.query();
    while (gr.next()) {
        arr_ci.push(gr.getDisplayValue('ci_item'));
    }

    current.work_notes = 'Task is currently assigned to InfoSec team' + arr_ci;

    // Update the current record after the transaction
    current.update();

})(current, previous);

 

Let me know your views on this and Mark Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.

 

Thanks,

Aniket


 

Saurabh Gupta
Kilo Patron
Kilo Patron

Hi @Mark Wood ,
As the word means "onAfter": after the database operation complete. 
Below line of code will not work as database operation already conclude.

 

 

	current.work_notes = 'Task is currently assigned to InfoSec team'+arr_ci;

 

 

you can still update the work_notes in onAfter business rule using current.update() for testing purpose (current.update() is not recommended by servicenow as there are implications of doing so, don't do this in prod)

 

C

If the provided solution meets your needs, kindly consider marking it as helpful and accepting it as the solution. This helps others who may have similar questions.


Thanks and Regards,

Saurabh Gupta

That footer layout looks familiar. Where did you get the inspiration from 😋

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

Hi @Mark Roethof 
Of course from the master, I just re-used yours Mark.



Thanks and Regards,

Saurabh Gupta