+ Start a Discussion
Ken Koellner 7Ken Koellner 7 

Before trigger to translate Lead.State to valid picklist value doesn't work.

We have the feature on that enabled strict picklist for the State field in addresses such as in Lead.  We do not want to configure intergration values as we want the state names, e.g., "Texas" to be used everywhere.

An external system is sending state code, .e.g, "TX".  I was hoping to write a trigger on Lead that runs on before insert that translates the value.

The code below is a portion of the code run in the before trigger...
Map<String, Integration_State_Map__mdt> stateMap = Integration_State_Map__mdt.getAll(); 
        for (Lead leadIter : newLeadList) {
            if (String.isBlank(leadIter.State)) {
                continue;
            }
            Integration_State_Map__mdt stateEntry = stateMap.get(leadIter.State);
            if (stateEntry != null) {
                leadIter.State = stateEntry.State_Name__c;
            }
            System.debug('\n' + JSON.serializePretty(leadIter));
        }

I can see that the trigger is indeed translating the state value.  Note the Sytem.debug statement above.  Below is a portion of the output showing that state is "Texas" and not "TX".

{
  "attributes" : {
    "type" : "Lead"
  },
  "Company" : "foo2",
  "DoNotCall" : false,
  "HasOptedOutOfFax" : false,
  "OwnerId" : "0055e000006PjyxAAC",
  "RecordTypeId" : "0125e000000qlvFAAQ",
  "CountryCode" : "US",
  "Status" : "Not Started",
  "IsConverted" : false,
  "IsUnreadByOwner" : false,
  "HasOptedOutOfEmail" : false,
  "State" : "Texas",
  "LastName" : "foo1"
}
A portion of the log with the error is shown below.
 
13:01:13.537 (1604369671)|CODE_UNIT_FINISHED|LeadInsert on Lead trigger event BeforeInsert|__sfdc_trigger/LeadInsert
13:01:13.537 (1610792921)|DML_END|[5]
13:01:13.537 (1611266013)|VF_PAGE_MESSAGE|There's a problem with this state, even though it may appear correct. Please select a state from the list of valid states.
13:01:13.537 (1611458601)|EXCEPTION_THROWN|[5]|System.DmlException: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, There's a problem with this state, even though it may appear correct. Please select a state from the list of valid states.: [State]
13:01:13.537 (1612090942)|HEAP_ALLOCATE|[5]|Bytes:216
13:01:13.537 (1612311151)|FATAL_ERROR|System.DmlException: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, There's a problem with this state, even though it may appear correct. Please select a state from the list of valid states.: [State]

I would think if the value is changed before trigger, it would accept the value.  I don't undestand why it is still complaining.

Any ideas how to get this to work????
Ken Koellner 7Ken Koellner 7

I do know that the State and Country/Territory Picklists feature does allow integration values.  And I considered using that.  The problem with that feature appears to be that it isn't a map only on API input.  It's like an encode/decode table.  The database field State will contain "TX" and the user will simple see "Texas" on the record when they view the record.  The busess users want all the processing to use the "Texas" value everywhere so all that will have to be rethought if we used the integration value feature.   
The documentation says re Integration Value https://help.salesforce.com/s/articleView?id=sf.admin_state_country_picklists_integ_values.htm&type=5 ...


By default, Name and Integration Value fields for your states and countries contain identical values. The value in the Name field displays to users who interact with your picklist. Integration Value is used by:

  • Apex classes and triggers
  • Visualforce pages
  • SOQL queries
  • API queries and integrations
  • Rules for assignment, AutoResponse, validation, and escalation
  • Workflow rules
  • Email templates
  • Custom buttons and links
  • Field set customizations
  • Reports and list views