Formula syntax with multiple IF statements

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. Any pointers would be much appreciated.

2 Answers

The Salesforce formula field is a wacky one, that's true. I hate working with it, but do have a couple of pointers.

1. 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))`
2. 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.
EDIT: Just remembered the BASH comment from the question.
`kedit` is my editor of choice on Linux (I prefer KDE) and I know that it has all the above functionality (and many more). Pretty sure `gedit` does too, for the GNOME users.

• Thanks for that, very helpful. I've replied in an answer with what your tips led me to. – BRIT HEdmondson Feb 27 '20 at 9:40

Thanks to AvailableName for the help. This is what I've ended up with:

``````IF(Gift_Aid_Eligible__c,
IF(ISPICKVAL(Payment_Type__c, "CAF Donate"),
Gross_Amount__c * 0.25,
IF(ISPICKVAL(Payment_Type__c, "JustGiving"),
Gross_Amount__c * 0.2375,
Amount * 0.25)),
NULL)
``````