How to set state field value in before BR

Prathamesh Chav
Tera Contributor

Hi Team,

 

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

    // Add your code here
    var checkAttach = new GlideRecord("sys_attachment");
 checkAttach.addQuery("table_sys_id", current.sys_id);
 checkAttach.query();
 if (!checkAttach.next()) {
     current.setAbortAction(true);
     current.setValue("state", 1);
     current.close_notes = "";
     gs.addErrorMessage("Add attachment before closing the task");
 }
})(current, previous);
 
here I am trying to set state field value when form gets fully refreshed,
it is clearing the value for close notes but not for the state
 
here I am checking attachment is attached or not for the particular record, and its working fine,
I just want to set state field to open
 
PrathameshChav_0-1736997824228.png

Can anyone help?

4 REPLIES 4

Ankur Bawiskar
Tera Patron
Tera Patron

@Prathamesh Chav 

try this

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

// Add your code here
var checkAttach = new GlideRecord("sys_attachment");
checkAttach.addQuery("table_sys_id", current.sys_id);
checkAttach.query();
if (!checkAttach.next()) {
current.setAbortAction(true);
current.state = previous.state;
current.close_notes = "";
gs.addErrorMessage("Add attachment before closing the task");
}
})(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

@Prathamesh Chav 

Hope you are doing good.

Did my reply answer your question?

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

@Prathamesh Chav 

when is your BR running?

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

yuvarajkate
Giga Guru

Try this code:

(function executeRule(current, previous /*null when async*/ ) {
    var checkAttach = new GlideRecord("sys_attachment");
    checkAttach.addQuery("table_sys_id", current.sys_id);
    checkAttach.query();

    if (!checkAttach.next()) {
        current.setAbortAction(true);
        // Explicitly set the state to "Open" (assuming 1 is the value for "Open")
        current.state = 1; // Alternatively, you can use setValue("state", 1)
        // Clear the close_notes field
        current.close_notes = "";
        // Display an error message
        gs.addErrorMessage("Add an attachment before closing the task");
    }
})(current, previous);