Back to all How-tos

Scripts: Combining data fields in conditions

To make communications as relevant as possible to customers, they need to be based on data, and often on combined data, e.g. where a customer lives, which products he has shown an interest in, and what age group he falls into. In OL Connect Designer, as of version 2021.1, the Conditional script dialog (right-click > Make Conditional…) lets you create conditions based on a combination of data fields very easily.

In older versions of the software this dialog allows to base a condition only on a single record field. So if you are using an older version, and you need to combine conditions that each depend on a different record field, or need a condition that triggers when a value is within a range, rather than just higher/lower, then this will have to be done with a script. This how-to gives a basic introduction.

Taking the time to learn even the simplest of JavaScript required will yield great reward in terms of control and power.

Basic formula

For a quick start you could right-click an element, select Make conditional and then click the Expand button. The Script editor will open with a basic script.

The basic formula for creating a condition is the following:

if( condition ) { // trigger if the condition is met
    results.hide(); // hides the element

So in this code, if() {} is the JavaScript way of creating a condition. Anything within the curly braces {} will trigger if the condition is true.
Adding a ! before the condition (e.g. !condition ) will invert the condition, so that the code inside the curly braces will trigger if the condition is not true.

The results are the elements in a template that match the selector of the script. Using results.hide(); simply hides the element(s), but looking at the source code (in Web and Email output) would still show the element and its contents. results.remove(); will completely “destroy” the element(s) when producing output, so that it is not available to the client.
But you can do so many different things, using basic JavaScript as well as use the Designer API to modify your template. For instance, the following would have the background of the element go red on values lower than 0 (negative values):

if(record.fields.amount < 0) {
    results.css("background-color", "red");

An overview of all that can be done with the results can be found here: Designer API.

Combined conditions

In order to combine conditions you can use a number of operators. The AND operator is &&, while the OR operator is ||. So here are a couple of examples based on actual record data checks:

// Value is between 1.5 and 2.6
if(record.fields.myvalue > 1.5 || record.fields.myvalue < 2.6) {

// Field1 is not below 0 (positive number) and Field2 is "true"
if(record.fields.Field1 >= 0 && record.fields.Field2 != "true") {

// Field "myvalue" is within a list of accepted values
if (!/Value1|Value2|Value3/.test(record.fields.myvalue)) {

Of course, you’re not limited to just one or two conditions! Combining many conditions together is possible.

if( (condition1 && condition2) || (condition3 && condition4) ) {
    // condition 1 and 2 are both true, OR condition 3 and 4 are both true.

In JavaScript conditions, the order of operation is very similar to basic mathematics: inner parenthesis are always evaluated first, going outwards. This is why !(condition && condition) and !condition && condition are not the same.

Leave a Reply

Your email address will not be published. Required fields are marked *