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.

How to Populate List collector values to the Multi line text variable

Aparna Gurav
Tera Contributor

Hello Team, 

I need your help. I’m trying to copy the value from a list collector to a multiline text variable, I attempted with the client script and script include but it's not working.

AparnaGurav_0-1740668519252.png

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

   //Type appropriate comment here, and begin script below
      // Get the list collector values (comma-separated sys_ids)
    var listCollectorValues = g_form.getValue('ad_group_members'); // Change this ID

    if (listCollectorValues) {
    
        var ga = new GlideAjax('SPR_Repo'); // Call Script Include
        ga.addParam('sysparm_name', 'getNames');
        ga.addParam('sysparm_ids', listCollectorValues);
        ga.getXMLAnswer(function(response) {
            if (response) {
                g_form.setValue('list_collectors_names', response); // Change this ID
            }
        });
    }
   
}
    getNames: function() {
        var userIds = this.getParameter("sysparm_ids");
        gs.log('User IDs:', userIds); // Debug statement

        if (!userIds) return '';

        var userNames = [];
        var idsArray = userIds.split(',');

        var gr = new GlideRecord('sys_user');
        gr.addQuery('sys_id', 'IN', idsArray);
        gr.query();

        while (gr.next()) {
            userNames.push(gr.getValue('name')); // Get full name
        }
        gs.log('User Names:', userNames); // Debug statement
        return userNames.join('\n'); // Join with line breaks
    },

 

6 REPLIES 6

Swapna Abburi
Mega Sage
Mega Sage

Hi @Aparna Gurav 

 

I think you don't need this line in your script include:

var idsArray = userIds.split(',');

Replace your script include function with below code and try.

 getNames: function() {
        var userIds = this.getParameter("sysparm_ids");
        gs.log('User IDs:', userIds); // Debug statement

        if (!userIds) return '';

        var userNames = [];

        var gr = new GlideRecord('sys_user');
        gr.addQuery('sys_id', 'IN', userIds);
        gr.query();

        while (gr.next()) {
            userNames.push(gr.getValue('name')); // Get full name
        }
        gs.log('User Names:', userNames); // Debug statement
        return userNames.join('\n'); // Join with line breaks
    },

 

 

@Swapna Abburi : Its the same issue, I update SI as per the above code


AparnaGurav_0-1740673289150.png

 

getNames: function() {
        var userIds = this.getParameter("sysparm_ids");
        gs.log('User IDs:', userIds); // Debug statement

        if (!userIds) return '';

        var userNames = [];
        

        var gr = new GlideRecord('sys_user');
        gr.addQuery('sys_id', 'IN', userIds);
        gr.query();

        while (gr.next()) {
            userNames.push(gr.getValue('name')); // Get full name
        }
        gs.log('User Names:', userNames); // Debug statement
        return userNames.join('\n'); // Join with line breaks
    },

Ankur Bawiskar
Tera Patron
Tera Patron

@Aparna Gurav 

use this

getNames: function() {
    var userIds = this.getParameter("sysparm_ids");
    gs.log('User IDs:', userIds); // Debug statement

    if (!userIds) return '';

    var userNames = [];

    var gr = new GlideRecord('sys_user');
    gr.addQuery('sys_id', 'IN', idsArray);
    gr.query();
    while (gr.next()) {
        userNames.push(gr.getValue('name')); // Get full name
    }
    gs.log('User Names:', userNames); // Debug statement
    return userNames.join('\n'); // Join with line breaks
},

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