state changes while business rule executes

ramesham
Tera Contributor

Hi,

I have created the business rule to make attachment mandatory while closing the problem task. When state is changed to closed it validates the attachment and gives the warning message.

The concern is the state moves to closed state but actually it does not closed and we have to refresh the page. How do keep the state as working progress while business rule is executed.

ramesham_0-1715579827130.png

I have used the below code. let me know if any changes required.

 

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

 

    // Add your code here

    var gr=new GlideRecord("sys_attachment");

    gr.addQuery("table_sys_id", current.sys_id);

    gr.query();

    if(!gr.next()){

        gs.addErrorMessage("This task cannot be closed until a final summary or conclusion is attached");

        current.state = previous.state;

        current.setAbortAction(true);

    }

   

})(current, previous);

 

2 REPLIES 2

Ankur Bawiskar
Tera Patron
Tera Patron

@ramesham 

try this i.e. reverse the lines

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


// Add your code here
var gr = new GlideRecord("sys_attachment");
gr.addQuery("table_sys_id", current.sys_id);
gr.query();
if (!gr.next()) {
gs.addErrorMessage("This task cannot be closed until a final summary or conclusion is attached");
current.setAbortAction(true);
current.state = previous.state;
}

})(current, previous);

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

Ankur Bawiskar
Tera Patron
Tera Patron

@ramesham 

another way is to have onChange on state and check if state changes to closed and then make a GlideAjax call to check if file is attached

If not then show error message using g_form.addErrorMessage()

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