The CreatorCon Call for Content is officially open! Get started here.

MRVS - List collector / Notification multiple values

Niels Toulorge
Tera Contributor

Hello every one,

 

I need to send a notification from a RITM (with an email script) containing mutiple values in a List collector of a MVRS, I've got some issue.

 

I have a MRVS with 2 Lists collector : 

Capture.JPG

In the notification, it works when only 1 user/profile is selected in each row with this code :

 

 for (var i = 0; i < rowCount; i++) {
            template.print("<tr>");
            var row = mrvs.getRow(i);
            template.print("<td>" + getName(row.collaborateur_test.toString(),'sys_user') + "</td>");
            template.print("<td>" +  getName(row.profil_test.toString(),'question_choice') + "</td>");
            template.print("</tr>");

 

But when mutiple users or profile are selected in the same row, it doesn't work (return "Undefined", like the example below) : 

Capture2.JPG
Without the "getName", the notification return the list of the user's Sys_ID, separate by comma, example : 2fcc62f087f6aa1061d4ec6e8bbb35d8,59bc6ab087f6aa1061d4ec6e8bbb351c 
 
I think it's beacuse "getName" can't catch the sys_ID of each user.

Any idea ?

 

Ps : sorry for my bad english and my lack of knowledge in code.

1 ACCEPTED SOLUTION

@Niels Toulorge 

the reason it's working is because for the 1st row it has only 1 user

In the 2nd row there are more than 1 sysIds and hence it's breaking

update the getName() function as this and it will work for sure

function getName(sys_id, tblName) {
        var arr = [];
        var rec = new GlideRecord(tblName);
        rec.addQuery('sys_id', 'IN', sys_id.toString());
        rec.query();
        while (rec.next()) {
            arr.push(rec.getDisplayValue());
        }
        return arr.toString();
    }

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

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

View solution in original post

11 REPLIES 11

Ankur Bawiskar
Tera Patron
Tera Patron

@Niels Toulorge 

share the complete email script

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

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

Hello @Ankur Bawiskar , thanks for your prompt reply. Here it is :

(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>Type de test : " + current.variables.type_test);
	template.print("<br>Formation requise : " + current.variables.collab_formation);
	template.print("<br>");
   
    var mrvs = current.variables.p_rim_tre_test; // MRVS Internal name
    var rowCount = mrvs.getRowCount();
    if (rowCount >= 1) {
        template.print("<br<b>Detail de la demande</b>");
        template.print("<table border =1>");
        template.print("<tr>");
        template.print("<th>Collaborateurs</th>");
        template.print("<th>Profil</th>");
        template.print("</tr>");
        for (var i = 0; i < rowCount; i++) {
            template.print("<tr>");
            var row = mrvs.getRow(i);
	template.print("<td>" + getName(row.collaborateur_test.toString(),'sys_user') + "</td>");
            template.print("<td>" +  getName(row.profil_test.toString(),'question_choice') + "</td>");
            template.print("</tr>");
        }
        template.print("</table>");
    }
	
	template.print("<br>Durée de l'accès : " + current.variables.pour_quelle_duree);
	template.print("<br>Motivation : " + current.variables.justificatif_commentaire);
	//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);

 

@Niels Toulorge 

try this and add logs and see

(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>Type de test : " + current.variables.type_test);
    template.print("<br>Formation requise : " + current.variables.collab_formation);
    template.print("<br>");

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

	var parsedData = JSON.parse(mrvs);

    var rowCount = parsedData.length;
    if (rowCount >= 1) {
        template.print("<br<b>Detail de la demande</b>");
        template.print("<table border =1>");
        template.print("<tr>");
        template.print("<th>Collaborateurs</th>");
        template.print("<th>Profil</th>");
        template.print("</tr>");
        for (var i = 0; i < rowCount; i++) {
            template.print("<tr>");
            var row = parsedData[i];
            template.print("<td>" + getName(row.collaborateur_test.toString(), 'sys_user') + "</td>");
            template.print("<td>" + getName(row.profil_test.toString(), 'question_choice') + "</td>");
            template.print("</tr>");
        }
        template.print("</table>");
    }

    template.print("<br>Durée de l'accès : " + current.variables.pour_quelle_duree);
    template.print("<br>Motivation : " + current.variables.justificatif_commentaire);
    //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);

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

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

@Ankur Bawiskar 

 

Result seems the same.

 

RITM entries : 

 

Capture4.JPG

Notification :

 

Capture3.JPG