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

Changing an invalid value to valid value for country picklist in before update trigger does not work

We have a piece of Apex code which calls a webservice reads some information from the webservice and updates accounts. 
One of the values it updates is BillingCountry and when the value returned by the webservice is not correct throws an FIELD_INTEGRITY_EXCEPTION.
To fix that I developed a before update and before insert trigger which converts the invalid value to a valid value. still throws an error.
The exact steps to reproduce are:
1. Turn address picklists on.
  2. Write a Before update, before instert trigger for account:
trigger Account_BIBU on Account(before insert, before update) {   
    Account acct = (Account)[0];
    acct.BillingCountry = 'France';
   3. Open the developer console => Debug => Execute Anonimous Apex and execute:
   Account acct = [Select Name, BillingCountry, BillingStreet FROM Account WHERE LIMIT 1];
   acct.BillingCountry ='United States of America';
    update acct;
    4. Execute it. It will throw an error.
    5. Now try to execute the below:
        Account acct = [Select Name, BillingCountry, BillingStreet FROM Account LIMIT 1];
        acct.BillingCountry ='United States';
        update acct;
       It executes fine and BillingCountry is France.

Does anyone have any ideas? Is this a bug in 
As far as I know runs validation rules after before update trigger but in this case they seem to be run before the before update trigger.

Hi , 

This might be due to the Execution Sequence of Triggers as per design, 

See the below Doc for further information,

Hi Ashsih,
Thank you for your reply.
Accoding to the doc you cited when you submit record from Apex the point 2 is not executed and thus the before update trigger is run before any validation rules, or it should be rather. 
I think that this is a bug. If you do the same with another field for example you submit a null value in a field which does not allow nulls and you add a value in a before update trigger it works fine and you do not get exceptions.
I opened a case with but after about a month of back and forth we are still debating on whether this is correct or not.

This is definetely a bug, and it's documented on Salesforce

Any responses from them when this is going to be solved? Thanks