Issue with updating RITM records is triggering a large number of emails

vivek11
Tera Contributor
var ritmGR = new GlideRecord('sysapproval_approver');
ritmGR.addEncodedQuery('state=requested^sysapproval.state=4'); // Target approvals for specific RITMs
ritmGR.query();
 
while (ritmGR.next()) {
    // Set approver state to rejected
    ritmGR.state = 'rejected';
    ritmGR.update();
 
    // Now update the corresponding RITM(s)
    var reqItemGR = new GlideRecord('sc_req_item');
    reqItemGR.addQuery('sys_id', ritmGR.sysapproval); // Match by sysapproval (RITM ID)
    reqItemGR.query();
 
    while (reqItemGR.next()) {
reqItemGR.setWorkflow(false);
        reqItemGR.autoSysFields(false);
        reqItemGR.setValue('state',4);
reqItemGR.setValue('stage','Closed Incomplete');
        reqItemGR.setValue('approval','rejected');
reqItemGR.setValue('active',false);
        reqItemGR.work_notes = 'Update by Fix Script';
        reqItemGR.update();
    }
}



Above script is directly updating the approval and RITM states in ServiceNow, which is triggering notification rules and business logic — especially since you're changing the approval state and setting RITMs to closed/incomplete. That explains the mail blast i an experiencing. Could you please me?

3 REPLIES 3

Ankur Bawiskar
Tera Patron
Tera Patron

@vivek11 

updated as this

-> added setWorkflow(false) for approval records as well

var ritmGR = new GlideRecord('sysapproval_approver');
ritmGR.addEncodedQuery('state=requested^sysapproval.state=4');
ritmGR.query();

while (ritmGR.next()) {
    // Update approval with workflows disabled
    ritmGR.setWorkflow(false); // Disables notifications/business rules
    ritmGR.autoSysFields(false);
    ritmGR.state = 'rejected';
    ritmGR.update(); // No notifications triggered here

    // Update RITM
    var reqItemGR = new GlideRecord('sc_req_item');
    reqItemGR.addQuery('sys_id', ritmGR.sysapproval);
    reqItemGR.query();

    while (reqItemGR.next()) {
        reqItemGR.setWorkflow(false); // Critical: Disables engines
        reqItemGR.autoSysFields(false);
        reqItemGR.state = 4; // Closed Incomplete
        reqItemGR.stage = 'Closed Incomplete';
        reqItemGR.approval = 'rejected';
        reqItemGR.active = false;
        reqItemGR.work_notes = 'Update by Fix Script';
        reqItemGR.update(); // No notifications triggered here
    }
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

@vivek11 

Hope you are doing good.

Did my reply answer your question?

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

Vasantharajan N
Giga Sage
Giga Sage

@vivek11 

 

Use setWorkflow(false) while updating the Approval records same as RITM records which helps you prevent the system running other BR's and Notification engines.

 

Below line requires the change

 // Set approver state to rejected

ritmGR.state = 'rejected';
ritmGR.setWorkflow(false); //Disable triggering other BR & Notification running 
ritmGR.update();

 

 

 

 


Thanks & Regards,
Vasanth