Thursday, August 14, 2014

Salesforce Lesson 1 - Formula Constructs AND / OR

What is the specific syntax of ANDs and ORs in formulae?

These constructs look very different from what conventional If statements** with ands or ors in many of the usual languages look like. (Though Excel are similar in their formula formulations to how Salesforce applies them.)
** IF statements will be the topic of my next post.

For starters the result of a compound conditional statement using either an AND or an OR is going to be a Boolean True or False. In Salesforce validation rules a true result is going to be a positive error condition. In essence though it is the same when using a formula for other purposes such as workflow conditions. That is - if the condition is True something will take place, otherwise whatever something is won't.
You set an outcome that will occur as the result of a formula evaluating to true. But if it evaluates false, again, the outcome does not "come out".

Anyway - back to ANDs and ORs. When a decision is based on multiple - and possibly nested - conditions this is where ANDs and ORs come into play.

The standard format of AND is:
AND(condition 1, condition 2,..., condition x)
and when ALL conditions 1 - x are true then the total result is true. If ONE OR MORE of them are false then the result is false.

The standard format of OR is:
OR(condition 1, condition 2,..., condition x)
and when ONE OR MORE conditions 1 - x are true then total result is true. If ALL of them are false then the result is false.

More complex constructs may easily be built:
AND(condition 1, condition 2, OR(condition a, condition b))
is a simple nested example. This means that if condition 1 is true and condition 2 is true and either or both condition a or condition b is true then the total result is true.

A few specifics to pay attention to:

  • Note your punctuation - its important.
    • Parentheses enclose every OR or AND block.
    • Commas follow every condition except the last in a given OR or AND block.
  • Picklist fields need to be wrapped by the TEXT() function for comparing with a text literal.
  • Use other formula functions when necessary or appropriate - such as ISBLANK to determine if a field has no value (null) in it.

Once you get the hang of this it becomes second nature to see how to solve a given problem. Mind you it helps to understand logic and set theory.

I kicked off my work in what might be thought of as reverse order - at least by the powers that be @ "[International No Symbol] SOFTWARE". I was coding some advanced solutions with Visualforce and Apex right out of the gate yet I really did not get the formula syntax until later when a client asked me to start building solutions with it.
Now its just like a walk in the park.

No comments:

Post a Comment