How to send an email to CC people through email script

sasipriya
Tera Contributor
Hi All,
 I have requirement that I need to send an email to approver managers and approvers in the notification .The below is the exact requirement 

As an Approver's Manager, I would like 1 approval escalation email to be sent on MOnday for all approvers on my team who have approvals that have aged more than 7 days

  •  1 email per manager of with pending approvals > 7 days for their team
  • Table in email with details/link
  • Post to the activity stream that Approval Escalation Email was sent

 

Email Template:

To: Manager 

CC: Approvers

Subject: ServiceNow Pending Approvals over 7 Days

Message: 


Dear [Manager Name],

 

The following approvals have been open for more than 7 days. Please work with the approvers on your team to action as soon as possible. Thank you!

[Table with Approver Name, RITM # & Link, Requested For, Short Description, Approval Created Date, Calculated Age Field]

 

I written email script like below everything is working fine except CC part.- Please help me on this 

 
(function runMailScript( /* GlideRecord */ current, /* TemplatePrinter */ template,
    /* Optional EmailOutbound */
    email, /* Optional GlideRecord */ email_action,
    /* Optional GlideRecord */
    event) {
    // Add your code here
    template.print('Dear '+ event.parm2+',');
    template.print('The following approvals have been open for more than 7 days. Please work with the approvers on your team to action as soon as possible.');
    var approvalGR = new GlideRecord('sysapproval_approver');
    approvalGR.addEncodedQuery('sysapprovalSTARTSWITHritm^state=requested^sys_created_onRELATIVELT@dayofweek@ago@7');
    approvalGR.query();
    template.print('<table border="1px solid black">');
    template.print( "<tr bgcolor='#ddd' align='center'>" );
    //template.print("<td style='text-align:center' colspan='3'><strong>Heading</strong></td>");
    template.print( "</tr>" );
    template.print( "<tr>" );
    template.print( "<td><left><b>" + "Approver name" + "</b></left></td>" );
    template.print( "<td><left><b>"+" RITM"+" </b></left></td>" );
    template.print( "<td><left><b>"+"Requested For"+"</b></left></td>" );
    template.print( "<td><left><b>"+"Short Description"+"</b></left></td>" );
    template.print( "<td><left><b>"+"Approval Created Date"+"</b></left></td>" );
    template.print( "<td><left><b>"+"Approval Age"+"</b></left></td>" );
    template.print( "</tr>" );
    var cc_users;
    while (approvalGR.next()) {
        if (approvalGR.approver.manager == event.parm1) {
            template.print( "<tr>" );
            template.print( "<td><left>" + approvalGR.approver.name+ "</left></td>" );
            var portalSuffix = new sn_ex_emp_fd.FoundationNotificationUtil().getPortalSuffix();
            var link =gs.getProperty('glide.servlet.uri')+ portalSuffix + '?id=ticket&table=sc_req_item&sys_id=' + approvalGR.document_id;
            template.print( "<td><left>"+'<a href="'+link+'"> '+approvalGR.sysapproval.number+' </a>'+"</left></td>" );
            template.print( "<td><left>"+approvalGR.document_id.requested_for.name+"</left></td>" );
            template.print( "<td><left>"+approvalGR.document_id.short_description +"</left></td>" );
            template.print( "<td><left>"+approvalGR.sys_created_on+"</left></td>" );
            var gdt = new GlideDateTime();
            var currDate = gdt.getLocalDate(); 
            var gdt2 = new GlideDateTime(approvalGR.sys_created_on);
            var dateDiff = GlideDateTime.subtract(gdt2,currDate);
            var daysDiff = Math.round((dateDiff.getNumericValue()) / 24 / 60 / 60 / 1000);
            template.print( "<td><left>"+daysDiff+"</left></td>" );
            template.print( "</tr>" );
            cc_users.push(approvalGR.approver);
        }
    }
    template.print( "</tr>" );
    template.print('</table>'); 

})(current, template, email, email_action, event);
1 REPLY 1

Tushar
Kilo Sage
Kilo Sage

Hi @sasipriya 

 

Please try below code and let me know -

(function runMailScript( /* GlideRecord */ current, /* TemplatePrinter */ template,
    /* Optional EmailOutbound */ email, /* Optional GlideRecord */ email_action,
    /* Optional GlideRecord */ event) {

    // Initialize the cc_users array as an empty array
    var cc_users = [];

    template.print('Dear ' + event.parm2 + ',');
    template.print('The following approvals have been open for more than 7 days. Please work with the approvers on your team to action as soon as possible.');

    var approvalGR = new GlideRecord('sysapproval_approver');
    approvalGR.addEncodedQuery('sysapprovalSTARTSWITHritm^state=requested^sys_created_onRELATIVELT@dayofweek@ago@7');
    approvalGR.query();

    template.print('<table border="1px solid black">');
    template.print("<tr bgcolor='#ddd' align='center'>");
    template.print("</tr>");
    template.print("<tr>");
    template.print("<td><left><b>" + "Approver name" + "</b></left></td>");
    template.print("<td><left><b>" + " RITM" + " </b></left></td>");
    template.print("<td><left><b>" + "Requested For" + "</b></left></td>");
    template.print("<td><left><b>" + "Short Description" + "</b></left></td>");
    template.print("<td><left><b>" + "Approval Created Date" + "</b></left></td>");
    template.print("<td><left><b>" + "Approval Age" + "</b></left></td>");
    template.print("</tr>");

    while (approvalGR.next()) {
        if (approvalGR.approver.manager == event.parm1) {
            // ... Your existing code ...

            // Push the approver into the cc_users array
            cc_users.push(approvalGR.approver);
        }
    }

    // Join the cc_users array into a comma-separated string
    var ccEmails = cc_users.map(function (user) {
        return user.email.toString();
    }).join(',');

    // Add CC recipients to the email
    email.addCC(ccEmails);

    template.print("</tr>");
    template.print('</table>');
})(current, template, email, email_action, event);

 

Please, don't forget to mark my answer as correct if it solves your issue or mark it as helpful if it is relevant for you!

Regards,
Tushar