//Refactored code after getting inputs from @David

public void HandleAfterInsert(){ 
    system.debug(LoggingLevel.DEBUG, '***Inside after insert- ' );

        for(Account account : newAccs){
         if((account.Location__Latitude__s == null)  && (String.isNotBlank(account.MapAddressCountry__c))){
                System.debug(LoggingLevel.DEBUG,'***Geocoding Account - ' + account.Name);

public void HandleAfterUpdate(){
      system.debug(LoggingLevel.DEBUG, '***Inside after update- ' );

        for(Account account : newAccs) {

            //check if Map Address has been updated
            Boolean addressChangedFlag = false;

                if(Trigger.isUpdate) {
                    Account oldAccount = (Account)Trigger.oldMap.get(account.Id);

                        if((account.MapAddressStreet__c != oldAccount.MapAddressStreet__c) ||
                            (account.MapAddressCity__c != oldAccount.MapAddressCity__c) ||
                            (account.MapPostalState__c!= oldAccount.MapPostalState__c) ||
                            (account.MapAddressPostalCode__c!= oldAccount.MapAddressPostalCode__c) || 
                            (account.MapAddressCountry__c!= oldAccount.MapAddressCountry__c)) {

                                addressChangedFlag = true;

                                System.debug(LoggingLevel.DEBUG, '***Address changed for - ' +oldAccount.Name);
                 if(((account.Location__Latitude__s == null) || (addressChangedFlag == true)) && (String.isNotBlank(account.MapAddressCountry__c))) {

                System.debug(LoggingLevel.DEBUG,'***Geocoding Account - ' + account.Name);

enter image description here

  • Welcome to Salesforce Stack Exchange and thank you for posting a very complete question. It's tough to find issues without a concrete example. Are you able to pin down a reproduction of the bug? Please make sure not to include API keys or other sensitive values in your pasted code. – David Reed Feb 3 '18 at 12:28
  • Dear David thanks for your response.At least I got one response from community.Actually I figured out that trigger is working fine when updating is happening. When I am creating a new account on UI(without using data loader) and providing Map addresses and clicks save then also coordinates are not stored.If I clicks on Edit button again and clicks Save then coordinates are getting saved/stored even though I didn't make any changes(simply clicks on edit and save on account record detail page).I don't understand why triggers is not working for insertion &only working on updating.I need real help – Harjeet Singh Feb 4 '18 at 12:54
  • Dear @DavidReed I hjave refactored code and included the sample data screenshots of account UI and edited the question – Harjeet Singh Feb 5 '18 at 4:25

You appear to have some copy-paste issues. The code in your after insert handler is checking to see whether the account's address fields have changed. Since you're in an after insert event, you're comparing the account against itself, and the if statement below will always be false.

          if((account.MapAddressStreet__c != newAccount.MapAddressStreet__c) ||
                (account.MapAddressCity__c != newAccount.MapAddressCity__c) ||
                (account.MapPostalState__c!= newAccount.MapPostalState__c) ||
                (account.MapAddressPostalCode__c!= newAccount.MapAddressPostalCode__c) || 
                (account.MapAddressCountry__c!= newAccount.MapAddressCountry__c)) {

Hence, addressChangedFlag is never set to true. Now, your following logic should run the geocoding anyway, provided you populate the country field, but since we can't see your input data it's not clear that that's the case.

I think this is a good push to refactor your code, remove the copy-pasted lines that don't belong there, and try again. Even if this exercise doesn't instantly fix your problem, it should make your code clearer and easier to debug to find out where the real issue lies. There's way too much logic here that doesn't make sense. (There's also no need to check Trigger.isInsert in your after insert handler - that's what your handler framework is for).

If you are finding that your debug statements are tracing a path through the "wrong" trigger handler, you have an issue at the trigger framework level, and you need to debug it there.

  • Dear @Davis thanks for your response. Yes indeed I refactor my code according to your suggestion and also you are right I am checking country fields should not be null. User has to fills map addresses fields(country,city,state,postal code etc) and clicks on save and corresponding coordinates will be stored in location field and latitude and longitude values for the same coordinates will be stored in long field and lat field in UI. Also if an user doesn't fills value for country field and fills rest of the map addresses fields then also coordinates should not be stored. – Harjeet Singh Feb 5 '18 at 2:37
  • Dear @Davis Reed, I have refactored my code and tried again to create a new account but still I can see flow is going inside ***Inside after update- debug logs under after update method in my code. Even though future method runs and on refresh of page coordinates are populating but why it is an update operation instead of an Insert opertaion. My worry point is if somebody inserts say 10K accounts records through data loader then whether coordinates will be populating or not because its updation happening not insertion. – Harjeet Singh Feb 5 '18 at 2:40
  • Below is my refactored code: – Harjeet Singh Feb 5 '18 at 2:41
  • @Harjeet, please edit your question with what you've tried/found rather than long comment threads. – David Reed Feb 5 '18 at 2:43
  • Dear @David I have edited my question for refactored code as suggested by you. I tried creating one account and provided map addresses including country fields and clicks on Save and checks mu debug logs. On account detail page after refreshing of page coordinates are getting successfully stored but in debug logs I can see ***Inside after update- instead of ***Inside after insert-. I was creating a new account not modifying/updating existing one then why flow went there. – Harjeet Singh Feb 5 '18 at 2:54

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.