1

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

  • Can you post the code of Sales_AutoPopulateState.Sales_AutoPopulateState(Trigger.new); ? – Vijay Ganji May 15 at 9:36
  • I sugest commenting this line Sales_AutoPopulateState.Sales_AutoPopulateState(Trigger.new); to verify if that method is causing the issue. – manjit5190 May 15 at 9:38
2

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);
        }
    }
}
  • Hi, I've commented the method called out in the trigger and it's still showing up as null for Billing country – Greg May 15 at 10:55
  • @Greg, Updated the answer, that should work! – Vijay Ganji May 15 at 12:45
  • Awesome and it works, thank you – Greg May 15 at 15:13
0

You can debug this functionlity by two ways:

  1. Comment below method and check the value of country and state is coming or not in debug log.

Sales_AutoPopulateState.Sales_AutoPopulateState(Trigger.new);

  1. You have used standard Billing country and Billing state field of account in code. The Account Standard Billing fields(all) are the text fields, but from above screenshot, I can see the state and Country are picklist on account, I belive those must be custom fields.

enter image description here

Make sure you are using correct field API names of account object which you have added on page layout.

  • Hi, I've commented the method called out in the trigger and it's still showing up as null for Billing country, The Billing State and Billing Country field is a standard Address Compound field so there are no custom fields there – Greg May 15 at 10:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.