Send Email Notification to all approver listed in RITM, when the RITM approved for the specific item

AbdulrehmanT
Kilo Guru

Good Afternoon from Pakistan
Guys, I need your help, I have a requirement to send the approval email notification to all approvers listed in the RITM, when the approver approve the RITM, with the information of Requested for

Can anyone please guide me or provide me the solution that will be really helpfull for me
Thanks in advance

1 ACCEPTED SOLUTION

(function executeRule(current, previous) {

// ======================================================
// HARD GUARD – run once per approval
// ======================================================
if (previous.state == 'approved')
return;

if (!current.state.changesTo('approved'))
return;

var ritmId = current.sysapproval;
if (!ritmId)
return;

var ritm = new GlideRecord('sc_req_item');
if (!ritm.get(ritmId))
return;

// Only specific catalog item
if (ritm.cat_item.name != 'Access/Installation for Existing Application/Software')
return;

// ======================================================
// Collect UNIQUE recipients
// ======================================================
var recipients = {};
var recipientList = [];

// 1️⃣ Approver who approved
if (current.approver) {
var approvedBy = current.approver.toString();
recipients[approvedBy] = true;
}

// 2️⃣ Remaining requested approvers
var appr = new GlideRecord('sysapproval_approver');
appr.addQuery('sysapproval', ritmId);
appr.addQuery('state', 'requested');
appr.query();

while (appr.next()) {
if (!appr.approver)
continue;

var approverId = appr.approver.toString();

if (recipients[approverId])
continue;

recipients[approverId] = true;
}

// Convert object → comma-separated list
for (var id in recipients) {
recipientList.push(id);
}

if (recipientList.length === 0)
return;

// ======================================================
// Fire ONE event
// ======================================================
gs.eventQueue(
'auto.approval',
current,
recipientList.join(','), // parm1 → ALL recipients
current.approver.toString()
);

})(current, previous);

View solution in original post

14 REPLIES 14

Thanks for your response

I want to send the email notification to all approver listed in the RITM approval section, when any approver approve the RITM. For this I used this solution............
Can you provide me just help in the script, I will be thankfull to you...
Let me tell you I have created the Notification and Event for it on approval table

Hi @AbdulrehmanT 

First I assume you have created notification and event registry on sysapproval_approver table.

2) run business rule on sysapproval_approver table instead of sc_req_item table so it syncs with u r notification and event

3) Here is business rule based on your question:

Run Business rule After , condition state changes to Approved

 

(function executeRule(current, previous) {

// current = sysapproval_approver
var ritmId = current.sysapproval;
if (!ritmId)
return;

var ritm = new GlideRecord('sc_req_item');
if (!ritm.get(ritmId))
return;

// Condition: only Laptop item
if (ritm.cat_item.name != 'Laptop')
return;

var recipients = [];
var appr = new GlideRecord('sysapproval_approver');
appr.addQuery('sysapproval', ritmId);
appr.query();

while (appr.next()) {
if (appr.approver) {
recipients.push(appr.approver.toString());
}
}

if (recipients.length === 0)
return;

gs.eventQueue(
'auto.approval',
current,
recipients.join(','),
current.approver.toString()
);

})(current, previous);

 

This is sample script please make changes accrding to your requirement.

 

If you find this helpful please mark it helpful

 

Regards,

Mohammed Zakir

Thank you so much for the script.
The script is working good but its sending the approval email to only that approver who approve the RITM
Its not sending the email to all users listed in the approval section of the Ritm.
Can you hel me with it

@AbdulrehmanT 

I think script is fine, did you verify recipient getting populated or not?

Share your event queue and notification configuration each screenshots, without those its tough to know where its going wrong.

Share all screenshots of configuration you did for this use case.

 

Regards,

Mohammed Zakir

(function executeRule(current, previous) {

// ======================================================
// HARD GUARD – run once per approval
// ======================================================
if (previous.state == 'approved')
return;

if (!current.state.changesTo('approved'))
return;

var ritmId = current.sysapproval;
if (!ritmId)
return;

var ritm = new GlideRecord('sc_req_item');
if (!ritm.get(ritmId))
return;

// Only specific catalog item
if (ritm.cat_item.name != 'Access/Installation for Existing Application/Software')
return;

// ======================================================
// Collect UNIQUE recipients
// ======================================================
var recipients = {};
var recipientList = [];

// 1️⃣ Approver who approved
if (current.approver) {
var approvedBy = current.approver.toString();
recipients[approvedBy] = true;
}

// 2️⃣ Remaining requested approvers
var appr = new GlideRecord('sysapproval_approver');
appr.addQuery('sysapproval', ritmId);
appr.addQuery('state', 'requested');
appr.query();

while (appr.next()) {
if (!appr.approver)
continue;

var approverId = appr.approver.toString();

if (recipients[approverId])
continue;

recipients[approverId] = true;
}

// Convert object → comma-separated list
for (var id in recipients) {
recipientList.push(id);
}

if (recipientList.length === 0)
return;

// ======================================================
// Fire ONE event
// ======================================================
gs.eventQueue(
'auto.approval',
current,
recipientList.join(','), // parm1 → ALL recipients
current.approver.toString()
);

})(current, previous);