Field reverts to original value when using display BR to update value on form load

alan_chia
Kilo Explorer

Hi All,

I am facing the following problem when trying to use a display business rule to modify the opened_by field with the current logged in user:

When the form loads, the opened_by field (as expected) is changed from the original creator UserX to the current logged in user. However, upon saving the record, the opened_by field is reverted back to UserX (and not the logged in user, which is what I am expecting).

If I was to manually select another user in the opened_by field, the record saves fine (ie. with the selected user as the value within the opened_by field).

I have ruled out any client scripts or ACLs that may be preventing the value to be saved so it looks like it might be how the onload script is written. Has anyone faced this issue or know what I can do to debug further (apologies, quite new to dev). Thanks in advanced!

Code Snippet:

Display Business Rule:

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

                              var fnUtil = new u_chg_Util();

                              fnUtil.updateFormOnLoad();

})(current, previous);

Script Include:

//if record is created by userX, replace the openedBy field with logged in user object

updateFormOnLoad: function(){

                              var userSysID = gs.getUserID();

                             

                              //if record is created by "userX",

                              //update OpenBy field with current logged in user

                              //apply logic when status is "open",

                             

                              if (current.opened_by.user_name == 'userX'){

                                                if (current.state == '30'){

                                                                      current.opened_by = userSysID;                            

                                                }

                            }

}

1 ACCEPTED SOLUTION

Abhinay Erra
Giga Sage

You are using current in your script include.Script include will not have access to current object. You can use onLoad client script for this.


View solution in original post

4 REPLIES 4

Brad Tilton
ServiceNow Employee
ServiceNow Employee

Hi Alan,



What's the use case for this and why are you trying to do it in a display business rule rather than a before update BR?


Abhinay Erra
Giga Sage

You are using current in your script include.Script include will not have access to current object. You can use onLoad client script for this.


Thanks for pointing me in the right direction! Got it to work with an onLoad client script.


Glad I could help