You need to sign in to do that
Don't have an account?
John 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,
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; }
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
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.
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<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;
}