How to get multiple if statements in salesforce formula?

2.0K    Asked by DavidEdmunds in Salesforce , Asked on Feb 28, 2023

I'm still very new to the formula syntax, and am hoping to achieve a series of nested IF statements. I'm a bit more experienced with bash, and what I'm trying to achieve would look something like this with that syntax:


if [ $GiftAidEligible ]; then
  if [ $PaymentType = "CAFDonate" ]; then
    GiftAidAmount=$(GrossAmount*0.25)
  elif [ $PaymentType = "JustGiving" ]; then
    GiftAidAmount=$(GrossAmount*0.2375)
  else
    GiftAidAmount=$(Amount*0.25)
  fi
else
  GiftAidAmount=NULL

fi

The field I'm trying to calculate with the above is called GiftAidAmount, a currency formula. The related static fields are:


-GiftAidEligible: A checkbox
-PaymentType: a picklist of string values
-GrossAmount: a currency field denoting the donation amount before fees
-Amount: another currency field denoting the donation amount after fees

I can explain the logic in a bit more detail if needed, but ultimately I'm after some help getting the above logic in to the correct syntax for my formula.

Answered by Dipika Agarwal

The multiple if statements in Salesforce formula field is a wacky one, that's true. I hate working with it, but do have a couple of pointers. The IF statement is pretty easy to get a handle on. Think of it as a 3 part function. IF(isTrue, then-thing, else-thing) If you want a nested condition, put that in the else-thing part. That would look like IF(isTrue, then-thing, IF(otherTrue, other-thing, other-then-thing)) Use an external text editor. Using an external text editor we can take the above example (which is nigh impossible to read and makes me flashback to writing LISP) and turn it into this:

IF(isTrue,
    then-thing,
    else-thing
)
Or
IF(isTrue,
    then-thing,
    IF(otherTrue,
        other-thing,
        other-then-thing
    )
)

And suddenly it becomes a lot more readable. I personally like Notepad++ because it will automatically tab in when I hit enter and highlight opening/closing brackets when the cursor is on them. This makes it a lot easier to keep track of where you are in your nested conditions.



Your Answer

Answer (1)

In Salesforce, creating complex formulas that involve multiple IF statements is common. You can nest IF statements within one another to handle various conditions. However, Salesforce has a character limit on formulas, so it’s crucial to keep them as concise as possible.


Here’s a step-by-step guide on how to use multiple IF statements in a Salesforce formula:

Basic Syntax of an IF Statement

  The basic syntax for an IF statement in Salesforce is:IF(logical_test, value_if_true, value_if_false)

Where:

  • logical_test is the condition you are testing.
  • value_if_true is the value returned if the condition is true.
  • value_if_false is the value returned if the condition is false.

Nested IF Statements

To use multiple IF statements, you can nest them within each other. Here’s an example of a nested IF formula:

IF(condition1, value1,
  IF(condition2, value2,
    IF(condition3, value3,
      value_if_all_false
    )
  )
)

Example Scenario

Suppose you want to evaluate a field called Stage and return different values based on its content. Here’s a formula that handles this:

IF(
  ISPICKVAL(Stage, "Prospecting"), "10%",
  IF(
    ISPICKVAL(Stage, "Qualification"), "20%",
    IF(
      ISPICKVAL(Stage, "Needs Analysis"), "30%",
      IF(
        ISPICKVAL(Stage, "Proposal/Price Quote"), "40%",
        "Unknown Stage"
      )
    )
  )
)

Using CASE Function

For readability and maintainability, you can use the CASE function, which is often more efficient for multiple conditions. Here’s how you can rewrite the above example using CASE:

CASE(Stage,
  "Prospecting", "10%",
  "Qualification", "20%",
  "Needs Analysis", "30%",
  "Proposal/Price Quote", "40%",
  "Unknown Stage"
)

Tips for Complex Formulas

Break Down Complex Formulas: If your formula is too complex or hitting limits, break it into smaller formula fields that can be referenced in the main formula.

Test Each Condition: Ensure each nested condition works as expected. Use the formula editor's syntax checker.

Character Limits: Salesforce formulas have a limit of 5,000 characters, but it’s good practice to keep them as concise as possible.

Example with Logical Functions

Combining logical functions can also be useful. Here’s an example using AND and OR with IF statements:

  IF(  AND(ISPICKVAL(Stage, "Prospecting"), Amount > 10000), "High Value Prospecting",  IF(    AND(ISPICKVAL(Stage, "Qualification"), Amount <= 10000), "Low Value Qualification",    "Other"  ))

Summary

  1. Use nested IF statements for multiple conditions.
  2. Use CASE function for a cleaner approach when dealing with multiple discrete values.
  3. Consider breaking down complex formulas and test each part thoroughly.

By following these guidelines, you can efficiently create and manage multiple IF statements in your Salesforce formulas.

4 Months

Interviews

Parent Categories