We've updated the ServiceNow Community Code of Conduct, adding guidelines around AI usage, professionalism, and content violations. Read more

Business Rule does not abort State advancement

lbless1
Giga Expert

I have a BR to require an attachment, however it is not preventing the State from progressing.

 

lbless1_0-1771372671079.png

 

(function executeRule(current, previous) {

// Only enforce when moving to Screening
if (current.state == '3' && previous.state != '3') {

var att = new GlideRecord('sys_attachment');
att.addQuery('table_sys_id', current.sys_id);
att.addQuery('table_name', 'dmn_demand');
att.setLimit(1);
att.query();

if (!att.hasNext()) {
gs.addErrorMessage('At least one attachment is required before moving to Screening.');
current.setAbortAction(true);
}
}

})(current, previous);
6 REPLIES 6

Ankur Bawiskar
Tera Patron

@lbless1 

you are already checking state value in BR filter condition, so no need of that in script to check

(function executeRule(current, previous) {

    var att = new GlideRecord('sys_attachment');
    att.addQuery('table_sys_id', current.sys_id);
    att.setLimit(1);
    att.query();
    if (!att.hasNext()) {
        gs.addErrorMessage('At least one attachment is required before moving to Screening.');
        current.setAbortAction(true);
    }

})(current, previous);

OR

You can use this small script and no need to query sys_attachment table

(function executeRule(current, previous) {

    if (!current.hasAttachments()) {
        gs.addErrorMessage('At least one attachment is required before moving to Screening.');
        current.setAbortAction(true);
    }

})(current, previous);

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

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

Tanushree Maiti
Giga Sage

Hi 

 

Please correct your if condition- 

(function executeRule(current, previous) {

// Only enforce when moving to Screening
if (current.state == 3 && previous.state != 3) {

var att = new GlideRecord('sys_attachment');
att.addQuery('table_sys_id', current.sys_id);
att.addQuery('table_name''dmn_demand');
att.setLimit(1 );
att.query();

if (!att.hasNext()) {
gs.addErrorMessage('At least one attachment is required before moving to Screening.');
current.setAbortAction(true);
}
}

})(current, previous);
Hope , it will work!
Please mark this response as Helpful & Accept it as solution if it assisted you with your question.
Regards
Tanushree Maiti
ServiceNow Technical Architect
Linkedin: