Populate users with comma separated in a String field
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-30-2020 10:11 AM
Hi folks,
on the request form, there are 2 fields, "Requester" which is of type reference and "Users List" which is of type String. When i select a user in the requester then in the Users List field, his user ID should populate and the Requester field should get cleared. Another time if you select other user, then the Users List field should populate with that value too with comma separated.
Note: The user which we have already selected should not populate again when we try to select him again. It should give alert like "you have already selected the user"
So for my above query, i am showing an example here:
If i select Abel Tuter in Requester field, then the Users List field should populate with Abel.Tuter, Now if you select Dean james then the Users List field should look like Abel.Tuter,Dean.Jones
Like this the Requester field should be cleared and the selected users should populate in the Users List field with comma separated. No duplicates should come.
I tried with get Reference method, and i am able to populate Users List field with only one value. I think we have to use for loop and an array to store those selected values.
Can some one please give me code for this above task
regards,
Lucky

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-30-2020 08:48 PM
My version:
Client script:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var ajax = new GlideAjax('GetUserInfo');
ajax.addParam('sysparm_name', 'getUserId');
ajax.addParam('sysparm_user_id', newValue);
ajax.getXMLAnswer(function(answer) {
if (answer.length > 0) {
var users = g_form.getValue('users_list');
var usersList = users.split(',');
if (!usersList.includes(answer)) {
if (users.length > 0) {
users += ',';
}
users += answer;
g_form.setValue('users_list', users);
}
}
});
g_form.setValue('requester', '');
}
Script include:
var GetUserInfo = Class.create();
GetUserInfo.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getUserId: function() {
var userSysId = this.getParameter('sysparm_user_id');
var grUser = new GlideRecord('sys_user');
if (grUser.get(userSysId)) {
return grUser.user_name;
}
return '';
},
type: 'GetUserInfo'
});
Execution output:
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-01-2020 12:19 AM
HI Hitoshi,
I tried with your code,
When i select Abel Tuter, the Users List field is populating with undefined,,Abel.Tuter, thats it.
Please observe, it is taking two commas above
The Value of Requester is getting cleared but when i again select, Alene Rebeck then the Abel.Tuter is replaced with Alene.Rebeck
So my Users List field is now, undefined,Alene.Rebeck
Regards,
Lucky

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-01-2020 03:02 PM
It may be the variable names. I've used "user_list" in my code. Following is a code using u_user_list.
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var ajax = new GlideAjax('GetUserInfo');
ajax.addParam('sysparm_name', 'getUserId');
ajax.addParam('sysparm_user_id', newValue);
ajax.getXMLAnswer(function(answer) {
if (answer.length > 0) {
var users = g_form.getValue('u_users_list');
var usersList = users.split(',');
if (!usersList.includes(answer)) {
if (users.length > 0) {
users += ',';
}
users += answer;
g_form.setValue('u_users_list', users);
}
}
});
g_form.setValue('requester', '');
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-01-2020 02:13 AM

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-01-2020 02:41 AM
Can you try below.
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var getmembers = g_form.getValue("u_members");
var existingList;
if (getmembers != '') {
existingList = getmembers;
}
var caller = g_form.getReference('u_select_the_members', getRequestedInfo);
function getRequestedInfo(caller) {
// var existingList = g_form.getValue("u_members");
var newList = [];
if (existingList.length > 0) {
newList = existingList.split(",");
if (newList.indexOf(caller.name) < 0) {
newList.push(caller.name);
g_form.setValue("u_members", newList);
} else
g_form.addErrorMessage("you have already selected the user");
} else
g_form.setValue("u_members", caller.name);
}
g_form.setValue("requestor", "");
}