Business rule to change field not shown on form - skip current.update()?

kuba4
Kilo Expert

Hi There!

I need to create Incident based on Requested Item, using Business Rule, something like this:

var gr = new GlideRecord('incident');

gr.initialize();

gr.assignment_group = current.assignment_group;

gr.insert();

current.u_has_incident = true;

I have a problem with field "Has incident", which is not a form field. Without current.update() at the end, it will not be changed. On the other hand I saw few articles with advice not to use current.update() in Business rules... what should I do then to make it work?

1 ACCEPTED SOLUTION

Use an after business rule to create the incident as following



var gr = new GlideRecord('incident');


gr.initialize();


gr.assignment_group = current.assignment_group;


// add your fields that has to be ampped



gr.insert();




current.u_has_incident = true;


current.update();


current.setWorkflow(false);



The last will prevent any business rule or workflow condition to execute.



Please like or mark correct based on the impact of the response.


View solution in original post

14 REPLIES 14

Ashutosh Munot1
Kilo Patron
Kilo Patron

HI Jakub,



Which business rule you are using.



Thank you,
Ashutosh


Brad Tilton
ServiceNow Employee
ServiceNow Employee

Hi Jakub,



If you need to set a field on the current record the business rule is running against you'll need to run the business rule as a before insert/update.


The SN Nerd
Giga Sage
Giga Sage

After business rules should be used when creating/updating external records.



You do then run into the problem where if you want to update a field on current only after a new record is created, you have to do a current.update().



You should never put a current.update() in a 'before' Business Rule, but I think there are some circumstances when it is ok to do so in an after rule.



ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022

andymcdonald
Kilo Guru

you shouldn't use update in a business rule unless the record has already been saved (after).   If this is a before rule, and action is insert, just set the value of the record going in and your value will be saved.   You especially don't want to use update in a before update, for obvious reasons.