List collector

anvitha ash
Tera Contributor

I need to get values from list collector field and pass it to Ajax

 

I have two fields

Primary owner - reference 

Secondary owner - list collector 

Now I need to fetch all the values from both the fields (field 1: Primary owner - reference, field 2: Secondary owner );

 

data : function () {

var name = this.getParameter(sysparm_item);

var gr = new Glide Record('xyz_table');

gr.addquery('sys_id', name);

gr.query();

if (gr.next()){

var aa = gr.secondary_owner + gr.primary_owner ;

return aa;

 

I am getting values but it is not population in comma seperate values

How can I get the values in comma seperated format and how can I access this values in Ajax

 

Need suggestions here. Thanks in advance ☺️ 

 

2 REPLIES 2

d_17
Tera Expert

Are you referring to one value which is returned from the above script.

Did you try gr.secondary_owner +","+ gr.primary_owner ;

Shaqeel
Mega Sage

Hi @anvitha ash 

 

 

Use a GlideAjax Script Include to handle the server-side logic and return the data to your client-side script.

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

    getOwners: function() {
        var sysId = this.getParameter('sysparm_item'); // get the sys_id passed from client-side

        var gr = new GlideRecord('xyz_table');
        gr.addQuery('sys_id', sysId); // query based on the passed sys_id
        gr.query();

        var result = '';

        if (gr.next()) {
            // Get Primary Owner (Reference Field)
            var primaryOwner = gr.getValue('primary_owner');
            
            // Get Secondary Owner (List Collector - contains multiple values)
            var secondaryOwners = gr.getValue('secondary_owner');
            
            // If primaryOwner exists, add to result
            if (primaryOwner) {
                result += primaryOwner;
            }

            // If secondaryOwners exists, split them by comma and add to result
            if (secondaryOwners) {
                var secondaryOwnerArray = secondaryOwners.split(',');
                if (result) {
                    result += ', ';  // Add a comma before appending secondary owners
                }
                result += secondaryOwnerArray.join(', ');
            }
        }

        // Return the comma-separated list of owners
        return result;
    }
});

 

 

 

Use a client-side GlideAjax call to invoke the server-side script and get the values.

function fetchOwners() {
    var sysId = g_form.getValue('sys_id'); // Assuming you have the sys_id of the record

    // Create the GlideAjax object
    var ga = new GlideAjax('CustomAjax');  // Name of your Script Include
    ga.addParam('sysparm_name', 'getOwners');  // Name of the function in the Script Include
    ga.addParam('sysparm_item', sysId);  // Pass the sys_id of the record to the server-side

    // Execute the GlideAjax call
    ga.getXMLAnswer(function(response) {
        var owners = response.responseXML.documentElement.getAttribute("answer");
        console.log("Comma-separated Owners: " + owners);

        // Do something with the owners (comma-separated list)
        // Example: display in an alert
        alert('Owners: ' + owners);
    });
}

// Trigger the fetchOwners function when needed
fetchOwners();

 

Thumsup if it works 🙂


*************************************************************************************************************
If my response proves useful, please indicate its helpfulness by selecting "Accept as Solution" and " Helpful." This action benefits both the community and me.

*************************************************************************************************************

 

 

Regards

Shaqeel

 

 


***********************************************************************************************************************
If my response proves useful, please indicate its helpfulness by selecting "Accept as Solution" and " Helpful." This action benefits both the community and me.

***********************************************************************************************************************





Regards

Shaqeel