Ternary Operator in Salesforce

In this blog, we’ll explore what the ternary operator is, how it’s used in Salesforce. We will be providing practical examples also to show its benefits. When it comes to writing clean and concise code in Salesforce, one operator that often goes unnoticed is the ternary operator. It is known for its simplicity and effectiveness.

The ternary operator is a conditional expression that can enhance your Apex code by reducing verbosity and improving readability.

What is a Ternary Operator?

This is a shorthand way of writing an if-else statement in a single line of code. Instead of writing the whole block, we can execute the same thing with just a single. Below is the syntax

Let’s understand each expression here

  1. condition: It is the expression to evaluate (e.g., x > 10).
  2. value_if_true: It will return the value when the condition is evaluated as true.
  3. value_if_false: It will return the value when the condition is evaluated as false.

Why Use the Ternary Operator in Salesforce?

This operator can be incredibly useful in Salesforce for a variety of reasons:

Code Simplification: It reduces the need for verbose if-else statements.

Improved Readability: It makes conditional logic more compact and easier to follow.

Enhanced Performance: While the performance difference is negligible, the simplicity can lead to cleaner and more maintainable code.

Example Usage

Let’s understand with one example here. First we will create one basic if-else block then we will understand how we can create ternary logic for that.

 

Here, we just simply checking the condition like if x is equals to Person 1 then it will be assigned to result else we are assigning value of y to the result.

OUTPUT

if else block

 

Now let’s write this conditional block by using the ternary operator

 

Ternary Operator

 

In this example, we have defined two String variables for which we are comparing the values and then we are assigning the value in the result variable. Just like the if-else block we are first checking the condition that if x is equals to Person 1 if yes then we are assigning x to the result else we are assigning y to it.

Let’s take another example of how and where we can use the ternary operator.

Here, we are using a ternary operator to dynamically determine field values or filters in a SOQL query

Syntax

 

Ternary in SOQL

 

Above we have declared one boolean variable isActive in which we are providing the value (true or false) and we are executing our SOQL on the accounts object where we are fetching a list where the Active__c filter will be applied based on the value provided in the isActive variable. Basically, the isActive variable determines the value of the Active__c filter in the SOQL query.

Things to remember

  1. Considering this expression x ? y : z we have to keep in mind that x can’t be null.
  2. If the ternary operator makes the code harder to understand, opt for traditional if-else statements. Always prioritise code readability.
  3. Hence nesting is possible with ternary operators but it is advisable to avoid nesting ternary operators, as it can quickly lead to confusion.

Special Ternary Operator

Here the special ternary operator is also called the safe navigation operator.

It displays as ?. For example: a?.b which means if a is not null then a.b gets evaluated otherwise it will return null.

For Example: Below is BillingCity of the first account in the acts list is null return null else return BillingCity in uppercase.

Syntax

 

safe navigation

Are you preparing for the Salesforce AI Certifications? Check out the Salesforce certification practice set here

FAQ’s

1. What is a ternary operator in Salesforce?
The ternary operator is a shorthand way to write if-else logic in a single line of code, with the syntax condition? value_if_true : value_if_false;.

2. How does the ternary operator affect performance?

The performance impact is negligible, but it contributes to cleaner and more maintainable code.

3. What is the null coalescing operator?

The null coalescing operator is a binary operator in the form a ?? b that returns a if a isn’t null and otherwise returns b.

4. Can we also use this operator in VF and LWC?

Yes, the ternary operator is a way of writing our if-else block in a different and effective way and can be used with VisualForce for conditional rendering and in Lightning Web Components in JavaScript for similar purposes.

Conclusion

The Ternary Operator in Salesforce is a valuable tool to write efficient and clean code. By understanding its syntax and applying it in appropriate scenarios, we can enhance our codebase’s readability and maintainability. However, like any tool, it’s important to use it wisely and avoid over-complicating your logic.

Get a complete Roadmap To Learn Salesforce Admin And Development

Share Now

Kashish have extensive Salesforce development experience, holding 4 Salesforce certifications. she posses expertise in Apex, Lightning Web Components, and Salesforce Admin, with a track record of successful project delivery. As a dedicated Salesforce enthusiast, she actively seek and embrace new challenges and opportunities within the dynamic Salesforce ecosystem.

Similar Posts

Leave a Reply

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