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
John NeilanJohn Neilan 

Mapping Issues

Hello,

I created the class below to fire from a trigger on the Update of a custom object.  The code is designed to update 2 fields on an associated Account with values from the associated custom object.  The Account_Manager_Handoff_Notes__c field updates fine.  However, the Secondary_Owner__c field does not update at all.  Can anyone help me decifer why that field is not updating?  Thanks,

public void addNotes(List<Account_Manager_Handoff__c> accountManagerHandoffs){
        
        Set<Id> accountsInTrigger = new Set<Id>();
        Map<String,String> accountMap = new Map<String,String>();
        List<Account> accountsToUpdate = new List<Account>();
        
        FOR(Account_Manager_Handoff__c amh : accountManagerHandoffs){
            IF(amh.AccountLookup__c != NULL){
            
            accountsInTrigger.add(amh.AccountLookup__c);
            String notes = amh.Platform_Experience__c + '\n\n' + amh.Issues_Encountered__c + '\n\n' + amh.Known_Goals__c + '\n\n' + amh.Additional_Notes__c;
            String owner = amh.Assigned_Account_Manager__c;
            accountMap.put(amh.AccountLookup__c,notes);
            accountMap.put(amh.AccountLookup__c,owner);
                
            }
        }
        
        FOR(Account a : [SELECT
                            Id,Account_Manager_Handoff_Notes__c,OwnerId
                         FROM
                            Account
                         WHERE
                            Id In :accountsInTrigger]){
                             
                             a.Account_Manager_Handoff_Notes__c = accountMap.get(a.Id);
                             a.Secondary_Owner__c = accountMap.get(a.Id);
                             accountsToUpdate.add(a);
                             
                         }
        
        UPDATE accountsToUpdate;
        
    }


Best Answer chosen by John Neilan
kevin lamkevin lam
Try this:

public void addNotes(List<Account_Manager_Handoff__c> accountManagerHandoffs){
       
        Set<ID> accountsInTrigger = new Set<ID>();
        Map<ID,String> accountMap = new Map<ID,String>();
        Map<ID,ID> accountMap2 = new Map<ID,ID>();
        List<Account> accountsToUpdate = new List<Account>();
       
        FOR(Account_Manager_Handoff__c amh : accountManagerHandoffs){
            IF(amh.AccountLookup__c != NULL){
           
            accountsInTrigger.add(amh.AccountLookup__c);
            String notes = amh.Platform_Experience__c + '\n\n' + amh.Issues_Encountered__c + '\n\n' + amh.Known_Goals__c + '\n\n' + amh.Additional_Notes__c;
            ID owner = amh.Assigned_Account_Manager__c;
            accountMap.put(amh.AccountLookup__c,notes);
            accountMap2.put(amh.AccountLookup__c,owner);
               
            }
        }
       
        FOR(Account a : [SELECT
                            Id,Account_Manager_Handoff_Notes__c,Secondary_Owner__c
                         FROM
                            Account
                         WHERE
                            Id In :accountsInTrigger]){
                            
                             a.Account_Manager_Handoff_Notes__c = accountMap.get(a.Id);
                             a.Secondary_Owner__c = accountMap2.get(a.Id);
                             accountsToUpdate.add(a);
                            
                         }
       
        UPDATE accountsToUpdate;
       
    }

All Answers

kevin lamkevin lam
Two things:
  1. You didn't query the Secondary_Owner__c field in your SELECT statement.
  2. The  put statement in line 14 will overwrite the notes entry with the owner entry. If the map previously contained a mapping for a key, the old value is returned by the put method and then replaced.
Jai SureshJai Suresh
Kevin is right. 

In line 14, you are overwriting what ever value is writtein in line 13.

You can create seperate Maps for each field, or create a map<String, Account> to get both values.
John NeilanJohn Neilan
Hi Guys,

Thanks for the feedback.  I tried the code below to create a 2nd map, but it's still not updating the Secondary_Owner__c field.  I'm not all that familiar with maps, so did I do something wrong?  Thanks!

public void addNotes(List<Account_Manager_Handoff__c> accountManagerHandoffs){
        
        Set<Id> accountsInTrigger = new Set<Id>();
        Map<String,String> accountMap = new Map<String,String>();
        Map<String,String> accountMap2 = new Map<String,String>();
        List<Account> accountsToUpdate = new List<Account>();
        
        FOR(Account_Manager_Handoff__c amh : accountManagerHandoffs){
            IF(amh.AccountLookup__c != NULL){
            
            accountsInTrigger.add(amh.AccountLookup__c);
            String notes = amh.Platform_Experience__c + '\n\n' + amh.Issues_Encountered__c + '\n\n' + amh.Known_Goals__c + '\n\n' + amh.Additional_Notes__c;
            String owner = amh.Assigned_Account_Manager__c;
            accountMap.put(amh.AccountLookup__c,notes);
            accountMap2.put(amh.AccountLookup__c,owner);
                
            }
        }
        
        FOR(Account a : [SELECT
                            Id,Account_Manager_Handoff_Notes__c,Secondary_Owner__c
                         FROM
                            Account
                         WHERE
                            Id In :accountsInTrigger]){
                             
                             a.Account_Manager_Handoff_Notes__c = accountMap.get(a.Id);
                             a.Secondary_Owner__c = accountMap2.get(a.Id);
                             accountsToUpdate.add(a);
                             
                         }
        
        UPDATE accountsToUpdate;
        
    }


kevin lamkevin lam
Try this:

public void addNotes(List<Account_Manager_Handoff__c> accountManagerHandoffs){
       
        Set<ID> accountsInTrigger = new Set<ID>();
        Map<ID,String> accountMap = new Map<ID,String>();
        Map<ID,ID> accountMap2 = new Map<ID,ID>();
        List<Account> accountsToUpdate = new List<Account>();
       
        FOR(Account_Manager_Handoff__c amh : accountManagerHandoffs){
            IF(amh.AccountLookup__c != NULL){
           
            accountsInTrigger.add(amh.AccountLookup__c);
            String notes = amh.Platform_Experience__c + '\n\n' + amh.Issues_Encountered__c + '\n\n' + amh.Known_Goals__c + '\n\n' + amh.Additional_Notes__c;
            ID owner = amh.Assigned_Account_Manager__c;
            accountMap.put(amh.AccountLookup__c,notes);
            accountMap2.put(amh.AccountLookup__c,owner);
               
            }
        }
       
        FOR(Account a : [SELECT
                            Id,Account_Manager_Handoff_Notes__c,Secondary_Owner__c
                         FROM
                            Account
                         WHERE
                            Id In :accountsInTrigger]){
                            
                             a.Account_Manager_Handoff_Notes__c = accountMap.get(a.Id);
                             a.Secondary_Owner__c = accountMap2.get(a.Id);
                             accountsToUpdate.add(a);
                            
                         }
       
        UPDATE accountsToUpdate;
       
    }
This was selected as the best answer
John NeilanJohn Neilan
Thanks Kevin, that worked!