How to get first array element
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-20-2022 12:52 PM
I have the below code and i am unable to get the first array element
var idset = workflow.scratchpad.idvalue;
var idset2 = workflow.scratchpad.idvalue2;
var idset3 = workflow.scratchpad.idvalue3;
var idset4 = workflow.scratchpad.idvalue4;
var idset5 = workflow.scratchpad.idvalue5;
var arr = [];
arr.push(idset, idset2, idset3, idset4, idset5);
for (var i in arr) {
var myObj = arr[i];
var r = new sn_ws.RESTMessageV2('Cisco ISE', 'Get voucher details');
r.setStringParameterNoEscape('id', myObj);
var response = r.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();
var jsonObj = JSON.parse(responseBody);
var UN = jsonObj.GuestUser.guestInfo.userName;
var unarr = [];
unarr.push(UN);
gs.log(unarr+"unarr");
var x = unarr[0].toString();
gs.log(x+"unarrx");
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-20-2022 10:22 PM
I think push accepts multiple parameters
Aman Kumar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-20-2022 10:30 PM
Oh yes. you are correct.
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-20-2022 10:31 PM
Hi,
are you sure the for loop is running 5 times?
update as this
var idset = workflow.scratchpad.idvalue;
var idset2 = workflow.scratchpad.idvalue2;
var idset3 = workflow.scratchpad.idvalue3;
var idset4 = workflow.scratchpad.idvalue4;
var idset5 = workflow.scratchpad.idvalue5;
var arr = [];
var unarr = [];
arr.push(idset, idset2, idset3, idset4, idset5);
for (var i in arr) {
var myObj = arr[i];
var r = new sn_ws.RESTMessageV2('Cisco ISE', 'Get voucher details');
r.setStringParameterNoEscape('id', myObj);
var response = r.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();
var jsonObj = JSON.parse(responseBody);
var UN = jsonObj.GuestUser.guestInfo.userName;
unarr.push(UN.toString());
}
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-20-2022 10:41 PM
I am still not getting the response.
I can have from 1 upto 5 ids. In case i have only one myobj is returning undefined. I have put an if for undefined. However it is still not working. I am not getting the log for response also
var idset = workflow.scratchpad.idvalue;
var idset2 = workflow.scratchpad.idvalue2;
var idset3 = workflow.scratchpad.idvalue3;
var idset4 = workflow.scratchpad.idvalue4;
var idset5 = workflow.scratchpad.idvalue5;
var arr = [idset, idset2, idset3, idset4, idset5];
gs.log("arr" + arr);
for (var i in arr) {
var myObj = arr[i];
gs.info("myObj"+myObj);
var r = new sn_ws.RESTMessageV2('Cisco ISE', 'Get voucher details');
if(myobj!="undefined"){
r.setStringParameterNoEscape('id', myObj);
var response = r.execute();
var responseBody = response.getBody();
gs.log("response" + responseBody);
var httpStatus = response.getStatusCode();
gs.log("httpStatus" + httpStatus);
var jsonObj = JSON.parse(responseBody);
}
var UN = jsonObj.GuestUser.guestInfo.userName;
var unarr = [];
unarr.push(UN);
gs.log(unarr + "unarr");
var x = unarr[0].toString();
gs.log(x + "unarrx");
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-20-2022 10:43 PM
Check your scratchpad variables values, are they valid?
gs.log("arr" + arr);// what does this returns?
Aman Kumar