+ Start a Discussion
rv90rv90 

After Update Trigger throwing error (DML error)

below is my trigger, I am trying to update a field  when every a user try to update a state field as 'Michigan' then the Physical COuntry Field should automatically change to 'USA'.  Please tell me where am i doing wrong.
Can i use Update  dml statement for after update trigger. because
Below code is throwing error. 

trigger fUpdate on Financial_Account__c (after update) {
    List<Financial_Account__c> acc = [SELECT Id,name, physical_country__c FROM Financial_Account__c WHERE Id IN: Trigger.newMap.keySet()];
system.debug(''+acc);
    for (Financial_Account__c a : acc) {
If(a.state__c == 'Michigan'){
        a.physical_country__c = 'USA';
}
 
  }
   update acc;
 
   
}

User-added image
 
Best Answer chosen by rv90
Andrew EchevarriaAndrew Echevarria
Hmm a before trigger may be appropriate for this, try this:
 
trigger fUpdate on Financial_Account__c (before update) {
    for(Financial_Account__c acc : trigger.new){
            if(acc.State__c == 'Michigan' && acc.physical_country__c != 'USA'){
                  acc.physical_country__c = 'USA';
            }
  }

}

 

All Answers

Andrew EchevarriaAndrew Echevarria
You don't need to do the soql query, please try the revised trigger below.
List<Account> accsToUpdate = new List<Account>();
trigger fUpdate on Financial_Account__c (after update) {
    for(Account acc : trigger.new){
            if(a.State__c == 'Michigan' && a.physical_country__c != 'USA'){
                  a.physical_country__c = 'USA';
            }

 
  }
}

if(accsToUpdate.size() > 0){
   update accsToUpdate;
}

Also, please take the time to review documentation on triggers and bulk triggers to ensure you meet governor limits:
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_triggers.htm
Andrew EchevarriaAndrew Echevarria
Minor mistake in my code, switch lines 1 and 2
rv90rv90
trigger fUpdate on Financial_Account__c (after update) {
    
    List<Financial_Account__c> accsToUpdate = new List<Financial_Account__c>();
    for(Financial_Account__c acc : trigger.new){
            if(acc.State__c == 'Michigan' && acc.physical_country__c != 'USA'){
                  acc.physical_country__c = 'USA';
            }

 
  }
    if(accsToUpdate.size() > 0){
   update accsToUpdate;
}
}

Thanks Andrew for helping.

FOr the above code : I am getting below error 

Error: Invalid Data. 
Review all error messages below to correct your data.
Apex trigger fUpdate caused an unexpected exception, contact your administrator: fUpdate: execution of AfterUpdate caused by: System.FinalException: Record is read-only: Trigger.fUpdate: line 6, column 1
brahmaji tammanabrahmaji tammana
Hi,

That error comes when our trigger goes on recursive loop.

You can achieve with before trigger only.
PFB:
trigger fUpdate on Financial_Account__c (before update) {    
    for (Financial_Account__c a : Trigger.New) {
        If(a.state__c == 'Michigan'){
            a.physical_country__c = 'USA';
        } 
  	}
}


Thanks,

Brahma
 

Andrew EchevarriaAndrew Echevarria
Hmm a before trigger may be appropriate for this, try this:
 
trigger fUpdate on Financial_Account__c (before update) {
    for(Financial_Account__c acc : trigger.new){
            if(acc.State__c == 'Michigan' && acc.physical_country__c != 'USA'){
                  acc.physical_country__c = 'USA';
            }
  }

}

 
This was selected as the best answer
rv90rv90
Thanks Brahma, Its worked. 
 
brahmaji tammanabrahmaji tammana
Thanks for your reply. Please mark it as best answer so that we can close this thread. :)