How to adjust this Salesforce formula with IF statement?
We have a formula field that references a picklist field. It should have data in it only for a certain record type, but somehow in migration it was populated on all records. I need to clear out those values. Since it's not possible to clear values from a formula field with Data Loader, I need to update the formula to say if the Stage picklist field is null, then null.
How do I update this formula to include that?
If(AND(
Text(Stage__c) <> "red",
Text(Stage__c) <> "blue",
Text(Stage__c) <> "green"), "Active", "Not Active")
UPDATE: commenter gives great additional context to how to look at this. For anyone wondering, the full solution here was this:
IF(
ISPICKVAL(Stage__c,""),"",
IF(AND(
Text(Stage__c) <> "red",
Text(Stage__c) <> "blue",
Text(Stage__c) <> "green"), "Active", "Not Active"))
To adjust this Salesforce formula with IF Statement -
You can chain IF() in a formula much like you can in code. You just add another IF() in the "then" and/or "else" portions.
We can do things a bit cleaner in code than what I'm about to present, but the equivalent to what you're looking for would be
if(String.isBlank(Stage__c)){
// then
return null;
}else{
if(Stage__c != 'red' && Stage__c != 'blue' && Stage__c != 'green'){
// then
return 'Active';
}else{
return 'Not Active';
}
}
Translating to a formula from that is pretty simple
IF(
ISBLANK(Stage__c),
/* Then */
Stage__c,
IF(
AND(/* stage checking for red/green/blue */),
/* Then */
'Active',
'Not Active'
)
)
As always, indenting formulas like they were code helps you keep track of things (it's really easy to get lost in a sea of parenthesis otherwise)