You need to sign in to do that
Don't have an account?
rv90
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;
}
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;
}
All Answers
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
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
That error comes when our trigger goes on recursive loop.
You can achieve with before trigger only.
PFB:
Thanks,
Brahma