Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Email Script to include Variables, Variable Sets, and their values

MyriahM
Tera Contributor

Hello,
Our current script [shown below], created by our implementation partners, is pulling the sys_id instead of the display name for the reference fields within the variables [Approver, Requested for] in our email script. We are reaching out to see if someone might have a fix for this. Also, on a side note, we are looking to include variable sets as well. Any help and/or guidance would be much appreciated.

Script:

(function runMailScript(current, template, email, email_action, event) {
    var html = '<h2>Summary of Requested Item</h2>';

    var reqItem = new GlideRecord("sc_req_item");
    reqItem.addQuery("sys_id", current.sysapproval);
    reqItem.query();

    if (reqItem.next()) {
        html += '<h3>Request Details:</h3>';
        html += '<table border="1" cellpadding="5" style="border-collapse: collapse;">';
        html += '<tr><td><strong>Request Item:</strong></td><td>' + reqItem.number + '</td></tr>';
        html += '<tr><td><strong>Requested for:</strong></td><td>' + reqItem.requested_for.getDisplayValue() + '</td></tr>';
        html += '<tr><td><strong>Item:</strong></td><td>' + reqItem.cat_item.getDisplayValue() + '</td></tr>';
        html += '<tr><td><strong>Quantity:</strong></td><td>' + reqItem.quantity + '</td></tr>';
        html += '</table>';

        html += '<h3>Variables:</h3>';
        html += '<table border="1" cellpadding="1" style="border-collapse: collapse;">';

        var variables = new GlideRecord("sc_item_option_mtom");
        variables.addQuery("request_item", reqItem.sys_id);
        variables.orderBy("sc_item_option.item_option_new.order");
        variables.query();

        while (variables.next()) {
            var itemOption = variables.sc_item_option.item_option_new;
            var label = itemOption.getDisplayValue();
            var value = variables.sc_item_option.value;

            // Handle reference fields - FIX APPLIED HERE
            if (itemOption.type == 'reference' && value) {
                var refTable = itemOption.reference;
                if (refTable) {
                    var refGR = new GlideRecord(refTable);
                    if (refGR.get(value)) {
                        value = refGR.getDisplayValue(); // Always get the display value
                    }
                }
            }

            // Handle checkbox display
            if (itemOption.type == 'checkbox') {
                value = (value == 'true') ? 'Yes' : 'No';
            }

            if (value && value.trim() !== '') {
                html += '<tr><td><strong>' + label + ':</strong></td><td>' + value + '</td></tr>';
            }
        }

        html += '</table>';
    }

    template.print(html);

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

1 ACCEPTED SOLUTION

Hi @MyriahM ,

 

(function runMailScript(current, template, email, email_action, event) {
    var html = '<h2>Summary of Requested Item</h2>';

    var reqItem = new GlideRecord("sc_req_item");
    reqItem.addQuery("sys_id", current.sysapproval);
    reqItem.query();

    if (reqItem.next()) {
        html += '<h3>Request Details:</h3>';
        html += '<table border="1" cellpadding="5" style="border-collapse: collapse;">';
        html += '<tr><td><strong>Request Item:</strong></td><td>' + reqItem.number + '</td></tr>';
        html += '<tr><td><strong>Requested for:</strong></td><td>' + reqItem.requested_for.getDisplayValue() + '</td></tr>';
        html += '<tr><td><strong>Item:</strong></td><td>' + reqItem.cat_item.getDisplayValue() + '</td></tr>';
        html += '<tr><td><strong>Quantity:</strong></td><td>' + reqItem.quantity + '</td></tr>';
        html += '</table>';

        html += '<h3>Variables:</h3>';
        html += '<table border="1" cellpadding="1" style="border-collapse: collapse;">';

        var variables = new GlideRecord("sc_item_option_mtom");
        variables.addQuery("request_item", reqItem.sys_id);
        variables.orderBy("sc_item_option.item_option_new.order");
        variables.query();

        while (variables.next()) {
            var itemOption = variables.sc_item_option.item_option_new;
            var label = itemOption.getDisplayValue();
            var value = variables.sc_item_option.value;

            // Handle reference fields - FIX APPLIED HERE
            if ((itemOption.type == 'reference' || itemOption.type == '8') && value) {
                var refTable = itemOption.reference;
                if (refTable) {
                    var refGR = new GlideRecord(refTable);
                    if (refGR.get(value)) {
                        value = refGR.getDisplayValue(); // Always get the display value
                    }
                }
            }

            // Handle checkbox display
            if (itemOption.type == 'checkbox') {
                value = (value == 'true') ? 'Yes' : 'No';
            }

            if (value && value.trim() !== '') {
                html += '<tr><td><strong>' + label + ':</strong></td><td>' + value + '</td></tr>';
            }
        }

        html += '</table>';
    }

    template.print(html);
})(current, template, email, email_action, event);
(function runMailScript(current, template, email, email_action, event) {
    var html = '<h2>Summary of Requested Item</h2>';

    var reqItem = new GlideRecord("sc_req_item");
    reqItem.addQuery("sys_id", current.sysapproval);
    reqItem.query();

    if (reqItem.next()) {
        html += '<h3>Request Details:</h3>';
        html += '<table border="1" cellpadding="5" style="border-collapse: collapse;">';
        html += '<tr><td><strong>Request Item:</strong></td><td>' + reqItem.number + '</td></tr>';
        html += '<tr><td><strong>Requested for:</strong></td><td>' + reqItem.requested_for.getDisplayValue() + '</td></tr>';
        html += '<tr><td><strong>Item:</strong></td><td>' + reqItem.cat_item.getDisplayValue() + '</td></tr>';
        html += '<tr><td><strong>Quantity:</strong></td><td>' + reqItem.quantity + '</td></tr>';
        html += '</table>';

        html += '<h3>Variables:</h3>';
        html += '<table border="1" cellpadding="1" style="border-collapse: collapse;">';

        var variables = new GlideRecord("sc_item_option_mtom");
        variables.addQuery("request_item", reqItem.sys_id);
        variables.orderBy("sc_item_option.item_option_new.order");
        variables.query();

        while (variables.next()) {
            var itemOption = variables.sc_item_option.item_option_new;
            var label = itemOption.getDisplayValue();
            var value = variables.sc_item_option.value;
            var itemOptionType = itemOption.type;
            // Handle reference fields - FIX APPLIED HERE
            if ((itemOptionType == '31' || itemOptionType == '8') && value) {
                var refTable = itemOption.reference;
                if (refTable) {
                    var refGR = new GlideRecord(refTable);
                    if (refGR.get(value)) {
                        value = refGR.getDisplayValue(); // Always get the display value
                    }
                }
            }

            // Handle checkbox display
            if (itemOption.type == 'checkbox') {
                value = (value == 'true') ? 'Yes' : 'No';
            }

            if (value && value.trim() !== '') {
                html += '<tr><td><strong>' + label + ':</strong></td><td>' + value + '</td></tr>';
            }
        }

        html += '</table>';
    }

    template.print(html);
})(current, template, email, email_action, event);

 

try this

 

 

Please mark my answer as helpful/correct if it resolves your query.

Regards,
Chaitanya

 

View solution in original post

5 REPLIES 5

Hi @MyriahM 

 

Sure

var itemOptionType = itemOption.type;

            // Handle reference fields - FIX APPLIED HERE

            if ((itemOptionType == '31' || itemOptionType == '8') && value) {

 

I have updated the if condition with backend values of variable types

In the backend for reference it 8 and for requested for its 31

 

If you go to any variables and check the choices of Type field the backend values are different 

You can  go to the sys_choce table to find the background values or if you have SNUtis with show technical names feature it shows the backend values of chices

 

 

Regards 

Chaitanya