Need Help with Using KM Variables in JavaScript

I'm really a fan of the drop-down validation of the javascript. However, I'm kind of stuck around passing arguments with a dynamic km variable.. if anyone has experience with Javascript or passing km variables, could you help me understand why the first code works, but the second doesn't? Thank you guys so much

// Get the object to work with.
var objSelect = document.getElementById("web-selection_"+document.kmvar.vZEROIndex+"_");

// Set the value of the object and activate the onchange() function.
setSelectedValue(objSelect, "ActualDropdownItem");

function setSelectedValue(object, value) {
for (var i = 0; i < object.options.length; i++) {
if (object.options[i].text === value) {
object.options[i].selected = true;
// object.onchange();
return;
} } }

var vDropdownInput = document.kmvar.vDropdownInput;

// Get the object to work with.
var objSelect = document.getElementById("web-selection_"+document.kmvar.vZEROIndex+"_");

// Set the value of the object and activate the onchange() function.
setSelectedValue(objSelect, vDropdownInput);

function setSelectedValue(object, value) {
for (var i = 0; i < object.options.length; i++) {
if (object.options[i].text === value) {
object.options[i].selected = true;
// object.onchange();
return;
} } }

@jayknowstheway, I have moved your post to a new topic since you asked a new question. Please follow this policy in the future.

Thanks.

1 Like

Thanks for the tip JMichaelTX I'll do that. I can see how the question is a bit independent from the JS fill dropdown post!

I've still been stumped by this but found this link in the forum in case anyone might have an idea on a solution:

Is the problem that km variables are "read-only" to javascript? I am trying to assign a variable from document.kmvar to a js variable in a function argument, and it looks like it's not even read. But kmvars can be read outside of an argument in a function? Sorry if it's a js noob question, I'm confused!

Yes, when using JavaScript in Browser.
However, JavaScript for Automation (JXA) can both read/write KM variables.

If you want to set a KM variable from data obtained using JavaScript in Browser, then you need to return the JavaScript results in the Execute a JavaScript in Browser action, like this:

image


Note: In the above JavaScript example, I did not do the best job of ensuring proper scope of data. By entering the statements directly, it places them in the global JavaScript space.

It is better to use functions, like this:

'use strict';
(function myMain() {	// this will auto-run when script is executed

var scriptResults = "TBD"

//--- YOUR CODE HERE ---

return scriptResults; // <==== This return returns the value of scriptResults to the KM Action

}	// END of function myMain()
)();	// autorun

Hey @JMichaelTX thank you, this answer has given me more ideas on scope that are super! I wasn't sure if I was missing something though..

I'm not sure I made myself clear in that I'm obtaining variable information from an excel spreadsheet. When I return the kmvariable data via Execute Javascript as in your example, the variable receives the data. However, inside a parameter in a function, it seems to not "see" the kmvariable!

Maybe I'm missing something obvious, but somehow the scope of the kmvariable is inaccessible in a function argument, whereas the function will return the kmvariable as you mention. I don't know much about JS, but does this in any way give you more information?

If you are running the JavaScript with Excel, then you should be using Execute a JavaScript For Automation action, NOT JavaScript in Browser Action. There are clear examples in that reference that show how to use KM variables.

If this does not clear things up for you, then please post your entire Macro and script so we can evaluate. See How to post/upload your script/macro.

Hey @JMichaelTX thanks again so much and I'm really sorry if I'm confusing you with this, I realize I must be because I tested this script on websites with dropdowns, and it seems to work just fine. For some reason, the website I'm trying to use this on will not take a variable as an argument, as I show in the macro below. Unfortunately, I can't share the actual site as you'd need credentials, but dropdown like the one I'm trying to use this on have user-specific options, they are not generalized. If you happen to have ideas as to workarounds to expose a variable in a more visible state in JS, or any other things to try, please let me know! Otherwise, I might have to bring this up with the site developers because this has been such a puzzle!! Thank you again so much for your help and kindness!

EXCEL VARIABLE TO BROWSER DROPDOWN SELECT JAVASCRIPT

EXCEL VARIABLE TO BROWSER DROPDOWN SELECT JAVASCRIPT copy.kmmacros (5.8 KB)

I cannot duplicate this behavior you see. What I see is that the JavaScript is reading/getting the KM Variable just fine:

Screenshot from the Chrome Dev Tools:

scriptResults = document.kmvar.vDropdownInput;

image

I didn't change your script, but I did put it in a function and add a few lines at the top.
Here's the script I ran/debugged:

'use strict';
(function myMain() {      // this will auto-run when script is executed

var scriptResults = 'TBD'

debugger;

scriptResults = document.kmvar.vDropdownInput;

// variable to enter into dropdown
var vDropdownInput = ("\""+document.kmvar.vDropdownInput+"\"");

// variable to get the Element by Name
var objSelect = document.getElementById("web-selection_"+document.kmvar.vZEROIndex+"_");


// function to do the work
setSelectedValue(objSelect, vDropdownInput);


// function definition
function setSelectedValue(object, value) {
    for (var i = 0; i < object.options.length; i++) {
        if (object.options[i].text === value) {
            object.options[i].selected = true;
            // object.onchange();
         //   vDropdownInput;
            return;

    }
}
    // Throw exception if option `value` not found.
    var tag = object.nodeName;
    var str = value;

    return str;
}

return scriptResults;
}  // END of function myMain()
)();  // autorun

I also run this simple test, which works fine:

var kmVarStr = document.kmvar.vDropdownInput;
alert('kmVar: ' + kmVarStr);
kmVarStr;

Since I don't have access to your web page, I can't really debug, but this statement does not look correct to me:

// variable to get the Element by Name
var objSelect = document.getElementById("web-selection_"+document.kmvar.vZEROIndex+"_");

First of all, you do NOT set a KM Variable named "vZEROIndex", so this statement would fail just based on that.

If you can't share the URL, can you upload the HTML code (in a Code Block) of the main part of the web page you are trying to use.

Let me be clear: The issue has NOTHING to do with getting KM Variables into JavaScript. That is clearly demostrated by my script.

I'd suggest that you use the Chrome debugger. Open the web page of interest, then open the Chrome Dev Tools (⌘⌥I) for that page, reselect the web page, and run my script which has the debugger enabled. Then you can step through the code to see where it fails.

Example of Chrome Debugger

1 Like

This has been one of the best answers, this led me straight to the solution. That debugger tool is fantastic!! The problem was along the lines of including quotes. JS already included the quotes in the variable, so adding them again caused an error, whereas explicitly stating the dropdown item in quotes did not add more quotations.

Sorry to have bothered you about a non-KM problem, though this is really a fantastic thing to validate JS and will save days of fumbling around. THANK YOU!!!

1 Like