How to get the values in an Object scripting help
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
4 hours ago
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:
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)?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 hours ago
@gjz try below code
var object = {
"inactiveusers" : [
{
description : "testing",
displayname : "archimides"
},
{
description : "user 2",
displayname : "newton"
}
]
};
object.inactiveusers.forEach(function(object){
gs.print(object.displayname)
})
var object = { ... }; : create an object named object.
"inactiveusers": [...] : Inside the object, there's a property called inactiveusers which holds an array of objects.
.forEach(function(object){ ... }) : This is a method that loops through each item in the inactiveusers array. For each item (in this case, an object representing a user), it executes the code inside the function.
gs.print(object.displayname) : This line is executed in each loop. It accesses the displayname property of the current user object and prints its value. The gs.print() function is commonly used in environments like ServiceNow for logging output.
If this helped to answer your query, please mark it helpful & accept the solution.
Thanks!
Krishnamohan
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
36m ago
Hi @gjz
Try this code
// Example JSON string (you can fetch this via REST API call too)
var jsonString = 'YOUR_JSON_STRING_HERE';
// Parse JSON
var obj = JSON.parse(jsonString);
// Loop through inactiveusers
for (var i = 0; i < obj.inactiveusers.length; i++) {
var user = obj.inactiveusers[i];
gs.info("Display Name: " + user.displayname);
gs.info("Department: " + user.department);
gs.info("Employee Type: " + user.employeetype);
gs.info("SAM Account: " + user.samaccountname);
gs.info("Last Logon Date: " + user.lastlogondate);
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
a minute ago
Indead of using a for(var key in myObject.inactiveusers) then use a normal for loop. Its an array and for / in handles objects.
for (var i = 0; i < myObject.inactiveusers.length; i++) {
gs.print(key + ' ' + myObject.inactiveusers[i].displayname);
}