Mail Script Notification

Ricardo Sanchez
Tera Contributor

I created a Mail Script to grab the variables to add them to the email notification, it looks like its working perfectly but I notice that Multiple Variable Rows arent being pull into the notification

(function runMailScript(current, template, email, email_action, event) {

    // Set email subject
    email.setSubject('Approval Required: ' + current.cat_item.getDisplayValue() + ' - ' + current.number);

    // Query the sysapproval_approver table to get the approval sys_id
    var approvalSysId = '';
    var approvalGR = new GlideRecord('sysapproval_approver');
    approvalGR.addQuery('document_id', current.sys_id);
    approvalGR.query();
    if (approvalGR.next()) {
        approvalSysId = approvalGR.sys_id.toString();
    }

    // Variable Summary section
    template.print('<h3 style="margin-top:20px; color: #003366;">Variable Summary</h3>');
    template.print('<div style="margin-bottom:20px;">');

    // Retrieve the associated RITM record
    var ritm = new GlideRecord('sc_req_item');
    ritm.get(current.sys_id);

    var variables = ritm.variables.getElements();
    for (var i = 0; i < variables.length; i++) {
        var question = variables[i].getQuestion();
        var label = question.getLabel();
        var value = question.getDisplayValue();

        if (label && value && value.toLowerCase() !== 'false') {
            template.print('<p style="margin: 4px 0;"><strong>' + label + ':</strong> ' + value + '</p>');
        }
    }
    template.print('</div>');

    // Manager Acknowledgment section
    template.print('<div style="border: 1px solid #ccc; padding: 15px; background-color: #f9f9f9; border-radius: 5px; margin-bottom: 20px;">');
    template.print('<p><strong>As the Manager/PM, your approval of this request implies that you:</strong></p>');
    template.print('<ul style="padding-left: 20px;">');
    template.print('<li>Certify that the justification provided is valid, applies to the user\'s assigned tasking, and is required to fulfill the contract.</li>');
    template.print('<li>Agree to enforce compliance with the terms of this agreement. Failure to comply with these requirements may result in the loss or suspension of privileges.</li>');
    template.print('</ul>');
    template.print('</div>');

    // Approve/Reject links
    template.print('<div style="margin-top: 20px;">');
    template.print('<p style="font-weight:bold;">To take action:</p>');
    template.print('<p>${mailto:mailto.approval}</p>');
    template.print('<hr style="border-top: 1px solid #ccc;"/>');
    template.print('<p>${mailto:mailto.rejection}</p>');
    template.print('</div>');

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

MihirN
Tera Contributor

This is actually expected: list collectors store multiple values in a GlideList format, and unlike simple string, choice, or reference variables, they need special handling in mail scripts. List collectors store multiple sys_ids, so getDisplayValue() often returns blank or just the sys_id list.

You need to:

  1. Detect if the variable is a list collector.

  2. Query the table it references.

  3. Loop through the selected values and build the display string


    The code should look something like this :

    var variables = ritm.variables.getElements();
    for (var i = 0; i < variables.length; i++) {
    var v = variables[i];
    var question = v.getQuestion();
    var label = question.getLabel();
    var type = question.getType();
    var value = v.getValue();

    if (type == 19 && value) { // 19 = List Collector
    var listValues = [];
    var targetTable = question.getReference();
    var grList = new GlideRecord(targetTable);
    grList.addQuery('sys_id', 'IN', value);
    grList.query();
    while (grList.next()) {
    listValues.push(grList.getDisplayValue());
    }
    if (listValues.length > 0) {
    template.print('<p style="margin: 4px 0;"><strong>' + label + ':</strong> ' + listValues.join(', ') + '</p>');
    }
    } else {
    var displayValue = v.getDisplayValue();
    if (label && displayValue && displayValue.toLowerCase() !== 'false') {
    template.print('<p style="margin: 4px 0;"><strong>' + label + ':</strong> ' + displayValue + '</p>');
    }
    }
    }

    I hope this works for you.
    *************************************************************************************************************
    If my response proves useful, please indicate its helpfulness by selecting " Accept as Solution" and " Helpful." This action benefits both the community and me.

@MihirN  Sorry the issue is with MVR's not list collectors