The Salesforce Ternary operator.

Why is this not documented further?

Because it’s common to every language.

PHP, Java, C#, Apex… All languages have this ternary operator. Probably why it’s not documented heavily. There are plenty of debate on the forums open if you want to go explore deeper. Let’s keep it at this…

Because it’s use case is limited to simple if statements.

Ternary Operators reduce, say, 7 lines of code to 1. However beyond simple if statements, they become cumbersome to ready and debug. This can be especially problematic for junior developers or Salesforce Admins (who should be able to read Apex code).

Let’s play.

I’ve seen this a couple of time but never really looked at it closely. But what if you are not really a developer and just want to play with the code a little. Here you go…

Source:

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_expressions_operators_understanding.htm

Definition:

Ternary operator (Right associative). This operator acts as a short-hand for if-then-else statements. If x, a Boolean, is true, y is the result. Otherwise z is the result. Note: x cannot be null.

Syntax:

x ? y : z

Example code in screenshots below.

@istest
public with sharing class TernaryTest {
@istest
    public static void TernaryTest() {
        String result = '';
        Boolean thisIsTrue = true;
        result = thisIsTrue ? 'It is True' : 'It is False';
        System.debug('Result ::' + result);
        System.AssertEquals(result,'It is True','Assertion Failed, Expected It is True, Actual ::'+result);
    }
}

How to test:

  • Use VSCode with the Salesforce CLI
  • Create a new class. We will create a test class.
  • Enter the following code into the class.
  • Save it, and deploy it to your sandbox.
  • Use the CLI to Turn on Apex Debug Log for Replay Debugger
  • Run the test by clicking Run Test or Run All Tests
  • Wait for the test to finish. Meanwhile, add a breakpoint on the debug statement.
  • Important: Put a Breakpoint in at the debug statement to stop the code.
  • Use the CLI to Get Apex Debug Logs
  • Click the latest one to download and open.
  • The Apex Debug log will download and open.
  • Right click anywhere and choose Launch Apex Replay Debugger with Current File
  • Press F11 to get into the code in debug, have fun…

My question is, is there any benefit to doing this over traditional if statements.

  • Less space for the code… improves readability… unless you don’t know what it is.
  • It really allows us to replace a simple if statement with a single line of code.
  • The problem is when you get to non-programmers… sticking to a simple if/else format is often easier for non-programmers to read.
  • I would also only recommend this on short if statements.
  • From Dan Tao on stackoverflow. https://stackoverflow.com/questions/3312786/benefits-of-using-the-conditional-ternary-operator
    • I would basically recommend using it only when the resulting statement is extremely short and represents a significant increase in conciseness over the if/else equivalent without sacrificing readability.
    • Good example:
      • int result = Check() ? 1 : 0;
    • Bad example:
      • int result = FirstCheck() ? 1 : SecondCheck() ? 1 : ThirdCheck() ? 1 : 0;

Need help getting the Salesforce CLI setup.

Documentation is here: https://developer.salesforce.com/docs/atlas.en-us.sfdx_setup.meta/sfdx_setup/sfdx_setup_intro.htm

Troy 9/21/21

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s