Use PDIs? Take our 5-minute survey to help shape the PDI roadmap.

Show variable from variable set of catalog item in approval email

Nicholas Hromya
Giga Guru

Hello everyone,

As an approver, I want to see the variable requested_for in my approval email.

This requested_for is in my variable set named SNAR_1

 

I have tried the following without results.  It seems there should be an easier way.  I think the variable set is what I don't understand.

 
var request = current.sysapproval;
 var notificationUtil = new RequestNotificationUtil();
    var requestDetails = notificationUtil.getRequestDetails(request, request, true);
    var showRequestedFor = requestDetails.showRequestedFor;
    var tasks = requestDetails.tasks;
    var totalTasks = requestDetails.totalTasks;


    template.print('<div style="font-size: 15pt; line-height:30px; padding-bottom:16px"><b>About this request</b></div>');

    var priceDisplay = gs.getProperty('glide.sc.price.display');
    var showRequestPrice = false;
    if (requestDetails.totalTasks > 1 && priceDisplay !== 'never' && request.price > 0) {
        showRequestPrice = true;
    }
    var showOpenedBy = false;

    if (request.opened_by.toString() !== request.requested_for.toString()) {
        showOpenedBy = true;
    }
    var requestItemsPadding = 'padding-top:16px;';
    if (showOpenedBy || showRequestedFor || showRequestPrice) {
        template.print('<div style="padding-bottom:16px">');

        if (showRequestedFor) {
            template.print('<div style="' + fontSize + lineHeight + '">Requested for: ' + '<b>' + request.requested_for.name + '</b></div>');
        }
        if (showOpenedBy) {
            template.print('<div style="' + fontSize + lineHeight + '">Opened by: ' + '<b>' + request.opened_by.name + '</b></div>');
        }
        if (showRequestPrice) {
            var recurringPriceText = notificationUtil.gerRecurringPriceRollup(request);
            template.print('<div style="' + fontSize + lineHeight + '">Total price: ' + '<b>' + request.price.getDisplayValue() + '</b><span style="font-size:14px;font-weight: 600">' + recurringPriceText + '</span></div>');
        }
        template.print('</div>');
    } else {
        requestItemsPadding = '';
    }

    if (requestDetails.totalTasks > 1) {
        template.print('<div style="font-size: 12pt;font-weight:600;' + requestItemsPadding + '">Requested items (' + requestDetails.totalTasks + ')</div>');
    }
    tasks.forEach(function(task, index) {
        var borderBottom = 'border-bottom:1px solid #DADDE2';
        if (requestDetails.totalTasks > 1) {
            template.print('<div style="padding-top:16px;padding-bottom:16px;');
        } else {
            template.print('<div style="padding-bottom:16px;');
        }
        if (requestDetails.totalTasks > requestDetails.tasks.length || (index + 1 < requestDetails.tasks.length)) {
            template.print(borderBottom);
        }
        template.print('">');
        template.print('<div style="' + fontSize + lineHeight + '">Requested item number: <b>' + task.requestNumber + '</b></div>');
        template.print('<div style="' + fontSize + lineHeight + '">Short description: <b>' + task.item + '</b></div>');
        if (task.variables.length > 0) {
            notificationUtil.setRequestItemVariablestoTemplate(task, template);
        }
        if (!showRequestedFor) {
            template.print('<div style="' + fontSize + lineHeight + '">Requested for: <b style="font-weight:600">' + task.requestedFor + '</b></div>');
        }
        if (task.showQuantity) {
            template.print('<div style="' + fontSize + lineHeight + '">Quantity: <b>' + task.quantity + '</b></div>');
        }
        notificationUtil.setPricingtoTemplate(task, template);
        template.print('</div>');
    });
    if (totalTasks > 3) {
        template.print('<div style="padding-bottom:16px;padding-top:16px' + fontSize + lineHeight + '"><a style="color:#3C59E7" href="' + requestUrl + '">');
        template.print(gs.getMessage('View all items'));
        template.print('</a></div>');
    }

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

 

8 REPLIES 8

Ankur Bawiskar
Tera Patron

@Nicholas Hromya 

which email is this?

the OOTB approval request email?

also it can be simplified always but if your customer requirement is to have particular font, style etc then if the above script works then keep it

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

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

I should have specified, I am trying to show the RITM #, the requested_for (from the RITM, or however to get it)  and the Item name.  The RITM and the Item name show as a field in the approval notification, but to get the requested_for, I could only find listed above.

 

I should also say, if I could get these three in a email script, that would be ideal, that way the formatting stays the same.

ajmalmuhamm
Tera Contributor

Hi @Nicholas Hromya ,

 

If requested_for is a catalog variable in your variable set (SNAR_1) rather than the standard Requested For field on the RITM, then request.requested_for won't return the value you're looking for.

You should retrieve the variable from the Requested Item (sc_req_item) instead. For example:

var ritm = new GlideRecord('sc_req_item');
if (ritm.get(current.sysapproval)) {
    var requestedFor = ritm.variables.requested_for; // Replace with the actual variable name
    template.print('<div>Requested For: <b>' + requestedFor.getDisplayValue() + '</b></div>');
}

Alternatively, if you're already using RequestNotificationUtil, you can inspect the task.variables collection that is populated by notificationUtil.setRequestItemVariablestoTemplate(task, template). If your variable set is included on the catalog item, the variable should already be available there.

The fact that the variable belongs to a variable set doesn't change how you access it—what matters is the variable name. As long as the variable is associated with the RITM, you can access it via ritm.variables.<variable_name>.