Ask a Question
Ask Question Login
Corporate Training
  1. Community
  2. Salesforce
  3. Question
Salesforce

How to get multiple if statements in salesforce formula?

Asked by David Edmunds Feb 28, 2023 4.1K views 2 answers
Share

About this question

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.

Your answer

2 Answers

Ranjana Admin JanBask Expert Latest answer

Answered on May 29, 2024

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.

Was this helpful?

More Salesforce discussions

Learn & Explore

Free tutorials and interview questions from industry experts — learn the skill, then get ready to prove it.

Latest Salesforce Blogs

Guides, tips and career advice on Salesforce from JanBask experts.