Adding values from a multi row variable set to an email notification

Bidduam
Tera Guru

I am trying to add the values from a multi row variable set and I'm just not having any luck.

 

I've had a look at these:

The one I've tried is:

How to Display Multi Row Variable set (MRVS) data... - ServiceNow Community

 

My MRVS is as follows:

Internal Name: other_items

Type: Multi Row

Layout: 1 Column Wide

I only have one variable within the MRVS

Name: other_options 

Type: Single Line Text

 

My current mail script is:

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


    // This sets the font size and type

    var mrvs = current.variables.other_items; // MRVS Internal name
    var rowCount = mrvs.getRowCount();
    if (rowCount >= 1) {
        template.print("<table border =1>");
        template.print("<tr>");
        template.print("<th>Others</th>");
        template.print("</tr>");
        for (var i = 0; i < rowCount; i++) {
            template.print("<tr>");
            var row = mrvs.getRow(i);
            template.print("<td>" + getName(row.others.toString(),'current.variables.other_options') + "</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);

 

All it is returning is the table border and heading, no variable values

 

What am I missing.

10 REPLIES 10

"Creating a summary"

 

Thats the part you need. With that you get all details of the MRVS. Ofcourse you need to format it yourself.

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

Sohithanjan G
Kilo Sage
Kilo Sage

Hi @Bidduam ,

 

In this modified version, I've removed the getName() function as it seems unnecessary for accessing the value of the other_options field. Instead, I directly retrieve the value using row.getValue('other_options'). Make sure 'other_options' is indeed the correct field name within each row of your MRVS.

 

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

    var mrvs = current.variables.other_items; // MRVS Internal name
    var rowCount = mrvs.getRowCount();
    
    if (rowCount >= 1) {
        template.print("<table border='1'>");
        template.print("<tr>");
        template.print("<th>Others</th>");
        template.print("</tr>");
        
        for (var i = 0; i < rowCount; i++) {
            template.print("<tr>");
            var row = mrvs.getRow(i);
            var otherOptionValue = row.getValue('other_options'); // Assuming 'other_options' is the correct field name
            template.print("<td>" + otherOptionValue + "</td>");
            template.print("</tr>");
        }
        
        template.print("</table>");
    }

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

 

HIT Helpful & Accept the Solution if it suffices !!

Please mark as Accepted Solution if this solves your query and HIT Helpful if you find my answer helped you. This will help other community mates too..:)

@Sohithanjan G 

 

This is very very close thank you, so now it is showing that is picking up how many rows have values in it. However for some reason it isn't picking up the actual value.

Bidduam_0-1708553790065.png

The name is definitely correct:

Bidduam_1-1708553833660.png

 

It doesn't appear to like the way the value of the variable set variable is being extracted, as I tried a new record with more values added and it picked up the extra lines, but still showed "undefined"

I then added text on either side of the variable value and it shows that without issue, just not getting the variable values.

 

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

    var mrvs = current.variables.other_items; // MRVS Internal name
    var rowCount = mrvs.getRowCount();
    
    if (rowCount >= 1) {
        template.print("<table border='0'>");
        template.print("<tr>");
        template.print("<th>Others</th>");
        template.print("</tr>");
        
        for (var i = 0; i < rowCount; i++) {
            template.print("<tr>");
            var row = mrvs.getRow(i);
			var otherOptionValue = row.getValue('other_options'); 
            template.print("<td>" + "This " + otherOptionValue + " That" + "</td>");
            template.print("</tr>");
        }
        
        template.print("</table>");
    }

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

 

 

Bidduam_0-1708563932209.png

 

@Sohithanjan G any ideas?

Ed Hefford
Tera Guru

This is my example including MRVS form sc_req_item table, which I've used for some time now.

Create a mailscript to include in your email notification. 

(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

    var lineManager = getUserDetails(event.parm2);
    email.addAddress("cc", lineManager.email, lineManager.name);

    var mrvs = current.variables.public_holiday_swap; // MRVS Internal name

	var origDate = '';
	var newDate = '';

    var rowCount = mrvs.getRowCount();
    if (rowCount >= 1) {
        template.print("<b>Substitution Details</b><br /><br />");
        template.print('<table style="border: 1px solid #ddd; padding: 8px;">');
        template.print("<tr style='padding-top: 12px; padding-bottom: 12px; text-align: left; background-color: #fff; color: #000;'>");
        template.print("<th>Substitute from date</th>");
        template.print("<th>Substitute to date</th>");
        template.print("</tr>");
        for (var i = 0; i < rowCount; i++) {
            template.print("<tr style='padding: 8px; border: 1px solid #ddd;'>");
            var row = mrvs.getRow(i);
			origDate = formatDate(current.variables.public_holiday_swap[i].public_holiday_date);
			newDate = formatDate(current.variables.public_holiday_swap[i].new_date);
			gs.log('JAMES DEBUG: '+current.number+' - '+origDate+' - '+newDate);
            template.print("<td>" + origDate + "</td>");
            template.print("<td>" + newDate + "</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();
        }
    }

    function getUserDetails(sysId) {

        var userDetails = {};

        var grUser = new GlideRecord('sys_user');
        grUser.addQuery('sys_id', sysId);
        grUser.addActiveQuery();
        grUser.query();

        if (grUser.next()) {
            userDetails.name = grUser.name;
            userDetails.first_name = grUser.first_name;
            userDetails.last_name = grUser.last_name;
            userDetails.email = grUser.email;
        }
        return userDetails;
    }

    function formatDate(origDate) {

		var gdt = new GlideDateTime(origDate+" 00:00:00");

        var day = gdt.getDayOfMonthLocalTime();
        var month = gdt.getMonthLocalTime();
        var year = gdt.getYearLocalTime();

        day = zeroPad(day);
        month = zeroPad(month);

        return day + '-' + month + '-' + year;
    }

    // Function to zero-pad single-digit numbers
    function zeroPad(number) {
        return (number < 10 ? '0' : '') + number;
    }
})(current, template, email, email_action, event);

 

My script will include the headers, variables, user information and do other things like CC in the submitters line manager.


Output is like this:

EdHefford_0-1708555585889.png

 




Hope this helps!