How to iterate the array find some elements & pass their associated values to store in new variable
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-19-2024 10:04 PM - edited 02-19-2024 10:09 PM
Hello @Ankur Bawiskar @Chuck Tomasi @Pradeep Sharma
We have an array retrieved from the "cmdb_ci_rel" table where the array contains the group's and their associated sys-id's but we are unable to iterate the array to find the values and then extract their sys_id to store these values in new variables which later needs to be passed in to another function
Sample array : [{"sys_id":"sys-id of the group","name":"group name "},{"sys_id":"sys-id of the group","name":"group name"},{"sys_id":"sys-id of the group","name":"group name"},{"sys_id":"sys-id of the group","name":"group name"}];
Can you suggest how we can interate , the group names are based on the CI in addition to certain suffixes of the groups where suffix could be something like approver , manager etc
Regards
Rochel
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-19-2024 10:24 PM
Hi @Rochel hans,
Where are you doing the iteration? A script? Flow designer? A workflow?
Also, can you provide what you have done and what went wrong?
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-19-2024 10:50 PM - edited 02-19-2024 10:51 PM
hello James ,
Its in the script include , there is already a function where some of the parameters like CI value , its owners are passed (the owners are the manager and deputy manager of these relationship groups ). There are 4 different types of groups .
We are able to generate the array with sys_id , name of the group but we also need to check in the array if a certain group exists that associated sys-id must be retrieved and stored in a new variable
code :
var busId = "sys_id of the CI";
var busServiceGroups = new GlideRecord('cmdb_rel_group');
busServiceGroups.addEncodedQuery("ci.sys_class_name=cmdb_ci_service^ci=" + busId);
busServiceGroups.query();
var groupDetails = []; // creating empty array to store group values
while (busServiceGroups.next()) {
var groupSysId = busServiceGroups.getValue('group');
var groupName = busServiceGroups.group.getDisplayValue();
//groupInfo is the object with 2 parameters , in the groupDetails array there are 4 objects in the output as there only 4 rel groups for this CI passed.
var groupInfo = {
sys_id: groupSysId,
name: groupName
};
groupDetails.push(groupInfo);
var newArray = JSON.stringify(groupDetails);
}
gs.info("The new array values are " + newArray);
the output is as i mentioned above
what we need now is : for example if the array has lets approver group in it , it should pass the sys_id of the approver group and i need to save it a new variable.
what we tried already is to use array.indexOf , include functions but its not passing the values like we need since its returns only the index.
Regards
Rochel
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-20-2024 01:12 AM - edited 02-20-2024 01:12 AM
Thanks @Rochel hans,
Sorry, I did not get the part where you said 'what we need now is : for example if the array has lets approver group in it , it should pass the sys_id of the approver group and i need to save it a new variable.'
But I am assuming that you want to save the sys_id of a group to another variable that passes a condition? If so, can't you just have an if statement within the while loop?
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-19-2024 10:47 PM
Hi @Rochel hans
To iterate over the array and extract the sys_id values for each group, you can use a loop to go through each element in the array.
Sample.
var arr = [{"sys_id":"sys-id of the group","name":"group name "},{"sys_id":"sys-id of the group","name":"group name"},{"sys_id":"sys-id of the group","name":"group name"},{"sys_id":"sys-id of the group","name":"group name"}];
for (var i in arr){
gs.info(arr[i].sys_id);
gs.info(arr[i].name);
}
Cheers,
Tai Vu