Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Unable to populate multiple email id's in the field

priyaj
Giga Explorer

Can help me @Vasantharajan N @Ankur Bawiskar 

 

As I want to populate selected users email id's in the field.

As off now in one of the field it's getting populated by the selected users as mentioned in below

priyaj_0-1710266230724.png

To get the details I had written mentioned Script Include

 

For Selected Users:

var CoUtils = Class.create();
CoUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    isPublic: function() {
        return true;
    },
    getSelectedUsers: function() {
        var userDetails = [];
        var userGr = new GlideRecord('sys_user');
        userGr.addEncodedQuery('sys_idIN' + this.getParameter('sysparm_selected_users'));
        userGr.query();
        while (userGr.next()) {
            userDetails.push(userGr.u_display_name + ' - ' + userGr.email);                
////This line is causing the issue as if I remove + ' - ' + userGr.email it is not capturing another selected user
      }
        return userDetails.join('\n');
    },
    assignToMe : function(){
        var selectedSysIds = this.getParameter('sysparm_selected_sys_ids');
        var taskGr = new GlideRecord('task');
        taskGr.addQuery('sys_id','IN',selectedSysIds);
        taskGr.query();
        while(taskGr.next()){
            taskGr.assigned_to = gs.getUserID();
            taskGr.update();
        }
    },
    type: 'CoUtils'
});

For Selected User Email ID's:

 

 

var CoEmails = Class.create();
CotEmails.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    isPublic: function() {
        return true;
    },
    getSelectedEmails: function() {
        var userDetailsEmail = [];
        var userEm = new GlideRecord('sys_user');
        userEm.addEncodedQuery('sys_idIN' + this.getParameter('sysparm_selected_emails'));
        userEm.query();
        while (userEm.next()) {
           userDetailsEmail.push(userEm.email);           
//This line is causing the issue as userGr.email it is not capturing another selected user email id
        }
        return userDetailsEmail.join('\n');
    },
    assignToMe : function(){
        var selectedSysEmails = this.getParameter('sysparm_selected_sys_emails');
        var taskGr = new GlideRecord('task');
        taskGr.addQuery('sys_id','IN',selectedSysEmails);
        taskGr.query();
        while(taskGr.next()){
            taskGr.assigned_to = gs.getSelectedEmails();
            taskGr.update();
        }
    },
    type: 'CoEmails'
});

And I had used this Script Includes in Catalog OnChange Client Scripts

 

For Selected Users Catalog Client Scripts: 

 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading) {
        return;
    }
    if (newValue == '') {
        g_form.clearValue('email_distribution_list_users_selected');      
    } else {
        var ga = new GlideAjax('global.CoUtils');
        ga.addParam('sysparm_name', 'getSelectedUsers');
        ga.addParam('sysparm_selected_users', newValue);
        ga.getXMLAnswer(function(answer) {
            g_form.setValue('email_distribution_list_users_selected', answer);       
        });
    }
}

 

 For Selected Users Email ID's Catalog Client Scripts:

 

function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading) {
      return;
   }
    if (newValue == '') {
        g_form.clearValue('selected_users_email_id_s');
    } else {
        var ga = new GlideAjax('global.CoEmails');
        ga.addParam('sysparm_name', 'getSelectedEmails');
        ga.addParam('sysparm_selected_emails', newValue);
        ga.getXMLAnswer(function(answer) {
            g_form.setValue('selected_users_email_id_s', answer);        
        });
    }
}

 

4 REPLIES 4

Ankur Bawiskar
Tera Patron
Tera Patron

@priyaj 

what debugging have you done so far?

update line as this

userDetailsEmail.push(userEm.email.toString());    

 

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

Vasantharajan N
Tera Sage
Tera Sage

@priyaj - Please update the below code to use toString() in CoEmails script include and check.

From

userDetailsEmail.push(userEm.email); 

 To

userDetailsEmail.push(userEm.email.toString()); 

 


Thanks & Regards,
Vasanth

How about 

For Selected Users:

        while (userGr.next()) {
            userDetails.push(userGr.u_display_name + ' - ' + userGr.email);                
////This line is causing the issue as if I remove + ' - ' + userGr.email it is not capturing another selected user
      }

SanSri1122
Tera Expert

Please try below line,

 

userDetailsEmail.push(userEm.email + '')