I have three different text boxes(Email, Work Email, and Other Email) to enter email addresses. I created a custom field of type "Formula". I need it to loop through the three email(s) and return one email address, the first one it sees, and ignore the rest. I'm using IF statement but I don't seem to get it to work? Any help and suggestions is much appreciated.
3 Answers
BLANKVALUE(Field1, BLANKVALUE(Field2, Field3))
This is a bit easier to read than nested IF statements. BLANKVALUE takes two parameters, and returns the first (left) parameter if it's not blank, otherwise the second (right) parameter.
-
+1. Much cleaner than my nested IF's. Wish I had thought of it :) Aug 28, 2014 at 18:40
If you literally just want to return one of them, not really caring which one if more than one has data, then something like this will work
IF(
NOT(ISBLANK(Email__c)),
Email__c,
IF(
NOT(ISBLANK(Work_Email__c)),
Work_Email__c,
Other_Email__c
)
)
If there's a value in the Email__c
field, it uses that, regardless of whats in the other 2 fields. If theres nothing in the Email__c
field, it moves on to the Work_Email__c
field, and if theres something in that, it uses it, regardless of whats in the Other_Email__c
field. If Work_Email__c
is blank, it uses the Other_Email__c
field, if it has a value. If all 3 are blank it returns null.
-
There's really no need for the third IF, since if Other_Email__c were null, it'd be the same net result, but a larger total compiled size.– sfdcfoxAug 28, 2014 at 18:41
-
I know you've chosen an answer, but this might be a better way
selectedEmail = email ? workEmail : otherEmail;
Which is the syntax for the ternary operator. https://www.salesforce.com/us/developer/docs/apexcode/Content/langCon_apex_expressions_operators_understanding.htm
-
Thanks for your answer Jeremy. I tried similar approach but return isn't supported by Salesforce.– AchaaAug 28, 2014 at 20:39
-
Can you declare and assign variables? The return keyword is only included there because the question used the word "return", ...oh, and because I made a faulty assumption. I may be using SF soon, so I'm learning, starting today. Answer edited. Aug 28, 2014 at 21:22
-
Can you have a buffer for loading a few things after you return from that method. Using email as ternary, there are lot of unfair load of primitive variables. Checking the value after the buffering and returning your ternary value would be better. I still don't think it's part of a salesforce issue though. Perhaps Scala or Haskell? Aug 29, 2014 at 6:54