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

Prevent form submission if no approval using business rule

TanushaP
Tera Contributor
I used before insert and update business rule  i.e when change request risk is high and state is authorize form should save when atleast one approval is got but without any approvals also form is saving can someone help
 
 
 
(function executeRule(current, previous /*null when async*/ ) {

    // Add your code here
    if (current.risk == 'High'&& current.state==-3)
    {
        // Query the Approvers table (usually sysapproval_approver)
        var gr = new GlideRecord('sysapproval_approver');
        gr.addQuery('sysapproval', current.sys_id); // link to this Change Request
        gr.addQuery('state', 'approved'); // look for at least one Approved
        gr.query();

        if (!gr.hasNext()) {
            gs.addErrorMessage('High-risk changes require at least one approver in Approved state before submission.');
            current.setAbortAction(true); // block save
        }
    }


})(current, previous);
1 ACCEPTED SOLUTION

Brad Bowman
Kilo Patron

Adding some logs or looking at a record would reveal that the value of the risk field when selecting 'High' is 2 - out of box, at least, you should confirm this in your environment then change your first if condition:

if (current.risk == 2 && current.state == -3) {

 

View solution in original post

2 REPLIES 2

Brad Bowman
Kilo Patron

Adding some logs or looking at a record would reveal that the value of the risk field when selecting 'High' is 2 - out of box, at least, you should confirm this in your environment then change your first if condition:

if (current.risk == 2 && current.state == -3) {

 

thank you for the reply Brad Bowman i tried same yesterday changing backend value of risk to -2 instead of HIGH this approach worked for me