2

Why is apex Trigger unable to get value from Country/BillingCountry field in a BEFORE INSERT/UPDATE trigger when State and Country/Territory picklist enabled and data created from salesforce UI in account/lead.

Just to prevent this scenario I have used CountryCode/BillingCountryCode and its working fine when data is created from UI and trigger is getting data.

I wanted to know the reason behind why CountryCode has data and Country doesnot.

3

1 Answer 1

2

When you enable State and Country Codes, the "classic" fields become "calculated", just as they would for formula fields. As such, in a before insert trigger, the value for the classic fields will be null, and in a before update trigger, the value for the classic fields will be whatever-the-value-was-before-saving. When using State and Country Codes, do not use the classic fields in a before trigger.

Example Trigger:

trigger Account on Account (before insert, before update, after insert, after update) {
    switch on Trigger.operationType {
        when BEFORE_INSERT {
            System.debug('BEFORE INSERT');
            System.debug(Trigger.new[0].BillingState);
            System.debug(Trigger.new[0].BillingStateCode);
        }
        when BEFORE_UPDATE {
            System.debug('BEFORE UPDATE');
            System.debug(Trigger.old[0].BillingState);
            System.debug(Trigger.old[0].BillingStateCode);
            System.debug(Trigger.new[0].BillingState);
            System.debug(Trigger.new[0].BillingStateCode);
        }
        when AFTER_INSERT {
            System.debug('AFTER INSERT');
            System.debug(Trigger.new[0].BillingState);
            System.debug(Trigger.new[0].BillingStateCode);
        }
        when AFTER_UPDATE {
            System.debug('AFTER UPDATE');
            System.debug(Trigger.old[0].BillingState);
            System.debug(Trigger.old[0].BillingStateCode);
            System.debug(Trigger.new[0].BillingState);
            System.debug(Trigger.new[0].BillingStateCode);
        }
    }
}

Example Script:

Account a = new Account(Name='Test', BillingStateCode='CO', BillingCountryCode='US');
insert a;
a.BillingStateCode='TX';
update a;

Example Log Output:

08:53:48.2 (277525018)|USER_DEBUG|[4]|DEBUG|BEFORE INSERT
08:53:48.2 (277810300)|USER_DEBUG|[5]|DEBUG|null
08:53:48.2 (279242024)|USER_DEBUG|[6]|DEBUG|CO
08:53:48.2 (855238791)|USER_DEBUG|[16]|DEBUG|AFTER INSERT
08:53:48.2 (855392322)|USER_DEBUG|[17]|DEBUG|Colorado
08:53:48.2 (855472341)|USER_DEBUG|[18]|DEBUG|CO
08:53:49.11 (1075528354)|USER_DEBUG|[9]|DEBUG|BEFORE UPDATE
08:53:49.11 (1075699140)|USER_DEBUG|[10]|DEBUG|Colorado
08:53:49.11 (1075793712)|USER_DEBUG|[11]|DEBUG|CO
08:53:49.11 (1075885991)|USER_DEBUG|[12]|DEBUG|Colorado
08:53:49.11 (1075964464)|USER_DEBUG|[13]|DEBUG|TX
08:53:49.11 (1113667727)|USER_DEBUG|[21]|DEBUG|AFTER UPDATE
08:53:49.11 (1113818457)|USER_DEBUG|[22]|DEBUG|Colorado
08:53:49.11 (1113923014)|USER_DEBUG|[23]|DEBUG|CO
08:53:49.11 (1113999945)|USER_DEBUG|[24]|DEBUG|Texas
08:53:49.11 (1114073514)|USER_DEBUG|[25]|DEBUG|TX
1
  • Thanks a lot @sfdcfox . I have a clear idea about it now :) Dec 23, 2019 at 13:23

You must log in to answer this question.

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