How to get first array element

SNOW39
Tera Expert

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");
    
}

26 REPLIES 26

Hi,

what happens if you hard-coded the id and check the API response?

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

no . What i understand is it is not entering the if loop..

it is unable to understand "undefined"

Hi,

try this syntax

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];
var unarr = [];

for (var i=0;i<arr.length;i++) {
    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;
     unarr.push(UN.toString());
    gs.log(unarr + "unarr");   
    gs.log(unarr[0]+ "unarrx");

}

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

This is giving me response but not unarr[] elements.. why?

 

 

try {
    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 myid = myObj.toString();
        if (myid != '' && myid) {

            var r = new sn_ws.RESTMessageV2('Cisco ISE', 'Get voucher details');
            r.setStringParameterNoEscape('id', myid);
            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.toString());
    current.variables.username1 = unarr[0];
    current.variables.username2 = unarr[1];
    current.variables.username3 = unarr[2];
    current.variables.username4 = unarr[3];
    current.variables.username5 = unarr[4];

} catch (e) {
    gs.info("guest wifi" + e);
}

Hi,

So using above the API is giving correct response

your next task is to check unarr

your script is wrong; I made the changes in bold

try {
    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];
    var unarr = [];

    for (var i in arr) {
        var myObj = arr[i];
        var myid = myObj.toString();
        if (myid != '' && myid) {

            var r = new sn_ws.RESTMessageV2('Cisco ISE', 'Get voucher details');
            r.setStringParameterNoEscape('id', myid);
            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());
        }
    }

    current.variables.username1 = unarr[0];
    current.variables.username2 = unarr[1];
    current.variables.username3 = unarr[2];
    current.variables.username4 = unarr[3];
    current.variables.username5 = unarr[4];

} catch (e) {
    gs.info("guest wifi" + e);
}

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader