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.

Need a client script for comma separated

Saib1
Tera Guru

Hi Team,

 

Please find the below screenshot

 

Username has the collector which contains the value as reference from sys_user table

 

For Example:

salma@gmail.com

rekha@gmail.com

ameer@gmail.com

 

Saib1_1-1700129221930.png

 

I need to write a client script to get those 3 value in comma separated and save it in below user id field

 

Saib1_0-1700129119576.png

 

I have a client script for getting one value , Not sure how to get 3 values with comma separated

 

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue === '') {
return;
}
var user = g_form.getReference('first_name', getdetails);
function getdetails(user) {
g_form.setValue('email', user.email);
g_form.setValue('user_id', user.user_name);
}
}

 

1 ACCEPTED SOLUTION

@Ankur Bawiskar 

 

as per my requirement you code this helps me ,It displays the sys id to string like below

 

rekha@gmail.com,sal@gmail.com,halit@gmail.com

 

Code

******

var newValue= "8fe3414f1bddb410f6e7a822b24bcb4b,133bd44f1bc5a910f03fa861f54bcb3a,cea8a0f71b978194880235e4464bcb70"
var userArr = newValue.toString().split(',');
var emailArr = [];
 
for(var i = 0; i < userArr.length; i++) {
var grUser = new GlideRecord('sys_user');
 
if(grUser.get(userArr[i].toString())) {
emailArr.push(grUser.getValue('email'));
}
}
 
var values = emailArr.join();
var test = values;
gs.print(values);

View solution in original post

10 REPLIES 10

@Saib1 

I answered something similar earlier

sample working script is here; enhance for your case

Populate Email Addresses of users selected in List Collector variable to Multi Line Text Variable

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

@Ankur Bawiskar 

 

 

I tried this code but result is not coming , getting the value from sysparm_env and displaying in  user_id variable

 

Script Include

var Fetch_UserDetails_User_Access_Management = Class.create();
Fetch_UserDetails_User_Access_Management.prototype = {
 
getfirstname: function(){
var userArr = this.getParameter('sysparm_name').toString().split(',');
var emailArr = [];
for(var i = 0; i < userArr.length; i++) {
var grUser = new GlideRecord('sys_user');
 
if(grUser.get(userArr[i].toString())) {
emailArr.push(grUser.getValue('user_name'));
}
}
emailArr.join();
return emailArr.toString();
 
},
 
    type: 'Fetch_UserDetails_User_Access_Management'
};
 
Client Script
 
function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
   alert(newValue);
    var ga = new GlideAjax("Fetch_UserDetails_User_Access_Management");
   ga.addParam("sysparm_name", "getfirstname");
   ga.addParam("sysparm_env", "rekha@gmail.com,selva@gmail.com,nagar@gmail.com");
   ga.getXMLAnswer(populateRoles);
 
   function populateRoles(ans){
var answer = ans;
g_form.setValue('user_id', answer);
 
}
    //Type appropriate comment here, and begin script below
   }

 

@Saib1 

the link I shared has a working solution.

Please refer that and enhance. Also debug it in terms of client side and server side

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

@Ankur Bawiskar 

 

as per my requirement you code this helps me ,It displays the sys id to string like below

 

rekha@gmail.com,sal@gmail.com,halit@gmail.com

 

Code

******

var newValue= "8fe3414f1bddb410f6e7a822b24bcb4b,133bd44f1bc5a910f03fa861f54bcb3a,cea8a0f71b978194880235e4464bcb70"
var userArr = newValue.toString().split(',');
var emailArr = [];
 
for(var i = 0; i < userArr.length; i++) {
var grUser = new GlideRecord('sys_user');
 
if(grUser.get(userArr[i].toString())) {
emailArr.push(grUser.getValue('email'));
}
}
 
var values = emailArr.join();
var test = values;
gs.print(values);

Community Alums
Not applicable

Hi Saib,

I'm not entirely sure what the ask is here... 

You want to populate a list collector field but from which other fields?