How to iterate through an array and run rest message for each array item
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-11-2020 02:05 PM
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);
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-11-2020 03:14 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-12-2020 06:03 AM
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

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-12-2020 06:26 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-12-2020 10:37 AM
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);