logo

NJP

How to Display Multi Row Variable set (MRVS) data in a notification

Import · Oct 21, 2020 · article

Today, I am going to share you a code snippet of Mail script which will fetch the submitted MRVS data and send it in a notification in a nice tabular format. It also returns you the display name if any reference columns exist.

Here are the steps todo

1. Create a notification on Requested Item (sc_req_item) table.

2. Inserted or updated. Inserted: True

image

What it contains

Call your mail script like thils

${mail_script:employment_details}

Mail Script

Name: employment_details

Script

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

    // Fetch the MRVS data and display them as table in notification
    template.print("<br>Name: " + current.variables.full_name);
    template.print("<br>Date of Birth: " + current.variables.date_of_birth);
    var mrvs = current.variables.employment_history; // MRVS Internal name
    var rowCount = mrvs.getRowCount();
    if (rowCount >= 1) {
        template.print("<br<b>Employment Details</b>");
        template.print("<table border =1>");
        template.print("<tr>");
        template.print("<th>Company</th>");
        template.print("<th>Designation</th>");
        template.print("<th>Start Date</th>");
        template.print("<th>End Date</th>");
        template.print("<th>Location</th>");
        template.print("</tr>");
        for (var i = 0; i < rowCount; i++) {
            template.print("<tr>");
            var row = mrvs.getRow(i);
            template.print("<td>" + getName(row.company.toString(),'core_company') + "</td>");
            template.print("<td>" + row.designation + "</td>");
            template.print("<td>" + row.start_date + "</td>");
            template.print("<td>" + row.end_date + "</td>");
            template.print("<td>" + getName(row.location.toString(),'cmn_location') + "</td>");     
            template.print("</tr>");
        }
        template.print("</table>");
    }
    //This function accept sys_id and table and returns the Display name.
    function getName(sys_id,tblName) {
            var rec = new GlideRecord(tblName); 
            if(rec.get(sys_id)) {
                return rec.getDisplayValue();
            }
    }
})(current, template, email, email_action, event);

That's it. Now submit your catalog item and test once.

Sample Output

image

Let me know if you have any questions in the comments below.

Related MRVS articles that I Wrote:

Mark the article as helpful and bookmark if you found it useful.

Regards,Asif

2020 ServiceNow Community MVP

View original source

https://www.servicenow.com/community/developer-articles/how-to-display-multi-row-variable-set-mrvs-data-in-a/ta-p/2301010