Setting values in string field on selection of list collector

Vijay Baokar
Kilo Sage

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:

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

 

    // Get the selected user IDs from the list collector
    var selectedUsers = g_form.getValue('u_users);

 

    // Call the Script Include to get the email IDs
    var emailIds = new GlideAjax('UserEmailHelper');
    emailIds.addParam('sysparm_name', 'getEmailIds');
    emailIds.addParam('sysparm_userIds', selectedUsers);
    emailIds.getXMLAnswer(function(response) {
        var emailString = response.responseXML.documentElement.getAttribute('answer');
        g_form.setValue('u_email_field', emailString);
 
 
Script Include: 

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? 

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@Vijay Baokar 

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.

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

View solution in original post

2 REPLIES 2

Ankur Bawiskar
Tera Patron
Tera Patron

@Vijay Baokar 

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.

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

Thanks @Ankur Bawiskar after making corrections it works.