The CreatorCon Call for Content is officially open! Get started here.

How to get the values in an Object scripting help

gjz
Mega Sage

I need help creating a script that will allow me to get the values of the fields in an object.  I've see a lot of examples, posts, videos, but unfortunately they are all single row, single value examples and they don't work for my needs.

 

In a custom Flow action, I return data from Active Directory in an Object that is defined like this:

gjz_0-1758583274496.png

 I need to loop through the object and get the values for the individual fields for further processing, but I don't know how.

 

So far, this is all I'm able to do:

var myObject = <same as the screen shot above - which isn't all the data>;

var objectLength = Object.keys(myObject.inactiveusers).length; // This does recognize how many rows are in the object.

 

for (var key in myObject.inactiveusers) {

    gs.print(key + '  ' + myObject.inactiveusers[key]); 

)

 

The above for loop produces

0 [object Object]

1 [object Object] and so on.

 

I tried this:

for (var key in MyObject.inactiveusers) {

   gs.print(key + ' ' + myObject.inactiveusers.displayname[key]);

 

and got an error: 

Script execution error: Script Identifier: null.null.script, Error Description: Cannot read property "0" from undefined, Script ES Level: 0

 

What syntax will allow me to loop through each one and get individual values?  Does anyone know where I can find documentation on Object and how to use it (for more than one row and more than one field)?

5 REPLIES 5

Ankur Bawiskar
Tera Patron
Tera Patron

@gjz 

it's an array of json objects so you can use script step to parse that and extract

share that sample JSON here so that we can share the script.

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