List collector have to get email ton populate by user name

Darlene York
Tera Contributor

I have a requirement to use a List collector and have the user that is selecteds email to populate by user name.

 

i can do it when it’s just a reference field and only one user can be selected.  But multiple users can be selected, that’s why I’m using a list collector. 

thank you in advance

 

4 REPLIES 4

VaniMadhuri
Tera Contributor

Hi  @Darlene York 

A List Collector stores multiple selected users as comma-separated sys_ids, so you cannot dot-walk like a normal reference field. You need to send those selected sys_ids to the server and return the emails.
You can solve this with a Catalog Client Script + GlideAjax.


Script include code

var GetUserEmails = Class.create();
GetUserEmails.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    getEmails: function () {
        var userIds = this.getParameter('sysparm_user_ids');
        var emails = [];

        if (!userIds)
            return '';
        var userGR = new GlideRecord('sys_user');
        userGR.addQuery('sys_id', 'IN', userIds);
        userGR.query();
        while (userGR.next()) {
            if (userGR.email) {
                emails.push(userGR.email.toString());
            }
        }
        return emails.join(', ');
    },
    type: 'GetUserEmails'
});

 

VaniMadhuri_2-1778030827455.png

 

 

Catalog Client Script for AWS Account Request and Variable name is “list of users”

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || !newValue) {
        g_form.setValue('user_emails', '');
        return;
    }

    var ga = new GlideAjax('GetUserEmails');
    ga.addParam('sysparm_name', 'getEmails');
    ga.addParam('sysparm_user_ids', newValue);

    ga.getXMLAnswer(function(answer) {
        g_form.setValue('user_emails', answer);
    });


   //Type appropriate comment here, and begin script below
   
}

VaniMadhuri_1-1778030801819.png

While tests in AWS Account Request 

  • User selects multiple users in List Collector
  • Script runs on change
  • Sends selected users to Script Include
  • Fetches emails
  • Then it automatically populates email field

VaniMadhuri_0-1778030764059.png

 

 

 

Ankur Bawiskar
Tera Patron

@Darlene York 

Sorry you will have list collector and when user selects multiple users what should happen?

 

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

Tanushree Maiti
Kilo Patron

Hi @Darlene York ,

I dont think your requirement is feasible.

 

You can do one thing. 

To display user emails from a List Collector, use an onChange Client Script paired with GlideAjax to fetch email addresses for selected users and populate them in a Multi-Line Text variable. The List Collector displays user names (from the sys_user table), while the script converts these into email addresses.

 

 

Please mark this response as Helpful & Accept it as solution if it assisted you with your question.
Regards
Tanushree Maiti
ServiceNow Technical Architect
Linkedin:

Thank you, that does help.  Would it be possible to get a sample script?

 

Thank you