Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

State is not setting to previous value in before update BR?

Saranya K
Tera Contributor

Hi all,

When change task move to close state   if there is no attachment change task abort the action  and set state and closed code to previous value.

 

I have written Before update BR on change_task table:

condition state change to closed.

 

only the state is not set to previous value

 

can anyone helpme on this.

 

var attach = new GlideRecord('sys_attachment');
    attach.addQuery('table_name', 'change_task');
    attach.addQuery('table_sys_id', current.sys_id);
    attach.query();
    if (!attach.next()) {
 
        gs.addErrorMessage("Kindly attach evidence of successful completion of the CTASK (email comms/ Screenshots)");
current.state = previous.state;
        current.close_code = previous.close_code;
current.setAbortAction(true);
}
 
 
12 REPLIES 12

Hi @Roshnee Dash ,

 

In the above  client script how the previous will work

 

 g_form.setValue('state', g_form.getValue('previous.state'));
            g_form.setValue('close_code', g_form.getValue('previous.close_code'))

 

@Saranya K "previous" object cannot be used in client scripts.

Yes @Saranya K 

  • previous.state is only accessible server-side in a Business Rule.

  • Storing the previous values when the form loads allows the Client Script to reference them later.

  • When the user tries to submit, the script checks for attachments and reverts the state using the stored values instead of attempting to use previous.state.

  • Therefore, you can return the required values from the server script in JSON format, including the error message, state value, and close code. Since the form has not yet been saved during this process, it will retrieve the previously stored value.

 

 

Your feedback makes the community stronger! If you found this helpful, marking it as the correct answer helps others.
Stay awesome,
Roshnee Dash