- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-01-2020 10:42 AM
Hi,
My requirement is to check with the req_for sys id is available in all the objects element received.
With below script i am able to get the get the array and objects, now i need to search if the sys id is available in the all the objects if it is available i need to display a message saying Sys ID Available if the sys id is not avialable atleast in one or any of the object the i should display a message as Sys ID Not Available
var arr = [];
var serverName = "c56c1c49db7657c4375d5458dc961965";
//var reqFor = "557e1f107bb300002e3ddb30aa4d4dce"; //Need to search with the sys id if it is available in all the object elements
var gr = new GlideRecord('cmdb_rel_ci');
gr.addQuery('child', serverName);
gr.addQuery('parent.sys_class_name', 'u_application');
gr.query();
while(gr.next()){
var obj = {};
obj.name = gr.parent.u_accountable_application_owner.getValue() +',' + gr.parent.u_technical_owner.getValue();
arr.push(obj);
}
gs.print(JSON.stringify(arr));
// Output
//
*** Script: [{"name":"557e1f107bb300002e3ddb30aa4d4dce,c77e93907bb300002e3ddb30aa4d4d0b,557e1f107bb300002e3ddb30aa4d4dce"},{"name":"557e1f107bb300002e3ddb30aa4d4dce,efe87b277bd0d0002e3ddb30aa4d4d68,1f7e9fdc7b7300002e3ddb30aa4d4d3f"}]
If you see the result "557e1f107bb300002e3ddb30aa4d4dce" is available in all the objects in this case i want to display as Sys ID is Avaialble.
Can anyone please help me on this
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-01-2020 12:53 PM
Hi,
I think you should create the object dynamically, or rename the key for the object that you have created because after parsing it will be easier to use it, you can create name + number attribute when you create the obj using the gr.getRowCount() for knowing how much keys are you to create.
Apart from that I let you some code for searching your sys id using the parse function for objects in JS:
this.searchArray(array, key); //This is for calling the function in the same script.
function searchArray (array, key) {
var parsedArray = array.parse();
var splittedObj = parsedArray['name'].split(',');
var found = false;
var index = 0;
while(!found && index != splittedObj.length) {
if(splittedObj[index] == key) {
found = true;
break;
}
index++;
}
if(found) {
gs.print('Sys ID Available');
}else{
gs.print('Sys ID Not Available');
}
}
It's made for one iteration of the object, if you need more, just create a loop inside o call the function into a loop.
If it was helpful, please give some positive feedback.
Thanks,
☆ Community Rising Star 22, 23 & 24 ☆

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-01-2020 11:23 AM
function search(nameKey, myArray){
for (var i=0; i < myArray.length; i++) {
if (myArray[i].name === nameKey) {
return myArray[i];
}
}
}
Something like this should help you. You iterate over the array and look for a property(namekey).
Hope this answer helped or solved your problem. If so, please mark as 'Helpful' or 'Correct'. Thanks!
Alexander
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-01-2020 11:29 AM
Do i just need to add above lines to my code will that works ??/
If you dont mind can you please help me where to add to my given script ?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-01-2020 11:42 AM
So yes, you would have to add the lines of code to your code. And then do this:
search(reqFor,arr);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-01-2020 12:53 PM
Hi,
I think you should create the object dynamically, or rename the key for the object that you have created because after parsing it will be easier to use it, you can create name + number attribute when you create the obj using the gr.getRowCount() for knowing how much keys are you to create.
Apart from that I let you some code for searching your sys id using the parse function for objects in JS:
this.searchArray(array, key); //This is for calling the function in the same script.
function searchArray (array, key) {
var parsedArray = array.parse();
var splittedObj = parsedArray['name'].split(',');
var found = false;
var index = 0;
while(!found && index != splittedObj.length) {
if(splittedObj[index] == key) {
found = true;
break;
}
index++;
}
if(found) {
gs.print('Sys ID Available');
}else{
gs.print('Sys ID Not Available');
}
}
It's made for one iteration of the object, if you need more, just create a loop inside o call the function into a loop.
If it was helpful, please give some positive feedback.
Thanks,
☆ Community Rising Star 22, 23 & 24 ☆