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

@Saranya K 

not possible.

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

J Siva
Kilo Patron
Kilo Patron

Hi @Saranya K 
The abort business rule only prevents data updates in the database. Although the state value may appear as "Closed," it will revert to the previous value once you refresh the page.

Regards,
Siva

Saranya K
Tera Contributor

Hi @J Siva 

I want to set state value to previous one without refreshing the form how to achieve it .please let me know

 

Thanks

@Saranya K I don't think it's possible. it's OOB behavior.

Roshnee Dash
Tera Guru
Tera Guru

Hi @Saranya K ,
To update the value without requiring a page reload, it is advisable to use a Client Script instead of a Business Rule.

To update the state and close_code without reloading the page in ServiceNow, you can use GlideAjax to make an asynchronous call to a Script Include. This way, you can validate attachments and update the fields dynamically without forcing a page reload.

Steps to Achieve This:

  1. Create a Script Include (Server-Side)

    • Go to Server-Side Script Include and create a new Script Include (set it as Client Callable).

    • Use the following script to check for attachments and revert the state:

var ValidateAttachment = Class.create();
ValidateAttachment.prototype = {
    initialize: function() {},

    checkAttachment: function(changeTaskId) {
        var attach = new GlideRecord('sys_attachment');
        attach.addQuery('table_name', 'change_task');
        attach.addQuery('table_sys_id', changeTaskId);
        attach.query();
        
        if (!attach.next()) {
            var task = new GlideRecord('change_task');
            if (task.get(changeTaskId)) {
                return JSON.stringify({
                    errorMessage: "Kindly attach evidence before closing.",
                    previousState: task.state,
                    previousCloseCode: task.close_code // you need to check your logic and update
                });
            }
        }
        return JSON.stringify({
            successMessage: "Valid attachment found."
        });
    }
};

 

  • Create a Client Script (Client-Side)

    • Go to Client Scripts and create a new script that runs onSubmit.

    • Call the Script Include using GlideAjax:

 

function onSubmit() {
    var ga = new GlideAjax('ValidateAttachment');
    ga.addParam('sysparm_name', 'checkAttachment');
    ga.addParam('sysparm_changeTaskId', g_form.getValue('sys_id'));

    ga.getXMLAnswer(function(response) {
        var result = JSON.parse(response);

        if (result.errorMessage) {
            g_form.addErrorMessage(result.errorMessage);
            g_form.setValue('state', result.previousState);
            g_form.setValue('close_code', result.previousCloseCode);
            return false; // Prevent form submission
        }
    });

    return false; // Ensures submission is stopped if validation is needed
}

 

 

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