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 iterate array using for loop and pass each value in a object

Priyadharashin1
Tera Contributor

Hello

 

I have the below script to iterate over an array and pass it to an object. But outside the for loop , I get a single value alone

Please provide some suggestion.

 

var id = ['0beb47131b78d910ceaa20e23b4bcb24', '0e3b835b1b38d910ceaa20e23b4bcbda'];

var name = [];
for (var i in id) {
    var gr = new GlideRecord("table");
    gr.get(id[i]);
    name = gr.u_name.toString();
    obj = {
        txt: name
    };

     gs.info('output 1: ' + id[i] + name);
}
//  return name;
gs.info('output 2: ' + obj.txt);
 
output:
** Script: output1 : 0beb47131b78d910ceaa20e23b4bcb24 test
*** Script: output1 : 0e3b835b1b38d910ceaa20e23b4bcbda test 2
*** Script: output2 : test 2
5 REPLIES 5

Valmik Patil1
Kilo Sage

Hello @Priyadharashin1 ,

 

Try below updated script , I have also include Id to show sys_id

 

 

var id = ['0beb47131b78d910ceaa20e23b4bcb24', '0e3b835b1b38d910ceaa20e23b4bcbda'];

var result = []; 
for (var i in id) {
    var gr = new GlideRecord("table");
    if (gr.get(id[i])) {  
        var name = gr.u_name.toString(); 
        
        var obj = {
            id: id[i],
            name: name
        };
        
        result.push(obj);

        gs.info('output 1: ID = ' + id[i] + ', Name = ' + name);
    } else {
        gs.info('Record not found for ID: ' + id[i]);
    }
}

gs.info('Final Result: ' + JSON.stringify(result));

 

 

Thanka,

Valmik Patil

Hi Valmik,

 

Thank you for your response.

 

I need the output as an object and not an array. Is this possible?

anshul_goyal
Kilo Sage

Hello @Priyadharashin1,

Try below code:

// Array of record IDs
var ids = ['0beb47131b78d910ceaa20e23b4bcb24', '0e3b835b1b38d910ceaa20e23b4bcbda'];
var results = []; // Array to store objects with txt field

// Loop through each ID to fetch records
for (var i = 0; i < ids.length; i++) {
    var gr = new GlideRecord('table'); // Replace 'table' with the actual table name

    // Check if the record exists and fetch it
    if (gr.get(ids[i])) {
        var name = gr.getValue('u_name'); // Safely get the value of 'u_name'

        // Create an object with the desired structure
        var obj = {
            txt: name
        };

        results.push(obj); // Append the object to the results array
        gs.info('Output 1: ID: ' + ids[i] + ', Name: ' + name);
    } else {
        gs.info('Record not found for ID: ' + ids[i]);
    }
}

// Log the final results array
gs.info('Final Results: ' + JSON.stringify(results));


Please mark my solution as Helpful and Accepted, if it works for you in any way!

Thanks

Runjay Patel
Giga Sage

Hi @Priyadharashin1 ,

 

Use below script.

var id = ['0beb47131b78d910ceaa20e23b4bcb24', '0e3b835b1b38d910ceaa20e23b4bcbda'];
var name = [];
var objArray = []; // Array to hold multiple objects

for (var i = 0; i < id.length; i++) {
    var gr = new GlideRecord("table");
    if (gr.get(id[i])) {
        var currentName = gr.u_name.toString(); // Retrieve name
        name.push(currentName); // Add name to the name array

        // Create object and push to objArray
        var obj = {
            txt: currentName
        };
        objArray.push(obj);

        // Log within the loop
        gs.info('output 1: ID: ' + id[i] + ', Name: ' + currentName);
    }
}

// Log after the loop
gs.info('All names: ' + name.join(', '));
gs.info('All objects: ' + JSON.stringify(objArray));

 

-------------------------------------------------------------------------

If you found my response helpful, please consider selecting "Accept as Solution" and marking it as "Helpful." This not only supports me but also benefits the community.


Regards
Runjay Patel - ServiceNow Solution Architect
YouTube: https://www.youtube.com/@RunjayP
LinkedIn: https://www.linkedin.com/in/runjay

-------------------------------------------------------------------------