Split a List Collector Variable to an array

Dan R
Kilo Expert

Good Afternoon SNC,

 

I have a form on the Service Portal that allows end users to request 1 or more users be added to a resolver group. They do this by selecting users from a list collector (variable name: members) and moving them from the left-side of the list collector to the right-side. The list collector references the sys_user table.

 

In order for me to fulfill this request, I have to go 1-by-1 through each of the users on the list collector, take their user_name and add it to a group in Active Directory. 

My issue here is, when this list of users to add is VERY long, it becomes extremely tedious to go 1-by-1 to get the user_name of each employee.

 

My thought here was that I could have a script run in the "advanced script" section of the Catalog Task in the Workflow, and have script:

1- take the 'members' list collector and create an array of values

2- take array of values and run a GlideRecord on the sys_user table to find the associated user_name for each employee

3- take the user_name of each employee and put it into a new array

4- display new array of user_name's in a multi-line text variable I named: admin_only

 

I tried giving this a stab but for some reason I feel like I'm getting stuck very early on in the array-creation process. Does anyone have insight into how I can best accomplish what I'm trying to do here?

 

Thanks in advance,

 

Dan R

1 ACCEPTED SOLUTION

Prateek kumar
Mega Sage

try

var list = current.list_collector_variable.toString();
var listSplit = list.split(',');

for(var i=0; i<listSplit; i++){
//Do something here
}

Please mark my response as correct and helpful if it helped solved your question.
-Thanks

View solution in original post

6 REPLIES 6

rajesh9885
Mega Guru

Hello

You can go for for loop

var ar = g_form.getValue('<filed>); // you field
var as = ar.toString();
var ad = as.split(',');
for (var i=0; i < ad.length; i++) {
var newval = ad[i];
// alert('vlaue of newval is:- '+newval);

if (newval)
{

var gr = new GlideRecord('table'); // table you need to glide and continue with desired code

this didnt' work.

Prateek kumar
Mega Sage

try

var list = current.list_collector_variable.toString();
var listSplit = list.split(',');

for(var i=0; i<listSplit; i++){
//Do something here
}

Please mark my response as correct and helpful if it helped solved your question.
-Thanks

This worked wonderfully minus 1 thing. Your code was missing the .length in the for loop. See below for my full code that I used to accomplish what I want.

 

// declare variable and assign list collector "members" to it as a string
var list = current.variables.members.toString();
// split the list variable on commas
var listSplit = list.split(',');
// for loop to parse through the array to address each value
 for(var i=0;i<listSplit.length;i++){
// instaniate GlideRecord variable on sys_user table
	 var user = new GlideRecord ('sys_user');
// add query that uses each value of the listSplit array and checks the sys_id field on the sys_user table
	 user.addQuery("sys_id",listSplit[i]);
	 user.query();
// parse results and for each result, append the user_name to the multi-line text field that will show these values
	 if(user.next()){
		 current.variables.admin_only += (user.user_name + "; ");
	 }

 }

 

Output was a semi-colon delimited list of user_name's that I can then copy and paste into AD and click check names and all users add the group much quicker.

 

 

Thanks for your help!