3

When using State & Country picklist functionality, is it possible to write a Validation Rule that targets the Billing State or Shipping State fields to make them required but only when the Country has States?

Since some Countries do not have States/Regions and this is a restricted picklist field, Salesforce prevents bad data at the database level as well as in the native UI. A Country with no States will always have a blank value in the State field(s).

So, for a Country which doesn't have any States configured, and therefore will accept no data in the Billing State or Shipping State address fields, what is the appropriate way to write the Validation Rule?

My functional requirement:

  • If the Country has States and BillingStateCode is blank, display error.
  • If the Country does not have States, BillingStateCode can (and must) be blank.
2
  • You may wish to vote on this IdeaExchange post: success.salesforce.com/ideaView?id=08730000000BqlNAAS. Related because if you could make individual address columns required, state and country picklist should work the way you're asking for here. Sep 25, 2014 at 1:56
  • @CarolynGrabill, voted. Considering that idea has been out there for 7 years and it's still not been implemented, I'm not going to hold my breath. :-)
    – Mark Pond
    Sep 25, 2014 at 14:33

1 Answer 1

2

I would think you could implement the following validation rule on State:

AND(
    ISPICKVAL(State, ''),
    OR(
        ISPICKVAL(Country, 'CountryWithStates1'),
        ISPICKVAL(Country, 'CountryWithStates2'),
        ISPICKVAL(Country, 'CountryWithStates3')
    )
)

You'll have to manage the OR list yourself, but that seems tolerable.

If you would like to try a more programmatic approach, check out this blog post. It would allow you to write a trigger based validation something like:

Map<String, List<String>> countryToStates = GetDependentOptions('Lead', 'Country', 'State');
for (Lead newLead : newLeads)
{
    if (countryToStates.containsKey(newLead.Country) &&
        !countryToStates.get(newLead.Country).isEmpty() &&
        newLead.State == null)
    {
        newLead.State.addError(Label.State_Required);
    }
}
2
  • This is what I suspected too and it's a good answer. I'm hoping there is some hidden magic out there that will make it self-managed. As it stands, with this approach the admin needs to maintain the two lists - which is a little error prone.
    – Mark Pond
    Sep 24, 2014 at 22:08
  • Did you see my updated response? There is a way to do it programatically, though you will need to validate in a trigger instead of a validation rule.
    – Adrian Larson
    Oct 14, 2014 at 23:45

You must log in to answer this question.

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