Get a first look at what's coming. The Developer Passport Australia Release Preview kicks off March 12. Dive in! 

How to iterate through an array and run rest message for each array item

triciav
Kilo Sage

I have a run script in my workflow that is calling a script include to run a rest message

My run script is as such:

var array = (current.variables.sam_accountname.toString()).split(",");
for (var i = 0; i < array.length; i++){
var samAccount = array[i];

gs.log("This is the SamAccounts "+array[i]);

This is the SamAccounts ttest3delete
This is the SamAccounts ttest4delete

var obj = new OffboardDisableAD();//name of script include

var returnedData = obj.disableAD(samAccount);//function call *This is were I need to loop through and pass each samAccount to the function so it iterates and runs for each array item

var json = new global.JSON();

//var returnedData = obj.DisabledAccounts('disabledAccountsData');
var obj2 = JSON.parse(returnedData);

workflow.scratchpad.statusad = obj2.status;
workflow.scratchpad.msgad = obj2.data;
}

This is the code in my script include: 
How do I get this to run for each array samaccount

gs.log("THIS IS THE Script include samAccounts "+samAccount[i]);

THIS IS the ScriptInclude users ttest3delete,ttest4delete


var request = new sn_ws.RESTMessageV2();
//devca19
//intranetappca02
request.setEndpoint('https://EP/sshadmin/api/Home/ServiceNowCommandGet?command=' + this.command + '&userName=' + this.user + '&passWord=' + this.password + '&userNameForCommand=' + samAccount);

19 REPLIES 19

Dan Ellis
Mega Sage

Looks like you are pretty much there, I would toString "samAccount" when making the call to the script include to ensure we have the correct value and not an old reference since we are in a loop.

Workflow Script

var array = (current.variables.sam_accountname.toString()).split(",");

// Loop through the array of accounts
for (var i = 0; i < array.length; i++){
    var samAccount = array[i];

    gs.log("Processing SAM Account: " + array[i]);

    var returnedData = new OffboardDisableAD().disableAD(samAccount.toString());

    var obj2 = JSON.parse(returnedData);

    workflow.scratchpad.statusad = obj2.status;
    workflow.scratchpad.msgad = obj2.data;
}

Script Include

var OffboardDisableAD = Class.create();
OffboardDisableAD.prototype = {	
    initialize: function() {
        command = '';
        user = '';
        password = '';
    },

    disableAD: function(samAccount) {
        gs.log("OffboardDisableAD.disableAD - Processing SAM Account: ' + samAccount);

        var request = new sn_ws.RESTMessageV2();
        request.setEndpoint('https://EP/sshadmin/api/Home/ServiceNowCommandGet?command=' + this.command + '&userName=' + this.user + '&passWord=' + this.password + '&userNameForCommand=' + samAccount);
        request.setHttpMethod('POST');

        var response = request.execute();
        var statusCode = response.getStatusCode();
        var responseBody = response.getBody();

        return responseBody;
    },

    type: 'OffboardDisableAD'
};

 

Let me know if this helps or if you need anything else.

 

Thanks, Dan

Hi Dan,

I am seeing the accounts in my logs OffboardDisableAD.disableAD - Processing SAM Account: ttest3delete

But it does not look like it is passing those to the rest.endpoint 

Offboard Request body = undefined

 

Okay, if you use gs.log() to print the value of "statusCode" and the value of "responseBody" what does it say?

gs.log("OffboardDisableAD.disableAD - Status Code: ' + statusCode);
gs.log("OffboardDisableAD.disableAD - Response Body: ' + JSON.stringify(responseBody));

Also, the API that you are calling, is it expecting the samAccount as a URL parameter (what you are currently doing) or does it need to be sent as JSON in the request body like this?

var requestBody = {"samAccount": samAccount};

request.setHttpMethod('POST');
request.setRequestBody(requestBody);

Thanks,
Dan

Hey Dan,

It is expecting URL not JSON

OffboardDisableAD.disableAD - Status Code: undefined

Offboard Request body = undefined

 

OffboardDisableAD.disableAD - Processing SAM Account: j1delete

request.setEndpoint('https://hostname/sshadmin/api/Home/ServiceNowCommandGet?command=' + this.command + '&userName=' + this.user + '&passWord=' + this.password + '&userNameForCommand=' + samAccount);