Skip to content
Groovy 11 - if then else / switch Adding Logic To the Scripts

if-else

Sometimes we want to control the flow of the code. This means executing a block of code when certain conditions are met or executing another block of code and ignoring other statements. To achieve this, we use if-else.

if-else is the most basic flow control statement in Groovy and many other programming languages. It tells your script to execute certain parts of the code only if a particular condition evaluates to true or false.

General Form of if-else

        if (condition) {
            // block of code to execute if the condition is met
        } else {
            // block of code to execute if the condition in if is not met
        }
        

If there is only one line of code following if or else, you do not have to encapsulate it in { }:

        if (condition)
            // 1 line of code to execute
        else
            // 1 line of code to execute
        

Example 1

        def var_A = 100
        if (var_A == 100)
            setAttribute('FieldName', var_A)
        else
            setAttribute('FieldName', 200)
        

Code above can also be written like:

        def var_A = 100
        if (var_A == 100) {
            setAttribute('FieldName', var_A)
        } else {
            setAttribute('FieldName', 200)
        }
        

Example 2

        def var_A = 100
        def var_B = 200
        if (var_A < 150) {
            var_A = var_B + 400
            setAttribute('FieldName', var_A)
        } else {
            var_B++
            var_A = var_B + 600
            setAttribute('FieldName', var_B)
        }
        

This code cannot be written as:

        if (var_A < 150)
            var_A = var_B + 400
            setAttribute('FieldName', var_A)
        else
            var_B++
            var_A = var_B + 600
            setAttribute('FieldName', var_B)
        

This is not correct

Of course, we can even combine the two styles like:

        if (var_A < 150) {
            var_A = var_B + 400
            setAttribute('FieldName', var_A)
        } else
            setAttribute('FieldName', 800)
        

Sometimes we need even more complex logic, so we control the flow depending on multiple conditions. For this, we use else if.

General Form

        if (condition) {
            // block of code to execute if the if condition is met
        } else if (condition) {
            // block of code to execute if the else if condition is met
        } else {
            // block of code to execute if none of the conditions above are met
        }
        

Example 3

        def var_A = 100
        if (var_A < 100) {
            setAttribute('FieldName', 10)
        } else if (var_A > 100) {
            setAttribute('FieldName', 300)
        } else {
            setAttribute('FieldName', 100)
        }
        

If you need to add even more conditions, you can simply add another else if:

        if (condition)
            // 1 line of code
        else if (condition)
            // 1 line of code
        else if (condition)
            // 1 line of code
        else
            // 1 line of code
        

Remember, if you only have one line of code to execute, you do not have to encapsulate it in { }. If you have a block of code to execute, then use { } to encapsulate it.

As a best practice, if you start an if statement, always end it with else. The code could work like this:

        if (condition) {
            // some code
        }
        

But to respect a standardized syntax, end the if with else, even if nothing needs to execute in the else part:

        if (condition) {
            // block of code
        } else {
            return false // this line means nothing will be returned, so basically nothing happens
        }
        

SWITCH

Switch is usually used to replace if-else and to give your code a more compact form and ease of reading. Sometimes it also helps in making the code more efficient. However, in the context of Sales Cloud, using switch might not make scripts visibly more efficient due to the limitations imposed by the Groovy engine. Therefore, using switch is a matter of preference and your desire to make the code more compact.

General Form of a Switch

        switch (Variable_to_Check) {
            case 1:
                // code to execute
                break;
            case 2:
                // code to execute
                break;
            case 3:
                // code to execute
                break;
            default:
                // code to execute
                break;
        }
        

Example

        def var_number = 10
        switch (var_number) {
            case 1:
                println("var_number = " + 1)
                break;
            case 5:
                println("var_number = " + 5)
                break;
            case 10:
                println("var_number = " + 10)
                break;
            case 100:
                println("var_number = " + 100)
                break;
            default:
                println("var_number has a different value")
                break;
        }
        

The code above works like this: we have a variable var_number that is equal to 10. The switch statement checks the value of this variable. If the value matches any of the cases, it executes the corresponding code. If no match is found, the default case is executed. The keyword break is used to exit the case. Without break, the code will continue to execute the following cases.