You need to sign in to do that
Don't have an account?
JJames
Problem updating custom field in Apex Trigger
I am getting an error when i run the trigger shown below. The error I get is:
"Error:Apex trigger acctTeamUpdate caused an unexpected exception, contact your administrator: acctTeamUpdate: execution of AfterUpdate caused by: System.FinalException: Record is read-only: Trigger.acctTeamUpdate: line 23, column 1"
and my trigger:
it says that the
a.Rep_Agency__c field is read only? And the Rep_Agency__c field is of type Lookup(Account)
Any idea why I might be getting this error?
Thanks.
"Error:Apex trigger acctTeamUpdate caused an unexpected exception, contact your administrator: acctTeamUpdate: execution of AfterUpdate caused by: System.FinalException: Record is read-only: Trigger.acctTeamUpdate: line 23, column 1"
and my trigger:
trigger acctTeamUpdate on Account (after update, after insert) { for (Account a : Trigger.new) { Territory__c t; try { // get the Territory record for the territory specified in the Account Record t = [SELECT ID, Name, ASD_Sales_Rep__c, PSD_Sales_Rep__c, Service_Sales_Rep__c, Switch_Sales_Rep__c, Transmission_Sales_Rep__c, Rep_Agency__c FROM Territory__c WHERE ID=:a.Territory__c LIMIT 1]; } catch(exception ex){ System.debug(' Failed Territory Record Retrieval'); } if(Trigger.isInsert) { } if(Trigger.isUpdate) { try { a.Rep_Agency__c = t.Rep_Agency__c; } catch(exception ex) { System.debug(' Failed Account Update'); } } } }
it says that the
a.Rep_Agency__c field is read only? And the Rep_Agency__c field is of type Lookup(Account)
Any idea why I might be getting this error?
Thanks.
You should store it in a list or map and then at end of your trigger loop you can update that list.
Please try below code and let me know if you get any errors.
You should read this : Click Here (https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_triggers_context_variables_considerations.htm)
Here is the sample code for scenario and let me know if you still have any issue. Thanks,
Pramodh
And on the last solution from Pramodh, you put ad.Compliant__c , should this be ad.id instead?
That's what I suspected, I tried that and it looks like it may be getting stuck in a recursive state as I get the error:
Error:Apex trigger acctTeamUpdate caused an unexpected exception, contact your administrator: acctTeamUpdate: execution of AfterUpdate caused by: System.DmlException: Update failed. First exception on row 0 with id 001Z0000015o4SbIAI; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, acctTeamUpdate: maximum trigger depth exceeded Account trigger event AfterUpdate for [001Z0000015o4Sb] Account trigger event AfterUpdate for [001Z0000015o4Sb] Account trigger event AfterUpdate for [001Z0000015o4Sb] Account trigger event AfterUpdate for [001Z0000015o4Sb] Account trigger event AfterUpdate for [001Z0000015o4Sb] Account trigger event AfterUpdate for [001Z0000015o4Sb] Account trigger event AfterUpdate for [001Z0000015o4Sb] Account trigger event AfterUpdate for [001Z0000015o4Sb] Account trigger event AfterUpdate for [001Z0000015o4Sb] Account trigger event AfterUpdate for [001Z0000015o4Sb] Account trigger event AfterUpdate for [001Z0000015o4Sb] Account trigger event AfterUpdate for [001Z0000015o4Sb] Account trigger event AfterUpdate for [001Z0000015o4Sb] Account trigger event AfterUpdate for [001Z0000015o4Sb] Account trigger event AfterUpdate for [001Z0000015o4Sb] Account trigger event AfterUpdate for [001Z0000015o4Sb]: []: Trigger.acctTeamUpdate: line 30, column 1
Here is the solution. Create another class having static boolean variable and static method so you can call from your trigger.
salesforce solution for recursive trigger : https://help.salesforce.com/apex/HTViewSolution?id=000133752&language=en_US (https://help.salesforce.com/apex/HTViewSolution?id=000133752&language=en_US)
Let me know if you get any issues.
Sorry for all the error, I didnt think about the recurrsive Trigger when writing the code.
Here the Code for your scenario. It will also give ability to insert/update the data in bulk. Please try to buliky your code when ever you trying to insert/update/delete. This will help you to write code more consistent way without hitting the governor limits
Please let me know if you still face any issue.
Thanks,
Pramodh