+ Start a Discussion
grandmastergrandmaster 

Account's OwnerID is not updating in Trigger

Hi All

 

I am held up with this and would really appreciate any suggestions, inputs , help.

 

I have a trigger on the Account object. The trigger pulls a user ID from a custom sObject. The User Id is successfully fetched. It also appears in the debug log that the fetched user Id is assigned correctly to the Account's OwnerID. However, when I go back and check the account record, it still shows the previous User Id and not the ID that is fetched from the custom object. 

 

I have pasted the trigger and debug log excerpt below. Any thoughts?

 

TRIGGER

 

trigger SetAccountOwner on Account (before insert, before update) {

Set<Id> accIds = new Set<Id>();
for(Account a:Trigger.new){
accIds.add(a.Id);
}
List<Account> acc = [Select Id, OwnerId, Billing_State_Abbreviation__c, BillingCity from Account where Id In: accIds];
Map<Id,String> accMap = new Map<Id,String>();
String strOwnerAddressKey ;
String strCity ;
List<Entity_Owner_Address_Map__c> ownerAddr = [Select Owner_Address_Key__c, Owner__c from Entity_Owner_Address_Map__c];
for(Account a: acc){
if(a.BillingCity==null){
strCity='DEFAULT';
}
else{
strCity=(String)a.BillingCity;
}
if(a.Billing_State_Abbreviation__c!=null){
strOwnerAddressKey = (String)a.Billing_State_Abbreviation__c + '-' + strCity;
accMap.put(a.Id, strOwnerAddressKey);
}
else{
// If State is absent, dont do anything
}
System.Debug('strOwnerAddressKey:' + strOwnerAddressKey);
strOwnerAddressKey = null; //Clear it
strCity = null; //Clear it //

// Now, loop through accMap and pull Owner Id from ownerAddr list, then update account
String strKeyInLoop;

for(Id ab:accMap.keySet()){
//System.Debug('a:'+a); // a Contains the account Id
strKeyInLoop=accMap.get(ab);
System.Debug('strKeyInLoop'+strKeyInLoop);
for(Entity_Owner_Address_Map__c e:ownerAddr){
System.Debug('e.Owner_Address_Key__c:'+e.Owner_Address_Key__c);
if((String)e.Owner_Address_Key__c==strKeyInLoop){
//ab.Id=a;
System.Debug('e.Owner__c:'+e.Owner__c);
System.Debug('a.OwnerId: Before '+a.OwnerId);
a.OwnerId = (Id)e.Owner__c;

System.Debug('a.OwnerId: After '+ a.OwnerId); // The debug log appears Good here, but the account record isnt actually updated
break;

}
}
}
}
}

 

DEBUG LOG:

 

11:55:33.116 (116084000)|USER_DEBUG|[41]|DEBUG|a.OwnerId: Before 005E0000003QJeKIAW
11:55:33.116 (116174000)|USER_DEBUG|[44]|DEBUG|a.OwnerId: After  005E0000000K9uHIAS
Best Answer chosen by Admin (Salesforce Developers) 
magicforce9magicforce9

If you want the values to be saved in before insert or before update trigger, then you should modify the vaules that are in trigger.new list. But In your case you have actually run a SOQL query and extracted accounts into another List<Account> acc and making modifications to the records in that list and this will not be saved automatically.

All Answers

magicforce9magicforce9

If you want the values to be saved in before insert or before update trigger, then you should modify the vaules that are in trigger.new list. But In your case you have actually run a SOQL query and extracted accounts into another List<Account> acc and making modifications to the records in that list and this will not be saved automatically.

This was selected as the best answer
grandmastergrandmaster

thanks magicforce. You spotted my mistake and I corrected it. The trigger works fine now.

 

Here is the updated code:

 

trigger SetAccountOwner on Account (before insert, before update) {

Set<Id> accIds = new Set<Id>();
for(Account a:Trigger.new){
accIds.add(a.Id);
}
List<Account> acc = [Select Id, OwnerId, Billing_State_Abbreviation__c, BillingCity from Account where Id In: accIds];
Map<Id,String> accMap = new Map<Id,String>();
String strOwnerAddressKey ;
String strCity ;
List<Entity_Owner_Address_Map__c> ownerAddr = [Select Owner_Address_Key__c, Owner__c from Entity_Owner_Address_Map__c];
for(Account a: acc){
if(a.BillingCity==null){
strCity='DEFAULT';
}
else{
strCity=(String)a.BillingCity;
}
if(a.Billing_State_Abbreviation__c!=null){
strOwnerAddressKey = (String)a.Billing_State_Abbreviation__c + '-' + strCity;
accMap.put(a.Id, strOwnerAddressKey);
}
else{
// If State is absent, dont do anything
}
System.Debug('strOwnerAddressKey:' + strOwnerAddressKey);
strOwnerAddressKey = null; //Clear it
strCity = null; //Clear it //

// Now, loop through accMap and pull Owner Id from ownerAddr list, then update account
String strKeyInLoop;

for(Id ab:accMap.keySet()){
//System.Debug('a:'+a); // a Contains the account Id
strKeyInLoop=accMap.get(ab);
System.Debug('strKeyInLoop'+strKeyInLoop);
for(Entity_Owner_Address_Map__c e:ownerAddr){
System.Debug('e.Owner_Address_Key__c:'+e.Owner_Address_Key__c);
if((String)e.Owner_Address_Key__c==strKeyInLoop){
for(Account ax:trigger.new){
if(ax.OwnerId ==a.OwnerId){
ax.OwnerId = (Id)e.Owner__c;
}
}
//ab.Id=a;
//System.Debug('e.Owner__c:'+e.Owner__c);
//System.Debug('a.OwnerId: Before '+a.OwnerId);
//a.OwnerId = (Id)e.Owner__c;
// a.OwnerId ='005E0000000K9uHIAS' ;
//System.Debug('a.OwnerId: After '+ a.OwnerId);
//break;

}
}
}
//a.OwnerId='005E0000000K9uHIAS';
}
}

 

Thanks!