custom approval email

panchbhaic
Tera Contributor

I have created a custom approval email notification for a specific catalog item, and I want to disable the OOB approval email for that catalog item which is on sysapproval_approver.

Below is the script that I tried but it blocked notification for all catalog item approvals.

(function() {
    answer = true;
    // Check if the approval is related to a catalog item request
    var usbFormSysId = "3286631f1b9f021005cefc8e034bcbd5";


    if (current.approval_for && current.approval_for.getTableName() === "sc_req_item") {
        var ritm = current.approval_for.getRefRecord();
 
        // Check if this is the USB Access request (adjust the condition as needed)
        if (ritm && ritm.cat_item == usbFormSysId) {
            answer = false;
        }
       
    }
 
})();


Any help will be appreciated.
Thanks in advance.
3 REPLIES 3

Voona Rohila
Tera Patron

Hi @panchbhaic 

Please replace if part of code with below logic and give it a try

if (current.sysapproval && current.sysapproval.sys_class_name == "sc_req_item") {
        var ritm = current.sysapproval.getRefRecord();
       
        // Check if this is the USB Access request (adjust the condition as needed)
        if (ritm && ritm.cat_item == usbFormSysId) {
            answer = false;
        }
       
  }

  

aruncr0122
Giga Guru

Hi @panchbhaic ,

 

In notifications (and business rules with condition scripts), ServiceNow expects the script to return a boolean (true = condition passes / notification fires, false = condition fails / suppressed).
If you just set answer but don’t explicitly return it, behavior can vary:
Sometimes it works because the engine picks up the global answer variable.
Sometimes it doesn’t, especially in self-invoking functions like your last example ((function(){ ... })();), because the scope of answer may not bubble up.

If you keep the self-invoking function, you must return answer:
(function() {
var answer = true; // default: suppress

var usbFormSysId = "3286631f1b9f021005cefc8e034bcbd5";

if (current.approval_for && current.approval_for.getTableName() === "sc_req_item") {
var ritm = current.approval_for.getRefRecord();
if (ritm && ritm.cat_item == usbFormSysId) {
answer = false; // allow for this item
}
}
return answer;
})();


Or if you don’t need the wrapper, just do:

answer = true;
var usbFormSysId = "3286631f1b9f021005cefc8e034bcbd5";

if (current.approval_for && current.approval_for.getTableName() === "sc_req_item") {
var ritm = current.approval_for.getRefRecord();
if (ritm && ritm.cat_item == usbFormSysId) {
answer = false;
}
}

Try above options and I hope this should work.

kaushal_snow
Giga Sage

Hi @panchbhaic ,

 

You should use the Advanced condition field on the OOB notification and/or adjust the script so it only returns false for that catalog item, and true otherwise. Also, consider using the wf_activity field or checking current.sysapproval > cat_item to identify which catalog item the approval is for............


You can do this in two places:

 

Modify the Condition / Advanced Condition on the OOB Approval Notification

use script:
if (current.approval_for && current.approval_for.getTableName() == 'sc_req_item') {
var ritm = current.approval_for.getRefRecord();
if (ritm && ritm.cat_item.toString() == '3286631f1b9f021005cefc8e034bcbd5') {
return false;
}
}
return true;
})();

 

If you found my response helpful, please mark it as ‘Accept as Solution’ and ‘Helpful’. This helps other community members find the right answer more easily and supports the community...'