Friday, September 19, 2014

Salesforce Lesson 2 - Formula IF Statement

What is the specific syntax of IF in Salesforce.com formulae?

First, to reiterate something (not so emphasized) in my last post (Lesson 1), Salesforce formula syntax is very much like Excel formula syntax. So if you have mastered an IF statement in Excel you will have little trouble understanding Salesforce IF in formulas.
(**Do note however that in Salesforce Apex language the IF statement is a considerably different animal. There it is just like it is in Java.)

The standard format of the formula IF is:
IF(condition, result if condition is TRUE, result if condition is FALSE)

Unlike the AND and OR statements which return only a Boolean, the IF stantement will return anything you like, including a Boolean True or False if you wish.

And you can nest IF statements inside the True or False result sections to create more complex conditions.
For example with custom fields on Lead TotalQuantity__c and TotalDollars__c you want to create a text formula field called Lead Size the formula could be something like:
IF(TotalQuantity__c > 1000, IF(TotalDollars__c > 10000, 'Huge', 'Large'), IF(TotalQuantity__c > 500, 'Medium', 'Small') )

This example works in the following way:
a. if TotalQuatity is greater than 1000 and TotalDollars is greater than 10,000 then the Lead Size is "Huge";
b. if TotalQuatity is greater than 1000 and TotalDollars is NOT greater than 10,000 then the Lead Size is "Large";
c. if TotalQuatity is NOT greater than 1000 but is greater than 500 then the Lead Size is "Medium";
d. finally, if TotalQuatity is NOT greater than 1000 NOR is it greater than 500 then the Lead Size is "Small".

If you are a Java or Apex coder interested in a comparison here is what this construct would look like in one of those laguages as a nested If statement:
If (TotalQuantity__c > 1000) {
    If (TotalDollars__c > 10000) {LeadSize__c = 'Huge';}
    Else {LeadSize__c = 'Large';}
}
Else {
    If (TotalQuantity__c > 500) {LeadSize__c = 'Medium';}
    Else {LeadSize__c = 'Small';}
}

If you simply need Lead Sizes of Large, Medium, and Small conditioning only with TotalQuantity criteria the formula would be a little simpler:
IF(TotalQuantity__c > 1000, 'Large', IF(TotalQuantity__c > 500, 'Medium', 'Small') )

Note in the above examples the lack of AND/OR statements. One could combine IF statements with AND/OR combinations but those function in formulas exactly as described in my previous post, not as one who is classically trained with conditional logic might expect.

Good luck with reinventing your brain around these and other Salesforce constructs and feel free to comment with questions.

Thursday, August 14, 2014

Salesforce Lesson 1 - Formula Constructs AND / OR

What is the specific syntax of ANDs and ORs in Salesforce.com 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 et.al. 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 Salesforce.com 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.