Add the 'requested_for' variable as a CC to the Approval Request notification

rachelconstanti
Mega Sage

I have been tasked with adding the 'requested_for' variable as a CC to the approval request notification.  This notification uses email template 'change.itil.approve.role'.

I have created an email script 'cc_requested_for' with this code:

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

// Add your code here
current.tableName() == 'sc_req_item';
email.addAddress("cc", current.getValue('requested_for'), current.getValue('name'));

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

 

This has been added to the email template 'change.itil.approve.role' however the requested for is not being added to the email notification.

 

How do I fix this?

Thank you,

Rachel

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@rachelconstanti 

do this if you are talking about requested_for variable and not field on RITM

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

    // Add your code here
    if (current.source_table == 'sc_req_item') {
        var gr = new GlideRecord("sc_req_item");
        gr.addQuery("sys_id", current.sysapproval);
        gr.query();
        if (gr.next()) {
            email.addAddress("cc", gr.variables.requested_for.email.toString(), gr.variables.requested_for.name.toString());
        }
    }

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

if you are talking about requested_for field on RITM then use this

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

    // Add your code here
    if (current.source_table == 'sc_req_item') {
        var gr = new GlideRecord("sc_req_item");
        gr.addQuery("sys_id", current.sysapproval);
        gr.query();
        if (gr.next()) {
            email.addAddress("cc", gr.requested_for.email.toString(), gr.requested_for.name.toString());
        }
    }

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

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

View solution in original post

7 REPLIES 7

JenniferRah
Mega Sage

You have to get the actual records, I believe. Your code should look more like this: 

var gr = new GlideRecord('sc_req_item');
gr.get(current.getUniqueValue());
var user = new GlideRecord('sys_user');
user.get(gr.request.requested_for);
email.addAddress("cc", user.getValue('email').toString(), user.getValue('name').toString());

 

Thank you - this added the requested for to the email but in the 'to' line and not 'cc'.

That's odd. Make sure the  email.addAddress line says "cc" and the quotes didn't do something weird if you copied and pasted them. It specifically says cc, so it shouldn't have added it to the to list.

Ankur Bawiskar
Tera Patron
Tera Patron

@rachelconstanti 

do this if you are talking about requested_for variable and not field on RITM

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

    // Add your code here
    if (current.source_table == 'sc_req_item') {
        var gr = new GlideRecord("sc_req_item");
        gr.addQuery("sys_id", current.sysapproval);
        gr.query();
        if (gr.next()) {
            email.addAddress("cc", gr.variables.requested_for.email.toString(), gr.variables.requested_for.name.toString());
        }
    }

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

if you are talking about requested_for field on RITM then use this

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

    // Add your code here
    if (current.source_table == 'sc_req_item') {
        var gr = new GlideRecord("sc_req_item");
        gr.addQuery("sys_id", current.sysapproval);
        gr.query();
        if (gr.next()) {
            email.addAddress("cc", gr.requested_for.email.toString(), gr.requested_for.name.toString());
        }
    }

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

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