Retrieving Variables Values : RMPApplication.get() vs "[[ ]]"
Note: This is only valid for the Application Runner engine.
These syntaxes are used to retrieve variable values, and both have a specific behavior. Let's see when and why you should or shouldn't use them.
Let's take 4 examples of widgets:
- A text input widget, variable firstname.
- A number input widget, variable amount.
- A multi selection list widget, whose value variable is product_ids.
- An array widget, whose variable is items_t
RMPApplication.get() syntax
How to use it
Technically, you expect to have firstname being a string, amount being a number, product_ids being an array and items_t being a json.
You have to keep in mind that RMPApplication.get("xxx")
will always return a string, so you will have to cast it to a number, array or json in your scripts:
RMPApplication.get("firstname"); //returns a string
parseFloat(RMPApplication.get("amount")); //returns a float/number
JSON.parse(RMPApplication.get("product_ids")); //returns an array
JSON.parse(RMPApplication.get("items_t")); //returns a json
Where to use it
- in js widgets
- in prelaunch scripts on buttons
- in .js files
Where/When you shouldn't use it
Don't use this syntax in JS dynamic rules on widgets, because it will be executed only once (see section on "[[ ]]"
syntax for more details).
"[[ ]]" syntax
How to use it
Technically [[myvar]]
will be replaced by the characters as value of myvar.
If you expect a string variable, then you must use "[[ ]]"
syntax. Ex:
"[[firstname]]"; //returns "John", if you've entered John in the text input => it's a string
If you expect a variable that is NOT a string (it's a number, or an array, or a json), you must use [[ ]]
syntax instead (note that there is no double quotes " "). Ex:
[[amount]]; //returns 1.234 if you've entered 1.234 in the number widget => it's a number
[[product_ids]]; //returns an array
[[items_t]]; //returns a json
If you don't follow above rules:
[[firstname]]; //returns John. JS engine will understand you try to call John variable and will lead to an error.
"[[amount]]"; //returns "1.234". You'll have to use parseFloat() to get a number
"[[product_ids]]"; //this is wrong, it returns an array surrounded by double quotes
"[[items_t]]"; //this is wrong, it returns a json surrounded by double quotes
Where to use it
Keep "[[ ]]"
syntax for the dynamic rules on widgets: it not only means you intend to retrieve a variable value, but also you are listening to this variable.
Where/When you shouldn't use it
- Special characters
If the RunMyProcess variable is a string and contains any special characters (ex: carriage return), you should't use "[[ ]]"
syntax since you'll get a JS execution error.
Ex: if you configure a multi-rows text input address, and enter a value with carriage returns: Street\n\City\nCountry :
"[[address]]"; //will be replaced by
"Street
City
Country"; //and this will lead to a JS execution error
So in that case, do rather use:
RMPApplication.get("address"); //returns "Street\n\City\nCountry"
- In a definition of a function
If you intend to create a function that calls firstname variable and display it:
function display_firstname() {
alert("[[firstname]]"); //Wrong!
}
If you call the function above, you will get the value firstname had at the moment you defined the function. So you must use this definition instead:
function display_firstname() {
alert(RMPApplication.get("firstname")); //Right!
}
Please give details of the problem