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.