Can you push more than one queried value into an array to 'format' the comma separated output?

Cirrus
Kilo Sage

We are using the below script to create an additional variable on a RITM to sort list collector values by alphabetical order (not the order in which they were selected). This variable is then displayed in a dashboard report. My question is is it possible in the text highlighted in red to query both name and user id and push both values into the array such that the end result is John Smith - john.x.smith,Fred Bloggs - fred.bloggs........

I have tried the following but it just returns the userIDName, so john.x.smth,fred.bloggs.... 

var userName = userGr.name;

 var userIDName = userGr.user_name

userNameArray.push(userName + "-" + userIDName)

 

The full code is:

 

//Get the value of the list collector variable

var listCollectorValue = current.variables.nominee_name.toString();  

//Split the value by comma

var listCollectorArray = listCollectorValue.split(",");

//Create an empty array to store the user names

var userNameArray = [];

 

//Loop through the list collector array

for (var i = 0; i < listCollectorArray.length; i++) {

  //Get the sys_id of each user

  var userId = listCollectorArray[i];

  //Create a GlideRecord object for the sys_user table

  var userGr = new GlideRecord("sys_user");

  //Query the table by sys_id

  userGr.get(userId);

  //Get the name of the user

  var userName = userGr.name;

  //Push the name to the user name array

  userNameArray.push(userName);

}

 

//Sort the user name array alphabetically

userNameArray.sort();

 

//Join the user name array by comma

var sortedListCollectorValue = userNameArray.join(", ");

 

//Return the sorted value as the scripted field value and update the RITM

var ritm = new GlideRecord('sc_req_item');

ritm.get('sys_id', current.sys_id); // Pass in the sys_id of the RITM

ritm.variables.<variable> = sortedListCollectorValue; 

ritm.update();

2 REPLIES 2

Kalyani Jangam1
Mega Sage
Mega Sage

Hi @Cirrus 

Please try below script

var arr_final=[];
var num,num1;
var gr=new GlideRecord("sys_user");
gr.addEncodedQuery("location=30fffb993790200044e0bfc8bcbe5dcc");
gr.query();
while(gr.next()){
num=gr.user_name.toString();
num1=gr.name.toString();
arr_final.push(num,num1)

}

gs.print(arr_final) // Here all UserID and Name of above user came

// Below logic for split the userid and Name

for(var i=0;i<arr_final.length;i++){
if(i%2){
gs.print("Name----"+arr_final[i]);

}
else{
gs.print("UserID---"+arr_final[i])
}

}

 

Please check and Mark Helpful and Correct if it really help you.

Thanks Kalyani, but still struggling here. Even before getting to the presentation aspect, if I just push 

userNameArray.push(userName,userIDName) would you not expect the returned value to be John Smith,john.x.smith,Fred Bloggs,fred.bloggs. Its just returning user names in the list.

 

Currently trying the below snippet, but now the variable returns blank

//Get the value of the list collector variable
var listCollectorValue = current.variables.nominee_name.toString();   
//Split the value by comma
var listCollectorArray = listCollectorValue.split(",");
//Create an empty array to store the user names
var userNameArray = [];
 
//Loop through the list collector array
for (var i = 0; i < listCollectorArray.length; i++) {
  //Get the sys_id of each user
  var userId = listCollectorArray[i];
  //Create a GlideRecord object for the sys_user table
  var userGr = new GlideRecord("sys_user");
   userGr.get(userId);
while (userGR(next)){
  var userName = userGr.name;
var userIDName = userGr.user_name;
}
    userNameArray.push(userName,userIDName);
}