Change a Form Variable based on Approval

Talls
Tera Contributor

Hello,

 

I am newer to the ServiceNow Dev world and I have a requirement I am trying to figure out. It seems simple but I think its going to require both a Business Rule (to update the server) and a catalog script. Basically, my workflow has two approvals, 1 to the requestors direct manager, then another to a group. On the form there is a variable called gatekeeper with a Yes or No option. If the user selects Yes, and the secondary approval is Rejected, I want the form variable to switch to No. I have the workflow set up after this point to continue provisioning access, but in the case of the secondary rejection, the user will just receive standard non-gatekeeper access to the item they are requesting.

 

I am hoping for some assistance as I'm pretty sure I need to script this.

 

I have a Run Script Utility in my workflow following the second approval task as rejected, in it I have:

 

if (current.sysapproval_group == 'rejected') {

    current.variables.gatekeeper = 'no';

}

 

Any assistance is appreciated.

 

Thanks!

1 REPLY 1

Kartik Magadum
Kilo Sage

Hello @Talls 

Business Rule:

First, you'll need a Business Rule that triggers when the secondary approval task is rejected. Here's how your Business Rule might look:

 

 

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

    if (current.sysapproval_group == 'rejected') {
        current.variables.gatekeeper = 'no';
        current.update();
    }

})(current, previous);

 

 

 

This Business Rule will run when the secondary approval task is rejected and update the gatekeeper variable to 'no'.

 

Catalog Client Script:

For the Catalog Client Script part, you can use something like this:

 

 

function onSubmit() {
    var gatekeeperField = g_form.getField('gatekeeper');
    var gatekeeperValue = gatekeeperField.getValue();
    var approvalStatus = g_form.getValue('approval_status'); // Assuming this is the approval status field

    if (gatekeeperValue == 'yes' && approvalStatus == 'rejected') {
        g_form.setValue('gatekeeper', 'no');
    }

    return true;
}

 

This script checks whether the gatekeeper variable is set to 'yes' and the approval status is 'rejected'. If both conditions are met, it changes the gatekeeper variable to 'no'.

 

Make sure you attach this Catalog Client Script to the catalog item form.

Remember to adjust the field names and conditions according to the actual field names and logic in your instance.

 

Please Mark my Solution as Accept and Give me thumbs up, if you find it Helpful.

 

Thanks & Regards,

Kartik Magadum