Having Issues Looping Through Values in a Workflow Run Script Action

jmiskey
Kilo Sage

I am trying to loop through a bunch of environments in a Run Script action in one of my workflows, and am experiencing some really weird behavior.  If I hard-code the value I want to loop through, everything works like it is supposed to.  Here is the beginning of the code with the original value to loop through hard-coded:

 

var envs = 'DEV,TRN';
gs.log('ENVS: ' + envs,'JOE0');
var indEnv = '';
var ad_grps = [];
//loop through all environments
splitEnvs = envs.split(',');
gs.log('Len: ' + splitEnvs.length,'JOE1');
for(i=0;i<splitEnvs.length;i++){
	indEnv = splitEnvs[i];

When I run the code, my first two log statements correctly return the following values:

ENVS: DEV,TRN

Len: 2

 

However, I need to pull the environments from a string variable on my Catalog Item.  So, my actual first line of code cannot be hard-coded, but needs to pull that value like this:

var envs = current.variables.all_cap_environments;
gs.log('ENVS: ' + envs,'JOE0');
var indEnv = '';
var ad_grps = [];
//loop through all environments
splitEnvs = envs.split(',');
gs.log('Len: ' + splitEnvs.length,'JOE1');
for(i=0;i<splitEnvs.length;i++){
	indEnv = splitEnvs[i];

When I run that code and view the log files, it looks like it correctly pulls the value from the string, but I get an error when trying to split it on the comma.  The log file results look like this:

ENVS: DEV,TRN

Len: Undefined

 

This error is obviously keeping my code from running correctly.

What do I need to do to get this to work correctly?

 

I even tried in my second code to declare envs as an array first, like this:

var envs = [];
envs = current.variables.all_cap_environments;

but that made no difference.  I still got the "undefined" error.

1 ACCEPTED SOLUTION

Prince Arora
Tera Sage
Tera Sage

@jmiskey ,

Could you please try this, just add a double quotes in the end to make it string:

var envs = current.variables.all_cap_environments + "";

Please try and let me know if it works or not

if it will not work, please let me know what type of variable it is.

If my answer solved your issue, please mark my answer as Correct & 👍Helpful based on the Impact.


View solution in original post

6 REPLIES 6

OK, so that worked too.  It appears that the issue was my initial declaration of:

var envs = current.variables.all_cap_environments;

needed to look like:

var envs = current.variables.all_cap_environments + "";

or

var envs = current.variables.all_cap_environments.toString();

 

Seeing how it is a Single Line Text variable to start with, I am confused why the

+ ""

or

.toString()

becomes necessary.  Maybe it doesn't translate over and needs to be declared explicitly?

jmiskey
Kilo Sage

Thank you both Prince_Arora and Brad Bowman for both coming up with the solution and explanation for me!

I have a much better understanding now of this!