0

I am trying to use nested IF statements to get a field to populate a country. It seems like no matter what I try I get the same result. Can you help me understand this structure?

The reason for wrapping the fields in TEXT() is due to the fact they are dropdown menus.

The error I get returned when I check the syntax is "Error: Incorrect number of parameters for function 'IF()'. Expected 3, received 4"

Thank you for your help!

IF(
    TEXT(Guest__r.ID_Type__c) = "Passport",
    TEXT(Guest__r.ID_Locale_Country__c), 
    NULL,
    (IF(
        TEXT(Guest__r.ID_Type__c) = "Drivers License",
        TEXT(Guest__r.Country_of_Residence__c), 
        NULL,
        (IF(
            TEXT(Guest__r.ID_Type__c) = "State ID",
            TEXT(Guest__r.Country_of_Residence__c), 
            NULL,
            (IF(
                TEXT(Guest__r.ID_Type__c) = "Military ID",
                TEXT( Guest__r.Country_of_Citenzenship__c), 
                NULL,
                (IF(
                    TEXT(Guest__r.ID_Type__c) = "Birth Certificate",
                    TEXT(Guest__r.ID_Locale_Country__c), 
                    NULL
                )
                )
            )
            )
        )
        )
    )
    )
)
1
  • The first step in debugging formulas is pretty much always to format them according to best practice (one "statement" per line, indent an extra level every time you encounter another open parenthesis). I have done this for you this time.
    – Derek F
    Jul 6 at 0:10

1 Answer 1

1

Like the error is telling you, IF() only accepts 3 parameters. If you look at the documentation, IF() looks like

IF(condition, value if true, value if false)

When you nest IF() functions in a formula, it is either going to be in the value if true or value if false parts (or sometimes, in both).

So instead of

IF(
    TEXT(Guest__r.ID_Type__c) = "Passport",
    TEXT(Guest__r.ID_Locale_Country__c), 
    NULL,
    IF(
        TEXT(Guest__r.ID_Type__c) = "Drivers License",
    ...

you need to get rid of that NULL, and have the next IF() be the "value if false"


IF(
    TEXT(Guest__r.ID_Type__c) = "Passport", /* condition */
    TEXT(Guest__r.ID_Locale_Country__c),    /* value if true */
    IF(                                     /* value if false */
        TEXT(Guest__r.ID_Type__c) = "Drivers License",
    ...

That said, a nested IF() isn't the best approach here. You can instead use CASE() and reduce the amount of repetition that you're doing (and it's one of the few functions that works with picklists)

CASE(
    Guest__r.Id_Type__c,
    "Passport", TEXT(Guest__r.ID_Locale_Country__c),
    "Drivers License", TEXT(Guest__r.Country_of_Residence__c),
    /* and so on */
    "No Match"
)

The final parameter of the CASE() ("No Match" in my example) is required. It's the value that gets used if none of the test values ("Passport", "Drivers License", etc...) match the input.

1
  • Thank you! I was doing this late at the end of a work day and my brain had abandoned me. I completely forgot about the whole structure of the IF(). Ah I didn't even look at the CASE()! Great points! Awesome and easy to understand explanation~! Thank you!
    – Aereisdin
    Jul 7 at 13:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .