[SalesForce] Before Trigger is not picking up BillingCountry on the Account Object

My class is suppose to pick up The Billing Country and Billing Postal Code and populate the state based on those two fields, however when debugging the before trigger It doesn't pick up the Billing country even though it's populated and it picks up all of the other populated fields.

This only happens on before insert and only happens on the account object, It works fine on Lead and contact objects. There is a state validation rule that doesn't allow the user to save if the state is not populated but the state auto population wouldn't work if the system doesn't recognize the Country being populated on Trigger.new .

Here is what the trigger looks like with the debug:

trigger AccountMasterTrigger on Account (before insert, before update) {
     if (Trigger.isBefore) {
        Sales_AutoPopulateState.Sales_AutoPopulateState(Trigger.new);
        for(Account acts : Trigger.new){
            System.debug('State: '+acts.Billingstate);
            System.debug('PostalCode: '+acts.BillingPostalCode);
            System.debug('Country: '+acts.BillingCountry);
        }
    }
}

Picture of the Billing Account populated on before insert:

enter image description here

Picture of the Logs from the developer console:

enter image description here

Best Answer

You will have to check the code of Sales_AutoPopulateState.Sales_AutoPopulateState(Trigger.new);, you must be nulling out the value of Billing Country field in Sales_AutoPopulateState method.

I tried with your code and created records both in Classic and Lightning (although it should not matter) and I was able to get the value of Billing Country in logs.

You can confirm this by moving your debug statements before calling Sales_AutoPopulateState method, I am sure you would be able to find the value of Billing Country in logs.


Update

But from the screenshot that you mentioned in your question, it appears that you enabled State and Country picklists in your organization because both these fields are displayed as picklists and not text fields.

If that's the case, you will have to use BillingStateCode and BillingCountryCode in your trigger to get the selected values. And your logic should be based on those fields rather than BillingState and BillingCountry.

Change your trigger to below and you should be able to get the values in log

trigger AccountMasterTrigger on Account (before insert, before update) {
     if (Trigger.isBefore) {
        Sales_AutoPopulateState.Sales_AutoPopulateState(Trigger.new);
        for(Account acts : Trigger.new){
            System.debug('State: '+acts.BillingStateCode);
            System.debug('PostalCode: '+acts.BillingPostalCode);
            System.debug('Country: '+acts.BillingCountryCode);
        }
    }
}
Related Topic