The CreatorCon Call for Content is officially open! Get started here.

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

It works when there is only 1 user but its not working if there is more than 1

I am logging everything and see the multiple logs for each samaccount

the response back is a false success because if I go to ldap the accounts are still enabled

so it does not appear to be running the rest twice for each user although my logging shows that it is.

I don't know how to use Postman

SO breakdown:

This is my runscript

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("OffboardDisableAD.disableAD Processing SAM Account Start: " + samAccount);

var obj = new DeprovisionDisableAD(); //name of script include
var returnedData = obj.disableAD(samAccount); //function call


gs.log("OffboardDisableAD.disableAD Processing SAM Account Return: " + returnedData + " samaccount to string " + samAccount);

var obj2 = JSON.parse(returnedData);

LOG Results

OffboardDisableAD.disableAD Processing SAM Account Start: j1delete

OffboardDisableAD.disableAD Processing SAM Account Start: b2delete

 

This is my Script Include

var DeprovisionDisableAD = Class.create();
DeprovisionDisableAD.prototype = {
initialize: function() {
this.midserver = 'MIDSERVER-DEV';
// this.midserver = "MIDSERVERNY-01";
this.user = 'serviceNow';
this.password = gs.getProperty('glide.ps.api');
this.command = 'Disable-User';
},

disableAD: function(samAccount) {

gs.log("OffboardDisableAD.disableAD function started processing samAccounts" + samAccount);

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

request.setHttpMethod('get');
request.setMIDServer(this.midserver);
request.setRequestHeader("Accept", "application/json");
request.setRequestHeader('Content-Type', 'application/json');
request.setRequestBody();

var response = request.execute();
//response.waitForResponse(60);
var responseBody = response.getBody();

//Parse the response

var json = new global.JSON();
var obj = JSON.decode(responseBody);
var response2 = JSON.parse(responseBody);
var disableAddata = JSON.stringify(response2);


// return disableAddata;
var status = response2.status;
var msg = response2.data;

gs.log("OffboardDisableAD.disableAD - Status Code: " + response2.status);
gs.log("OffboardDisableAD.disableAD - Response Body: " + disableAddata);

return disableAddata;


},
type: 'DeprovisionDisableAD'
};

 

These are the Logs:

OffboardDisableAD.disableAD function started processing samAccounts j1delete

OffboardDisableAD.disableAD function started processing samAccounts b2delete

OffboardDisableAD.disableAD - Response Body: {"data":["User not found or user has already been disabled"],"status":"success"} samaccount j1delete

OffboardDisableAD.disableAD - Response Body: {"data":["User not found or user has already been disabled"],"status":"success"}samaccount b2delete

 

The Response is not correct. So it appears the the samaccounts are not being passed in.

If I look in AD the samaccounts are still active and not disabled.

I have no idea what is wrong.

Any ideas or suggestions would be greatly appreciated.

Thank you 

Tricia

 

 

Can you add below lines after var responseBody = response.getBody(); and see what status code it returns. 

var statusCode = responseBody.getStatusCode();
gs.log("STATUS CODE is - "+ statusCode);

 

Regards,
Muhammad

Can you add below lines after var responseBody = response.getBody(); and see what status code it returns. 

var statusCode = responseBody.getStatusCode();
gs.log("STATUS CODE is - "+ statusCode);
Regards,
Muhammad

My Code is actually working, its the endpoint that is having the issue

 

Thank you for assistance

Sounds Good!

 

if you think there is any response that helps you please mark that helpful or accepted so that this thread can be closed. Happy Coding!

 

Thanks & Regards,

Sharjeel

Regards,
Muhammad