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

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.


Wow, that worked!  Thank you very much!

 

Can you explain why we needed to do that?

In case you need more information, it is a Single Line Text variable that is being set via a Catalog Client Script that runs upon Form Submission, and that part of the code that populates it looks like this:

	//build string for all CAP Environments selected
	var cap_env = '';
	
	//loop through all 5 environment check boxes
	if(g_form.getValue("cap_dev_environment") == 'true'){
		cap_env += 'DEV,';
	}
	if(g_form.getValue("cap_trn_environment") == 'true'){
		cap_env += 'TRN,';
	}

	//remove last comma from string and populate variable
	cap_env = cap_env.substring(0, cap_env.length-1);
	g_form.setValue("all_cap_environments", cap_env);	

I just want to understand it better, so I can avoid this hang-ups in the future.

@jmiskey ,

"current.variables.all_cap_environments" returned an object value, so we simply added the "" at the end to make it a string.
So previously you couldn't use the string functions (split,indexOf) on the Object type variable, but once we changed it to string, it worked!

Please Accept the solution if its working for you!

Brad Bowman
Kilo Patron
Kilo Patron

Hi Joe,

As a matter of best coding practice, you should always declare and initialize variables, even though JavaScript doesn't always require this.  Although the variable is a string, it can't hurt to force the assignment to a string if the split is still not working, so your script would look more like this - with an added log to show you what splitEnvs contains:

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