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

Sending out wrong events for Emergency change Request

Pooja Khatri
Tera Contributor

Hi All ,

 

I have written the below script to trigger different sorts of event for approval based on different conditions , so in the below script for emergency change request approvals it should trigger event approval.inserted but it is triggering event nc.approve.inserted

 

I have written the after (insert , update) BR on the sysapproval_approver table 

 

How can I fix it ?

Script :

 

function CR() {
var task = current.sysapproval.sys_class_name || current.source_table;
return (task == 'sr');
}

function CST() {
var task = current.sysapproval.sys_class_name || current.source_table;
return (task == 'st');
}

function CSC() {
var task = current.sysapproval.sys_class_name || current.source_table;
return (task == 'scp');
}

function CNC() {
if (current.source_table == 'change_request' || current.sysapproval.sys_class_name == 'change_request') {
var changeSysId = current.document_id;
var assignedto = current.approver.sys_id.toString();
var CR = new GlideAggregate('change_request');
CR.addEncodedQuery("type=Normal^sys_id=" + changeSysId + "^assigned_to=" + assignedto + "^ORrequested_by=" + assignedto);
CR.addAggregate('COUNT');
CR.query();
var count = 0;
if (CR.next()) {
count = CR.getAggregate('COUNT');
}
if (count > 0) {
return false;

} else {
return true;
}
} else
return true;
}


var isRequest = CR();
var isSCTask = CST();
var isStdChange = CSC();
var isnormalchange = CNC();

if (current.state.changes() && current.state == 'requested') {
event = "approve.inserted";
if (isRequest) {
event = "req.approve.inserted";
} else if (isSCTask) {
event = "st.approve.inserted";
} else if (isStdChange) {
event = "scp.approve.inserted";
} else if (isnormalchange) {
event = "nc.approve.inserted";
}
else if (!(current.source_table == 'change_request' || current.sysapproval.sys_class_name == 'change_request'))
event = "approval.inserted";
if (event != "") {
gs.eventQueue(event, current, gs.getUserID(), gs.getUserName());
updateTask(current, current.approver.getDisplayValue() + " requested to approve task");
}
}

0 REPLIES 0