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

If the status code is undefined then it is obviously not even attempting to execute the API, if it had attempted and failed because of wrong formatting then it would have at least returned a status code.

When you run the code do the system logs show any errors?

I'm pretty out of ideas if there are no log errors. 

Thanks, Dan

Hey Dan,

How can I add a sleep or a delay between each object in the array when sending the rest message.

It appears that this is running the rest api multiple times which I am expecting however I need to wait for the first one to finish before executing the next one, etc.

I tried adding gs.sleep(15000); //15 seconds but its not working

Or how can I wait until the first response received, then go to the next array and run that, wait for response, go to next etc...

Thank you

 

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


// Loop through the array of accounts
for (var i = 0; i < array.length; i++) {
gs.sleep(15000);
var samAccount = array[i];
gs.sleep(15000);
gs.log("OffboardDisableAD.disableAD Processing SAM Account Start: " + samAccount);

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

Hi,

I'm curious to know what is the reason for needing to wait for the API response before processing the next samAccount?

Thanks,
Dan

Hi Dan,

 

The developer who created the powershell command asked me to put a timer or wait in there.

I am seeing the unique endpoints and calls being made, however, the Accounts are not getting set to disabled.

If I run this with just 1 user then it works, but not soo with multiple rest calls.

I just don't get it, it would appear all of my code is working as expected.

Do I have to wait for all to return or something?

var obj2 = JSON.parse(returnedData);

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

 

 

*** Script: OffboardDisableAD.disableAD Processing SAM Account Start: b2delete
*** Script: OffboardDisableAD.disableAD function started processing samAccountsb2delete
*** Script: OffboardDisableAD.disableAD Endpoint=https://host/sshadmin/api/Home/ServiceNowCommandGet?command=Disable-User&userName=serviceNow&passWor...
*** Script: OffboardDisableAD.disableAD - Status Code: success
*** Script: OffboardDisableAD.disableAD - Response Body: {“data”:[“User not found or user has already been disabled”],“status”:“success”}
*** Script: OffboardDisableAD.disableAD Processing SAM Account Start: j1delete
*** Script: OffboardDisableAD.disableAD function started processing samAccountsj1delete
*** Script: OffboardDisableAD.disableAD Endpoint=https://host/sshadmin/api/Home/ServiceNowCommandGet?command=Disable-User&userName=serviceNow&passWor...
*** Script: OffboardDisableAD.disableAD - Status Code: success
*** Script: OffboardDisableAD.disableAD - Response Body: {“data”:[“User not found or user has already been disabled”],“status”:“success”}

Do I need to do or how do I do a Rest Promise to wait for all to process?

Not sure if that is what I am missing