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 09:53 PM
You are in global/scoped app?
Aman Kumar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-20-2022 09:55 PM
global

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-20-2022 10:07 PM
Are you getting some sort of error, I tried the same in background script, it works fine.
Try to print UN, and also verify if you are getting the response body correctly, and prefer using gs.info instead of gs.log
Aman Kumar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-20-2022 10:10 PM
I am not getting the response body as well using this script but working fine in Backgrund script. Is there an alternate way of getting this?
I can send upto 5 id and i have to get 5 response and from the response the i need to retrieve the username and password.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-20-2022 10:16 PM
Hi,
you are declaring array in wrong manner
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 = [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");
}
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader