How to use the Execute JavaScript Activity to pass Variables

Hi!

I am new to using JavaScript and am curious how to pass variables to the JS function. I appreciate any guidance you can offer. Thank you in advance!

My process reads data from a spreadsheet and assigns values from the spreadsheet to variables. Please see the example below.

Below is the JS function I’m using so far. The browser opens; however, I am receiving an error that states “Variable ‘siteurl’ is not defined.” It asks me to create the variable, but the variable is created earlier in the process.

function logIntoSystem() {

window.open(siteurl);
document.addEventListener("DOMContentLoaded", ready);
document.getElementById("myform:sbmt_consult").click();
document.getElementById("myform:id").value = siteun;
document.getElementById("myform:fein").value = sitepw;
document.getElementById("myform:sbmt_cont").click();

}

@BrandonTerry, Are you using “Execute JavaScript” from the Web Browser group or “Execute JS code” from the Programming group?

If you are using “Execute JavaScript” you can transfer the variables using the “Input parameter” option. You can read more here: Link

If you are using “Execute JS code” please be sure that variables are set as Global. You can see a note here: Link

image

1 Like

Hi, @dm.ferreyra !

Thank you for your reply! I am using “Execute Javascript” from the Web Browser group for this. I had a feeling I could use the Input Parameters to pass the variables on to the .js file. I’m a little unsure how to do this. It would need to be in JSON format, right? I need to pass 3 variables to the .js file–siteurl, siteun, and sitepw. What would that look like in the parameters? Would I need to put the variables and their values in JSON format? If so, that’s not going to work for me because the values change as the process continues. Again, thank you in advance! Have an awesome day!

Brandon

Hi @BrandonTerry ,

It could be a JSON object or an array. Here is an example of how you can define the “Input parameter” value to use a JSON object with the variables you have defined:

“Execute JavaScript” properties:

Then inside your JavaScript code you reference each value by using for instance:

function myFunction(a) {
return a["siteurl"];
}

Hi, @BrandonTerry Please let us know if the solution presented above by @dm.ferreyra worked.

@dm.ferreyra

My apologies for the late reply. We had a company-wide meeting last week, so unfortunately there wasn’t any time to try this out or get back to you.

Your response is so awesome–very detailed! I truly appreciate that! Please allow me to compare what I did on my end in relation to what you specified. Also, please note that I only tested one piece of this.

What am I doing wrong? Thank you!

@BrandonTerry, because you are passing an object you should reference the siteurl in the following way: window.open(a["siteurl"])

1 Like