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.

Display Multi row variable set columns in notification

Chenab Khanna
Tera Expert

Hi

I have a multi row variable set and there is data in the columns. Does anyone have any idea how can i display that table in notification?

I am using email script wherein i am calling the variables via GlideAppVariablePoolQuestionSet, but unable to get the multi row variable?

Is there any way possible?

or may be some columns in it?

1 ACCEPTED SOLUTION

@Chenab Khanna 

that's correct.

If you are using any reference variable inside MRVS you will get sys_id and not the display value

you will have to query the reference table with that sys_id and get the display value.

Something like this

Updated in bold is the change

(function runMailScript( /* GlideRecord */ current, /* TemplatePrinter */ template,
                                            /* Optional EmailOutbound */
                                            email, /* Optional GlideRecord */ email_action,
                                            /* Optional GlideRecord */
                                            event) {
    var req_id = current.sysapproval;
    template.print('<p><span style="font-family: arial, helvetica, sans-serif; font-size: 10pt;"><strong>' + req_id.number + '</strong>');
    template.print(' : ' + req_id.short_description + ' <br /> <br />');

    //print variables
    var set = new GlideappVariablePoolQuestionSet();
    set.setRequestID(req_id);
    set.load();
    var vs = set.getFlatQuestions();

    if (vs.size() > 0) {
        template.print('<strong>Request Item Variables:</strong><br /><br />');
        for (var i = 0; i < vs.size(); i++) {
            var show_line = 'true';
            if (vs.get(i).getLabel() == 'Line Manager Approval required?' || vs.get(i).getLabel() == 'Line Manager') {
                show_line = 'false';
            }

            if (vs.get(i).getLabel() == 'AD Action') {
                var ritmGR = new GlideRecord('sc_req_item');
                if (ritmGR.get(req_id)) {
                    gs.log("Entered into the IF conditon of AD Action");

                    template.print('<table>');
                    template.print('<th>Name</th><th>Email</th><th>Department</th>');
                    var mrvs = ritmGR.variables.ad_management_multirow; //check and put correct name
                    var rowCount = mrvs.getRowCount();
                    gs.log("print mrvs " + mrvs);
                    gs.log("Row count of MRVS length " + rowCount);

                    for (var s = 0; s < rowCount; s++) {
                        var row = mvrs.getRow(s);

                        var displayDepartmentValue = getActualValue('cmn_department', row.user_department);

                        template.print("<tr>");
                        template.print("<td><center>" + row.user_name + "</center></td>"); //check fieldname
                        template.print("<td><left>" + row.user_email + "</left></td>"); //check fieldname
                        template.print("<td><left>" + displayDepartmentValue + "</left></td>"); //check fieldname
                        template.print("</tr>");
                    }

                    template.print('</table>');
                }
            }
            if (vs.get(i).getLabel() != '' && vs.get(i).getLabel() != null && show_line == 'true' && vs.get(i).getDisplayValue() != '' && vs.get(i).getLabel() != 'AD Action') {
                template.space(4);
                template.print('<strong>' + vs.get(i).getLabel() + "</strong>: " + vs.get(i).getDisplayValue() + "<br />");

            }
        }
    }

    function getActualValue(table, value){

        var gr = new GlideRecord(table);
        gr.addQuery("sys_id", value);
        gr.query();
        if (gr.next()) {
            return gr.getDisplayValue();
        }        
    }

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

Regards
Ankur

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

View solution in original post

21 REPLIES 21

Hi

In your mail script, you can access all variables from MRVS like this and dispaly in a table. Check and change the field names correctly.

var mrvs = current.variables.MRVS_name;//check and put correct name
var rowCount = mrvs.getRowCount();
template.print("<table>");
for (var i = 0; i < rowCount; i++) {
        template.print("<tr>");
	var row = mrvs.getRow(i);
        template.print("<td>"+row.variable1+"</td>");//check fieldname
        template.print(" ");
        template.print("<td>"+row.variable2+"</td>");//check fieldname
        template.print(" ");
        template.print("<td>"+row.variable3+"</td>");//check fieldname
        template.print("</tr>");
}
template.print("</table>");

Kindly mark the comment as a correct answer and also helpful once worked.

Thanks for the reply but it did not work. Nothing is displayed in the notification.

Please find the email script below - 

(function runMailScript( /* GlideRecord */ current, /* TemplatePrinter */ template,
/* Optional EmailOutbound */
email, /* Optional GlideRecord */ email_action,
/* Optional GlideRecord */
event) {
var req_id = current.sysapproval;
template.print('<p><span style="font-family: arial, helvetica, sans-serif; font-size: 10pt;"><strong>' + req_id.number + '</strong>');
template.print(' : ' + req_id.short_description + ' <br /> <br />');

//print variables
var set = new GlideappVariablePoolQuestionSet();
set.setRequestID(req_id);
set.load();
var vs = set.getFlatQuestions();
if (vs.size() > 0) {
template.print('<strong>Request Item Variables:</strong><br /><br />');
for (var i = 0; i < vs.size(); i++) {
var show_line = 'true';
if (vs.get(i).getLabel() == 'Line Manager Approval required?' || vs.get(i).getLabel() == 'Line Manager') {
show_line = 'false';
}
if (vs.get(i).getLabel() == 'Assignment Group') {
var gGroup = new GlideRecord('sys_user_group');
gGroup.get(vs.get(i).getDisplayValue());
gGroup.next();
template.print('<strong>' + vs.get(i).getLabel() + "</strong>: " + gGroup.getValue('u_display_name') + "<br/>");
}
if(vs.get(i).getLabel() == 'AD Action'){
var mrvs = current.variables.ad_management_multirow;//check and put correct name
var rowCount = mrvs.getRowCount();
template.print('<table>');
for (var s = 0; s < rowCount; s++) {
template.print("<tr>");
var row = mrvs.getRow(s);
template.print("<td>"+row.user_name+"</td>");//check fieldname
template.print(" ");
template.print("<td>"+row.user_email+"</td>");//check fieldname
template.print(" ");
template.print("<td>"+row.user_department+"</td>");//check fieldname
template.print("</tr>");
}
template.print('</table>');
}
if (vs.get(i).getLabel() != '' && vs.get(i).getLabel() != null && show_line == 'true' && vs.get(i).getDisplayValue() != '' && vs.get(i).getLabel() != 'Assignment Group' && vs.get(i).getLabel() != 'AD Action') {
template.space(4);
template.print('<strong>' + vs.get(i).getLabel() + "</strong>: " + vs.get(i).getDisplayValue() + "<br />");

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

The other fields are displaying but AD Action field is not

Hi,

I have added logs. Can you check if the code is entering the if condition of AD action or not. Share me the logs output.

(function runMailScript( /* GlideRecord */ current, /* TemplatePrinter */ template,
/* Optional EmailOutbound */
email, /* Optional GlideRecord */ email_action,
/* Optional GlideRecord */
event) {
var req_id = current.sysapproval;
template.print('<p><span style="font-family: arial, helvetica, sans-serif; font-size: 10pt;"><strong>' + req_id.number + '</strong>');
template.print(' : ' + req_id.short_description + ' <br /> <br />');

//print variables
var set = new GlideappVariablePoolQuestionSet();
set.setRequestID(req_id);
set.load();
var vs = set.getFlatQuestions();
if (vs.size() > 0) {
template.print('<strong>Request Item Variables:</strong><br /><br />');
for (var i = 0; i < vs.size(); i++) {
var show_line = 'true';
if (vs.get(i).getLabel() == 'Line Manager Approval required?' || vs.get(i).getLabel() == 'Line Manager') {
show_line = 'false';
}
if (vs.get(i).getLabel() == 'Assignment Group') {
var gGroup = new GlideRecord('sys_user_group');
gGroup.get(vs.get(i).getDisplayValue());
gGroup.next();
template.print('<strong>' + vs.get(i).getLabel() + "</strong>: " + gGroup.getValue('u_display_name') + "<br/>");
}
gs.log("Value is"+vs.get(i).getLabel());
if(vs.get(i).getLabel() == 'AD Action'){
gs.log("Entered into the IF conditon of AD Action");
var mrvs = current.variables.ad_management_multirow;//check and put correct name
var rowCount = mrvs.getRowCount();
gs.log("Row coun tof MRVS is "+rowCount);
template.print('<table>');
for (var s = 0; s < rowCount; s++) {
template.print("<tr>");
var row = mrvs.getRow(s);
template.print("<td>"+row.user_name+"</td>");//check fieldname
template.print(" ");
template.print("<td>"+row.user_email+"</td>");//check fieldname
template.print(" ");
template.print("<td>"+row.user_department+"</td>");//check fieldname
template.print("</tr>");
}
template.print('</table>');
}
if (vs.get(i).getLabel() != '' && vs.get(i).getLabel() != null && show_line == 'true' && vs.get(i).getDisplayValue() != '' && vs.get(i).getLabel() != 'Assignment Group' && vs.get(i).getLabel() != 'AD Action') {
template.space(4);
template.print('<strong>' + vs.get(i).getLabel() + "</strong>: " + vs.get(i).getDisplayValue() + "<br />");

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

var mrvs =current.variables.ad_management_multirow; - this is giving undefined.

I tried by using JSON.parse but looks like it doesn't work here

Hi,

current referring to RITM and that multirow variable set present in that ritm is ad_management_multirow

Cross verify the name once. 

Also share the output of remaining logs.