How to get multiple if statements in salesforce formula?
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.
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.