Built something you're proud of? Tell the story. A quick G2 review of App Engine or Build Agent helps other developers see what's possible on ServiceNow. Share your experience.

Email notification script help (Reqeust Complete)

ElizabethG22728
Tera Contributor

I'm trying to edit an email script (that is referenced by "Request Complete" email notification) to display the RITM short description.
The script it is referencing task.item and I think it should be referencing task.short_description (sc_request table extends to the task table). It doesn't find a value when referencing task.short_description. Just returns "undefined." I also tried sq_reqeust.task.short_description (which I didn't think would work but I'm stuck).

I was unable to copy the script so I created a new one and pasted the code from original script. This new script has as new name and is being referenced in my notification. How can I get the script to reference and display the RITM short description?

Origanl code:

        template.print('">');
        template.print('Requested item number: <b style="font-weight:600">' + task.requestNumber + '</b><br/>' + 'Short description: <b style="font-weight:600">' + task.item + '</b><br/>');
        template.print('</div>');


Modified code: 

        template.print('">');
        template.print('Requested item number: <b style="font-weight:600">' + task.requestNumber + '</b><br/>' + 'Request: <b style="font-weight:600">' + task.item + '</b><br/>' + 'Short description: <b style="font-weight:600">' + task.short_description + '</b><br/>');
        template.print('</div>');
 
Coding is not my skillset but I can read it for the most part and make minor tweaks. I'm stuck here.
FYI the script I'm working with is About_The_Request_Req_Survey_Req_Completed
Much Thanks!
1 REPLY 1

Vishal Jaswal
Giga Sage

Hello @ElizabethG22728 

Here is the Updated Email script "About_The_Request_Req_Survey_Req_Completed"  - Please find the updated code in between comments //Modified here: Starts and //Modified here: Ends



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

    var portalSuffix = new sn_ex_emp_fd.FoundationNotificationUtil().getPortalSuffix();
    var viewAllUrl = '/' + portalSuffix + '?id=order_status&table=sc_request&sys_id=' + current.sys_id;
    var requestUrl = '';
    var buttonText = '';
    if (email_action.sys_name.toString() === 'Request survey') {
        requestUrl = event.parm2;
        buttonText = gs.getMessage('Take the survey');
    } else {
        requestUrl = viewAllUrl;
        buttonText = gs.getMessage('View request');
    }

    //Generates primary action of view request
    var requestNotificationJs = new global.RequestNotificationUtil();
    requestNotificationJs.createNotificationPrimayAction(template, requestUrl, buttonText);
    if (email_action.sys_name.toString() === 'Request survey') {
        template.print('<div style="font-size: 15pt; line-height: 30px; padding-bottom: 16px;"><span style="font-weight:600">About your recent request</span></div>');
    } else {
        template.print('<div style="font-size: 15pt; line-height: 30px; padding-bottom: 16px;"><span style="font-weight:600">About this request</span></div>');
    }

    //request details required for notification template
    var requestDetails = requestNotificationJs.getRequestDetails(current.sys_id, current);
    var requestItemPadding = '';
    if (email_action.sys_name.toString() === 'Request completed' && !current.close_notes.nil()) {
        var comments = current.close_notes.toString();
        comments = comments.replace(/\n/g, '<br/>');
        template.print('<div> Completed by : ' + '<b style="font-weight:600">' + current.closed_by.name + '</b></div>');
        template.print('<div> Completion notes : ' + '<b style="font-weight:600">' + comments + '</b></div>');
        requestItemPadding = 'padding-top:16px;';
    }

    if (email_action.sys_name.toString() === 'Request approved') {
        var comment = requestNotificationJs.getRequestComment(current.sys_id, 'approved');
        if (comment) {
            template.print('<div> Approval notes : ' + '<b style="font-weight:600">' + comment + '</b></div>');
            requestItemPadding = 'padding-top:16px;';
        }
    }
    if (requestDetails.totalTasks > 1) {
        template.print('<div style="font-size: 12pt;font-weight:600;' + requestItemPadding + '">Requested items (' + requestDetails.totalTasks + ')</div>');
        requestItemPadding = 'padding-top:16px;';
    }
    requestDetails.tasks.forEach(function(task, index) {
        var borderBottom = 'border-bottom:1px solid #DADDE2';
        template.print('<div style="padding-bottom:16px;' + requestItemPadding);
        if (requestDetails.totalTasks > requestDetails.tasks.length || (index + 1 < requestDetails.tasks.length)) {
            template.print(borderBottom);
        }

        //Modified here: Starts
        var ritmShortDesc = '';

        if (task.requestNumber) {
            var ritmGR = new GlideRecord('sc_req_item');
            ritmGR.addQuery('number', task.requestNumber);
            ritmGR.setLimit(1);
            ritmGR.query();
            if (ritmGR.next()) {
                ritmShortDesc = ritmGR.short_description.getDisplayValue();
            }
        }


        template.print('">');
        template.print('Requested item number: <b style="font-weight:600">' + task.requestNumber + '</b><br/>' + 'RITM Short description: <b style="font-weight:600">' + ritmShortDesc + '</b><br/>' + 'Request Short description: <b style="font-weight:600">' + current.short_description.getDisplayValue() + '</b><br/>');
        template.print('</div>');
    });
    //Modified here: Ends

    if (requestDetails.totalTasks > 3) {
        template.print('<div style="padding-bottom:18px;padding-top:16px;color:#3C59E7"><a href="' + viewAllUrl + '">');
        template.print(gs.getMessage('View all items'));
        template.print('</a></div>');
    }
})(current, template, email, email_action, event);



  template.print('">');
        template.print('Requested item number: <b style="font-weight:600">' + task.requestNumber + '</b><br/>' + 'RITM Short description: <b style="font-weight:600">' + task.item + '</b><br/>'  + 'Request Short description: <b style="font-weight:600">' + current.short_description.getDisplayValue() + '</b><br/>');
        template.print('</div>');

VishalJaswal_0-1777652368961.png

 



Validation Results:

VishalJaswal_2-1777651950093.png

 

 


Hope that helps!