0

I have to use an address field in a formula field but the formula returns the following error:

Error: Incorrect parameter type for function 'IF()'. Expected Location, received Text

The formula is as follows:

IF(MSM_AlmacenCarga__r.MSM_CodigoAlmacen__c = '8058', MSM_AlmacenCarga__r.MSM_DireccionAlmacen__c,
    IF(MSM_ClienteEnvioPdV__r.RecordType.DeveloperName = 'MSM_Distribuidor',
        IF(ISBLANK(MSM_ClienteEnvioPdV__r.BillingStreet), "" & BR() , MSM_ClienteEnvioPdV__r.BillingStreet & BR()) &
        IF(ISBLANK(MSM_ClienteEnvioPdV__r.BillingCity), "" & BR(), MSM_ClienteEnvioPdV__r.BillingCity & BR()) &
        IF(ISBLANK(MSM_ClienteEnvioPdV__r.BillingPostalCode), "" & BR(), MSM_ClienteEnvioPdV__r.BillingPostalCode & BR()) &
        IF(ISBLANK(MSM_ClienteEnvioPdV__r.BillingCountry), "" & BR(), MSM_ClienteEnvioPdV__r.BillingCountry & BR()) &
        IF(ISBLANK(Account.Phone), "" & BR(), Account.Phone & BR()), 
    IF(MSM_ClienteEnvioPdV__r.RecordType.DeveloperName = 'MSM_DireccionEntrega',
        IF(ISBLANK(MSM_ClienteEnvioPdV__r.ShippingStreet), "" & BR(), MSM_ClienteEnvioPdV__r.ShippingStreet & BR()) &
        IF(ISBLANK(MSM_ClienteEnvioPdV__r.ShippingCity), "" & BR(), MSM_ClienteEnvioPdV__r.ShippingCity & BR()) &
        IF(ISBLANK(MSM_ClienteEnvioPdV__r.ShippingPostalCode), "" & BR(), MSM_ClienteEnvioPdV__r.ShippingPostalCode & BR()) &
        IF(ISBLANK(MSM_ClienteEnvioPdV__r.ShippingCountry), "" & BR(), MSM_ClienteEnvioPdV__r.ShippingCountry & BR()) &
        IF(ISBLANK(MSM_ClienteEnvioPdV__r.Phone), "" & BR(), MSM_ClienteEnvioPdV__r.Phone & BR()), 
    "")
    )
)

MSM_CodigoAlmacen__c is a string and MSM_DireccionAlmacen__c is the Address field.

Is there any way to solve the error?

Edit: Added actual formula

3
  • The CustomFieldA__c = 'X', bit looks like it'd be the issue, but it's hard to say what the fix would be with it being obfuscated/generic-ified. Seeing the actual field name and value you're testing against would be helpful (also, is CustomFieldA__c a field of type Location? or is it a relationship field to an SObject named "Location"?). Please edit your question to add that clarification.
    – Derek F
    Commented Mar 13 at 13:27
  • Best guess right now is that you need to be using the GEOLOCATION() function in concert with DISTANCE()
    – Derek F
    Commented Mar 13 at 13:29
  • @derekF I have added the formula, how it makes you see the idea more clearly Commented Mar 13 at 13:41

1 Answer 1

1

The then and else parameters of an IF() must be the same datatype.

From some brief experimenting, it looks like Salesforce is treating Address fields (a type of compound field) as a Location, and there's no function to convert that into a textual representation. The only functions that can work with a Location are DISTANCE() and GEOLOCATION() (which itself can only be used in DISTANCE()). Salesforce does say that DISTANCE() doesn't work with custom Address fields.

What you need to do here is access the individual parts of your MSM_DireccionAlmacen__c Address field just like you are with the ShippingAddress/BillingAddress from MSM_ClienteEnvioPdV__r. Accessing those for a custom address field is a little different. The format is <field api name> + __ + <address component> + __s

i.e. MSM_AlmacenCarga__r.MSM_DireccionAlmacen__c__Street__s

So your formula should look like

IF(
    MSM_AlmacenCarga__r.MSM_CodigoAlmacen__c = '8058',
    IF(ISBLANK(MSM_AlmacenCarga__r.MSM_DireccionAlmacen__c__Street__s), "" & BR() , MSM_AlmacenCarga__r.MSM_DireccionAlmacen__c__Street__s & BR()) &
        IF(ISBLANK(MSM_AlmacenCarga__r.MSM_DireccionAlmacen__c__City__s), "" & BR(), MSM_AlmacenCarga__r.MSM_DireccionAlmacen__c__City__s & BR()) &
        IF(ISBLANK(MSM_AlmacenCarga__r.MSM_DireccionAlmacen__c__PostalCode__s), "" & BR(), MSM_AlmacenCarga__r.MSM_DireccionAlmacen__c__PostalCode__s & BR()) &
        IF(ISBLANK(MSM_AlmacenCarga__r.MSM_DireccionAlmacen__c__Country__s), "" & BR(), MSM_AlmacenCarga__r.MSM_DireccionAlmacen__c__CountryCode__s
    /* and then the rest of your formula is unchanged */
)

+edit: the "address" component for Country is actually CountryCode, as noted by OP in the comments. Answer has been updated to address that.

1
  • Thanks for sharing your idea. It worked! The only drawback is that I can't use Country__s, you can't select it, It has to be CountryCode__s. I have marked your answer as solution. Commented Mar 13 at 16:09

You must log in to answer this question.

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