Add IF based on fields

Hi everyone,

I'm trying to add IF based on fields, but I'm missing something. This is the JS that I'm trying to use:

{{ if ({{ select6.value }} === "Dubai"){
return "2%";
} else if(select6.value === "Abu Dhabi"){
return "4%";
} else if(select6.value === "Sharjah"){
return "1%";
} else {
return ""; // return empty string if none of the conditions match
}

Where is this code running from? A JS query, a transformer or in a component property? Unfortunately, the rules are different for each one.

If you are using a JS query, you can get rid of all of the handlebars: {{ }}. A JS query runs fully within the context of the Retool development scope, meaning that it knows what select6.value. Never use handlebars in a JS Query.

If you are doing this in a transformer (either a separate JS transformer or within a query) you only need to use handlebars to reference Retool components and other values, So you do need {{select6.value}} but if is part of javascript so it does not need it.

Code within a component property must be within a handlebars set and this code has to fit onto what JS considers to be one line. So if statements are not allowed. You need to use a ternary operator (condition ? true : false) to accomplish the equivalent of an if. Note that everything within the handlebars gets access to the Retool development scope so you don't need to nest handlebars. Also and importantly, you can have multiple handlebar sets within the property! But each stands on its own.7

1 Like