Ternary Operator in Salesforce Apex

In Salesforce Apex, Ternary Operators are part of the Apex conditional statements. They serve as a shorthand replacement for if-else conditionals, making the code logic more understandable.

In this Salesforce tutorial, we will learn about Ternary operators in Salesforce Apex and how to use them as conditionals in our code logic.

What are Ternary Operators in Salesforce Apex?

In Salesforce Apex, ternary operators are used as a one-liner replacement for if-else conditional statements. The logic in the ternary operators works similarly to if-else conditionals. It evaluates a condition and returns one value if it is true and another if it is false.

For example, if x is true, y is the result. Otherwise, z will be the result. Let’s see how we define this condition using the ternary operator in Salesforce Apex. The ternary operator condition is written as follows.

X ? Y : Z ;

In the above expression, X is the condition evaluated as true or false. If the condition is true, the returned value will be Y; otherwise, it will be Z.

Use Case of Ternary Operator in Salesforce Apex

In the code below, we will understand the implementation of the ternary operator through an example. We will define the condition that when the Score (integer) value is less than 30, the return value will be True or False.

Integer score = 85;
String result = score >= 30 ? 'Passed' : 'Failed';
System.debug(result); 

In the above code, we have defined an integer value of 85, which is greater than 30 according to the condition. Thus, the expected output will be the stringPassed.

Ternary operators in Salesforce

When I change the integer value to less than 30, the expected output will be the string “Failed” according to the defined condition.

Use ternary operator in Salesforce Apex

This way, you use and execute the ternary operator in your Apex code, simplify the code structure, and make it easy to understand.

Handling Null Values in Ternary Operators in Salesforce

In Salesforce Apex, we can also handle null values in the ternary operator. To achieve this, we can set the condition parameters so that when the value is null, the output defaults to the specified value.

Apex code:

Default Value = "APEX";
String input = null;
String result = (input != null) ? input : Default Value ;
System.debug(result);

According to the condition we have put in the above code, if the input value is null, then it will take the input as the Default Value.

Handle null values in Salesforce Ternary operator

This way, we handle null values in Ternary operators in Salesforce Apex by following the code structure above.

When not to use Ternary Operators

When your apex code logic is complex and difficult to understand in a single-line ternary operator, you should use the if-else statement. This will make your code more readable and easier to understand.

We should also avoid nesting in ternary operators. Though we can nest in the ternary operator again, it will make the code look more complex and difficult to maintain.

Apex code for nested ternary operator (Not recommended):

Integer x=15;
Integer y=20;
Integer z=25;

String result = (x > y) ? ((x > z) ? 'x is the largest number' : 'z is the largest number') : ((y > z) ? 'y is the largest number ' : 'z is the largest number');

 The above code, which uses nested ternary operators, will also run fine; however, it is very difficult to understand for more complex logic and is also challenging to modify later.

Use of regular if-else statements (recommended):

Integer x=15;
Integer y=20;
Integer z=25;
String result='';

if (x > y && x > z) {
    result = 'x is largest';
} else if (y > x && y > z) {
    result = 'y is largest';
} else {
    result = 'z is largest';
}
Nested Ternary operator in Salesforce Apex

This way, you can use an if-else statement for nested conditionals in Salesforce Apex and avoid using the ternary operator.

Conclusion

In this Salesforce tutorial, we learned about the ternary operator and understood its syntax with the help of an example. After this, we also discussed when and when not to use the ternary operator in Salesforce Apex.

You may also like to read:

Agentforce in Salesforce

DOWNLOAD FREE AGENTFORCE EBOOK

Start with AgentForce in Salesforce. Create your first agent and deploy to your Salesforce Org.

Salesforce flows complete guide

FREE SALESFORCE FLOW EBOOK

Learn how to work with flows in Salesforce with 5 different real time examples.