function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Ayan HoreAyan Hore 

Updating Lead.StateCode in before Insert trigger leads to Field Integrity Eexception

Hi,

I've a requirement where some external values (in Japanese) are being passed to Lead State. I need to update the StateCode based on some pre-defined mapping (Japanese to valid StateCode). This mapping maps external values to existing Salesforce Country/State picklist values. I'm trying to unit test this using SOAPUI. However, when I'm passing these values, I'm getting the following error:
<message>There's a problem with this state, even though it may appear correct. Please select a state from the list of valid states.: State/Province</message>
<statusCode>FIELD_INTEGRITY_EXCEPTION</statusCode>

The coding for changing the mapping is implemented in Before Insert/Update context and I can see from the logs that the value are being changed properly, however, this error still remains (Ideally it shouldn't, as Im mapping proper values in before context in Trigger).

Any help would be greatly appreciated..!!

Sample:
<urn:sObjects xsi:type="Lead">
            <FirstName>Sample</FirstName>
            <LastName> Lead (EnterPrise WSDL)</LastName>
            <RecordTypeId>SOME_VALUE</RecordTypeId>
            <Company>Personal</Company>
            <Status>Open</Status>
            <Country>Japan</Country>
            <State>東京都</State>
</urn:sObjects>
The "<State>東京都</State>" is Japanese for Tokyo and I can see that it is being mapped properly in Trigger.

-Ayan
Best Answer chosen by Ayan Hore
Ayan HoreAyan Hore
Solved this. There are two parts to this:
  1. Incoming data is in another data type - In my case, it was in Japanese language. To solve this, need to add the Japanese language euivalent (translation) to the field: "Integration Value" in the Country/State picklist. Salesforce matches incoming data with the value in this field, as well as the Name field (English) and performs the appropriate StateCode population.
  2. Incoming data is incorrect - Typically happens with a spelling mistake, if the source system uses a free-text field. In this case there is no straight up solution. However, there is one approach: Define another state/country picklist entry and use the code to perform checks on the incoming data and if there are no match, populate this state/country value instead. This is not a foolproof solution and there are some limitations to it as well. Below are some I can identify:
    1. The "Integration Value" is not accessible in Apex. So, in case the incoming value is in other language (e.g. Japanese), we have no way of checking this. One way to do this is to store the mapping (Integration Value --> Picklist Value) in a custom metadata type and use that one. This will actually lead to maintenance issues in the future as well.
    2. There is a known issue which prevents updating the state/country picklist value in before trigger context. Until Salesforce resolved this issue, the above solution (#1 above) cannot be implemented. Find out more here: https://success.salesforce.com/issues_view?id=a1p30000000T4OMAA0
This is the information that I could gather at this moment. Further additions/opinions are welcome..!!

-Ayan