- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-15-2025 01:53 AM
Hi There,
I have a requirement to set email ids in string field "," separated on selection on users in list collector field. Once any user is removed from list collector then it should remove the related email id as well form string field.
i tried with below script but its not working
Client script:
var UserEmailHelper = Class.create();
UserEmailHelper.prototype = {
initialize: function() {
},
getEmailIds: function(userIds) {
var emailIds = [];
var userRecord = new GlideRecord('sys_user');
userRecord.addQuery('sys_id', 'IN', userIds);
userRecord.query();
while (userRecord.next()) {
emailIds.push(userRecord.email.toString());
}
return emailIds.join(',');
},
type: 'UserEmailHelper'
};
Any correction required?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-15-2025 02:08 AM
update as this
Client Script:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading) {
return;
}
// Get the selected user sys_ids from the list collector (comma-separated string)
var selectedUsers = g_form.getValue('u_users'); // Ensure 'u_users' is your list collector's name
// Call the Script Include to get the email IDs
var ga = new GlideAjax('UserEmailHelper');
ga.addParam('sysparm_name', 'getEmailIds');
ga.addParam('sysparm_userIds', selectedUsers); // Pass the comma-separated sys_ids
ga.getXMLAnswer(function(response) {
g_form.setValue('u_email_field', response); // Set the comma-separated emails
});
}
Script Include:
var UserEmailHelper = Class.create();
UserEmailHelper.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getEmailIds: function() {
var userIds = this.getParameter('sysparm_userIds');
var emailIds = [];
if (userIds) {
var gr = new GlideRecord('sys_user');
gr.addQuery('sys_id', 'IN', userIds);
gr.query();
while (gr.next()) {
if (gr.email) {
emailIds.push(gr.email.toString());
}
}
}
return emailIds.join(',');
}
});
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-15-2025 02:08 AM
update as this
Client Script:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading) {
return;
}
// Get the selected user sys_ids from the list collector (comma-separated string)
var selectedUsers = g_form.getValue('u_users'); // Ensure 'u_users' is your list collector's name
// Call the Script Include to get the email IDs
var ga = new GlideAjax('UserEmailHelper');
ga.addParam('sysparm_name', 'getEmailIds');
ga.addParam('sysparm_userIds', selectedUsers); // Pass the comma-separated sys_ids
ga.getXMLAnswer(function(response) {
g_form.setValue('u_email_field', response); // Set the comma-separated emails
});
}
Script Include:
var UserEmailHelper = Class.create();
UserEmailHelper.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getEmailIds: function() {
var userIds = this.getParameter('sysparm_userIds');
var emailIds = [];
if (userIds) {
var gr = new GlideRecord('sys_user');
gr.addQuery('sys_id', 'IN', userIds);
gr.query();
while (gr.next()) {
if (gr.email) {
emailIds.push(gr.email.toString());
}
}
}
return emailIds.join(',');
}
});
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-15-2025 02:45 AM
Thanks @Ankur Bawiskar after making corrections it works.