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.

MRVS in JSON format

hanaphouse
Tera Guru

What changes to the code should I do to achieve the desired output?

We have a workflow with a Run Script step that has the following script:

var groups = current.variables.groups_mrvs;
var groupStr = JSON.stringify(groups);

However, groupStr returns the following data:

[ {
"group" : "TeamA",
"group_permissions" : "f392c4e71bd54d1021adfeeccd4bcbe6,0bd248e71bd54d1021adfeeccd4bcbe1"
} ]

The desired output should be:

[{"TeamA":["Create","Update"]
}]

Groups (Select Box) and Group Permissions (List Collector) are part of the groups_mrvs which is a multi-row variable set.

1 ACCEPTED SOLUTION

Brad Bowman
Kilo Patron
Kilo Patron

Your desired output looks a little misleading in that you're taking the value of 2 variables, but presenting it in a name : value format.  In any case, if that's what you want, you'll need to build it yourself from the MRVS JSON (your groups script variable).  You'll also need to do a GlideRecord on the list collector referenced table to get the display value associated with each sys_id, so something like this will get you closer:

var groupArr = [];
var groupPermArr = [];
var groups = current.variables.groups_mrvs; //creates an array of each row of variable names and values stored in the Multi Row Variable Set
var rowCount = mrvs.getRowCount();
for (var i=0; i<rowCount; i++) { //for each row in the MRVS
 groupPermArr = []; 
 var row = mrvs.getRow(i); 
 var groupPerm = new GlideRecord('dms_document_group_permission'); //list collector reference table
 groupPerm.addQuery('sys_id', 'IN', row.group_permissions.toString());
 groupPerm.query();
 while (groupPerm.next()) {
  groupPermArr.push(groupPerm.type); //display field on list collector table
 }
 groupArr.push({
   row.group.toString() : groupPermArr.join(',')
 });
} 
var groupStr = JSON.stringify(groupArr.join(','));


View solution in original post

3 REPLIES 3

Brad Bowman
Kilo Patron
Kilo Patron

Your desired output looks a little misleading in that you're taking the value of 2 variables, but presenting it in a name : value format.  In any case, if that's what you want, you'll need to build it yourself from the MRVS JSON (your groups script variable).  You'll also need to do a GlideRecord on the list collector referenced table to get the display value associated with each sys_id, so something like this will get you closer:

var groupArr = [];
var groupPermArr = [];
var groups = current.variables.groups_mrvs; //creates an array of each row of variable names and values stored in the Multi Row Variable Set
var rowCount = mrvs.getRowCount();
for (var i=0; i<rowCount; i++) { //for each row in the MRVS
 groupPermArr = []; 
 var row = mrvs.getRow(i); 
 var groupPerm = new GlideRecord('dms_document_group_permission'); //list collector reference table
 groupPerm.addQuery('sys_id', 'IN', row.group_permissions.toString());
 groupPerm.query();
 while (groupPerm.next()) {
  groupPermArr.push(groupPerm.type); //display field on list collector table
 }
 groupArr.push({
   row.group.toString() : groupPermArr.join(',')
 });
} 
var groupStr = JSON.stringify(groupArr.join(','));


Thanks. I actually went with a similar solution. I was thinking that there might be an easier way especially with list collectors in an MRVS returning sys_id.

pavel_martinec
Tera Contributor

Hi, I had the similar problem, and I couldn't find a solution. Things like looping Object.keys(mrvs); dodn't work as those keys are internal functions, and so on.

 

In the end, the solution is laughably easy.

var mrvsJSON = JSON.parse(current.variables.mrvs+"");

 

 

Works like charm.

 

Hope this helps someone later as well 🙂