- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-27-2021 09:01 AM
Hello, Requesters would be selecting the users using List Collector and requirement is to copy the email addresses of selected users with comma separated (Right Side of List collector) to another variable (Multi Line Text Variable). Text Variable to make it read only and visible to Fulfillers.
I have tried creating script by looking some past examples but seems its not working. Any suggestions on what seems to be wrong please
Variable Details:
Client Script
Type - onChange, Variable Name - select_users //list collector variable
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
if(newValue === '') {
g_form.clearValue('copied_from_list');
}
var gaPhone = new GlideAjax('getEmailAddress');
gaPhone.addParam('sysparm_name', 'get_email');
gaPhone.addParam('sysparm_users', newValue);
gaPhone.getXMLAnswer(_handleResponse);
function _handleResponse(response) {
var answer = response;
g_form.setValue('copied_from_list', answer);
}
}
Script Include - Client Callable: Checked
var getEmailAddress = Class.create();
getEmailAddress.prototype = Object.extendsObject(AbstractAjaxProcessor, {
get_email : function() {
var userArr = this.getParameter('sysparm_users').split(',');
var emailArr = [];
for(var i = 0; i < userArr.length; i++) {
var grUser = new GlideRecord('sys_user');
if(grUser.get(userArr[i])) {
emailArr.push(grUser.getValue('email'));
}
}
emailArr.join();
return emailArr.toString();
},
type: 'getEmailAddress'
});
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-28-2021 06:39 AM
Hi Vinay,
then you can use Run Script in the workflow of catalog item
Updated: to remove current.update()
var userArr = current.variables.select_users.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();
current.variables.copied_from_list = values;
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-27-2021 09:32 AM
Hi,
Try this code.
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var gaPhone = new GlideAjax('getEmailAddress');
gaPhone.addParam('sysparm_name', 'get_email');
gaPhone.addParam('sysparm_users', newValue);
gaPhone.getXMLAnswer(_handleResponse);
function _handleResponse(response) {
alert(response);
g_form.setValue('copied_from_list', response);
}
}
Script Include
var getEmailAddress = Class.create();
getEmailAddress.prototype = Object.extendsObject(AbstractAjaxProcessor, {
get_email : function() {
var userArr = this.getParameter('sysparm_users').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'));
}
}
emailArr.join();
return emailArr.toString();
},
type: 'getEmailAddress'
});
Mark the comment as a correct answer and helpful if this helps to solve the problem.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-14-2023 05:28 AM
Thanks for this code. This is working super fine.
** to show the email address in the text variable, just write down one line code after alert in client script.
g_form.setValue('business_owner_email', response);
Where ('business_owner_email') is my variable field name.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-28-2021 05:07 AM
Hello, Thank you both for your response. Checked with alert and response seems to be coming from Script Include, If i select and add more than 5 user records in list collector and submit the form in Service Portal, can see the values in text variable.
If right side selected user list is less than 5 then text variable showing empty after submission and then if i select and try to add any user then i get response from script include.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-28-2021 05:53 AM
Try below to run in synchronous mode.
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
if(newValue === '') {
g_form.clearValue('copied_from_list');
}
var gaPhone = new GlideAjax('getEmailAddress');
gaPhone.addParam('sysparm_name', 'get_email');
gaPhone.addParam('sysparm_users', newValue);
gaPhone.getXMLAnswer(handleResponsefn);
function handleResponsefn(ans) {
var answer = ans;
g_form.setValue('copied_from_list', answer);
}
}
Can you let us know why you are copying the email addresses in a multi line text field. Is this field should be visible while submitting the form? or if you want to show this field for fulfillers then the best way is to update the variables in workflow.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-28-2021 06:05 AM
Yes, It should be visible to only fulfillers as read only to copy all the email addresses.
This shouldn't be visible while submitting.
I have tried to run in synchronous mode but it does't copy the email addresses if its less than 5 records.