Add multiple email addresses to List Collector on Service Portal/Catalog

Mairvette Tuck1
Mega Contributor

Hello.

Within a catalog item, we have two fields. One is for the user's name the other for their email address.

I've used the table 'sys_user' for both fields. When the user's name is populated, I have it automatically adding their email address to the specific email field, both with 'List Collector' as their variable type.

Issue I am having, is if multiple users are added to the User field, no email address (even previously displayed for the first user) will be displayed. How do I get each additional email address to be displayed in the List Collector / email field?

find_real_file.pngfind_real_file.png

30 REPLIES 30

i get this in the native:

find_real_file.png

Let me look into it

Thanks,
Mohit Kaushik
ServiceNow MVP (2023-2025)

can you change the below line 

user.query('sys_id', ids[i]);

with 

user.addQuery('sys_id',ids[i]);

Thanks,
Mohit Kaushik
ServiceNow MVP (2023-2025)

same error message

Hi Mairvette,

Sorry for my above responses. Here is the final script with testes results.

You need to create a script include and then you can call it in your client script and it will work properly. Keep the client callable check box true in your script include

// Client script
function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }

    //Type appropriate comment here, and begin script below
    var ids = g_form.getValue('users_add'); // to get the comma separated ids.
    var ga = new GlideAjax('UserEmails');
    ga.addParam('sysparm_name', 'getEmails');
    ga.addParam('sysparm_user', ids);
    ga.getXML(setEmail);

    function setEmail(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
        var clearvalue; // Stays Undefined
        if (answer) {
            var returneddata = answer;
            g_form.setValue("email_address_add", returneddata);
        } else {
            g_form.setValue("email_address_add", '');
        }
    }

 

Below is your script include code

var UserEmails = Class.create();
UserEmails.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getEmails: function () {
	var user;
	var emailids='';
	var userId = this.getParameter('sysparm_user').toString().split(',');
	for (var i=0; i<userId.length;i++)
		{
			user = new GlideRecord('sys_user');
			user.addQuery('sys_id',userId[i]);
			user.query();
			if(user.next())
				{
					if(emailids=='')
						{
							emailids=user.email;
						}
					else
						emailids=emailids+','+user.email;
				}
		}
	return emailids;
},
    type: 'UserEmails'
});

 

Below are the screenshots for working code

find_real_file.png

find_real_file.png

 

Thanks,

Mohit Kaushik

Thanks,
Mohit Kaushik
ServiceNow MVP (2023-2025)