current.update() on "Insert" only Business Rule

seberly
Giga Expert

I am writing a business rule to inherit a custom field value on change_task from change_request, however when the business rule runs on update, change_task records that are created at the creation of the change_request record by a workflow running on change_request are seen as null by the before business rule.

If the business rule is run after or async, however, the values on the parent change_request record are what they were submitted as (not null).

know that current.update() in a business rule is a big no-no - I have felt the wrath of the consequences of such a business rule - but what about if a business rule only runs on insert? Then using current.update() shouldn't be an issue, right? This is my only working solution at this point, and it seems like this should be a pretty simple function, but it hasn't proven to be in practice. Any help is appreciated!

I floated the question here, but it didn't seem to get too much traction: https://community.servicenow.com/community?id=community_question&sys_id=e18772fcdb231344a39a0b55ca96...

Thanks!
Scott

13 REPLIES 13

Allen Andreas
Administrator
Administrator

Hi,

As you've mentioned, it is not best practice or really ideal to use current.update() in any variation of a BR:

https://community.servicenow.com/community?id=community_blog&sys_id=f12d66e5dbd0dbc01dcaf3231f961988

As far as what you're trying to do, you're trying to take a value from one table and copy it to another table, right?

Can you show your BR script?

And what table do you have this running on?

Please mark reply as Helpful/Correct, if applicable. Thanks!


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

Yes. I completely understand best practices, but I am wondering how to solve this issue. This solution seems to work. There doesn't seem to be any recursion or strange logging going on. Furthermore, we've had mixed results using a calculated field before for this type of use case.

BR config-------

table = change_task
insert = true
update = false
when = async
order = 500

(function executeRule(current, previous /*null when async*/) {

   current.u_field = current.parent.u_field;
   current.update();

})(current, previous);

Hi,

As Ian and Shrutika mentioned below, the big complain with current.update in BR is that it causes all before BR's to run again which could cause issues. If you do .setWorkflow(false) then in theory...that should stop all BRs from running due to this update.

🙂


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

Allen,

My issue with setWorkflow(false) is that may stop other BRs or even the workflow from processing which can't happen.

As mentioned, I've seen positive results with insert only, async, current.update().

Do you have any idea how I can prevent the change_task.parent (change_request) record from being null? I doesn't quite make sense to me why the parent (change_request) record would be null but the child (change_task) record isn't.

Thanks,
Scott