0

I have written this trigger up update an field on the account if the field on any opportunity related to the account is checked. At first I had the check box being updated by a workflow but I thought that might be what was causing the problem. I turned off the workflow but still getting the same error. Any help?

Error:Apex trigger OpportunityUpdateAccount caused an unexpected exception, contact your administrator: OpportunityUpdateAccount: execution of AfterUpdate caused by: System.FinalException: Record is read-only: Trigger.OpportunityUpdateAccount: line 5, column 1

trigger OpportunityUpdateAccount on Opportunity( after insert, after update ) {
    List<Account> accsToUpdate = new List<Account>();

    for(Opportunity opp : trigger.new){
        if (opp.Opportunity_Order_Status__c = true )
            accsToUpdate.add(new Account(Id = opp.Account.Id, ACCOpportunity_Order_Status__c = 'true'));
    }

    if (accsToUpdate != null && !accsToUpdate.isEmpty())
        Database.update(accsToUpdate);
}

3 Answers 3

1

Are there any triggers on Accounts that could be trying to update the Opportunity? Try working backwards through the complete Apex log as it may reveal other triggers or workflow rules that have locked the Opportunity.

One suggestion. In addition to checking if the Opportunity_Order_Status__c is true also check if the value is being changed when in an update trigger.

4
  • I have changed this a bit. I removed the workflow and now the trigger looks to see if a stage is greater than 0 and the amount is greater than or equal to 50k. I get a new error. Apex trigger OpportunityUpdateAccount caused an unexpected exception, contact your administrator: OpportunityUpdateAccount: execution of AfterUpdate caused by: System.DmlException: Update failed. First exception on row 0; first error: MISSING_ARGUMENT, Id not specified in an update call: []: Trigger.OpportunityUpdateAccount: line 10, column 1 Apr 25, 2013 at 18:09
  • Here is the new trigger: trigger OpportunityUpdateAccount on Opportunity( after insert, after update ) { List<Account> accsToUpdate = new List<Account>(); for(Opportunity opp : trigger.new){ if (opp.Stage_Calc__c > 0 && opp.Amount >= 50000) accsToUpdate.add(new Account(Id = opp.Account.Id, Active_Opp_over_50kcalc__c = true)); } if (accsToUpdate != null) Database.update(accsToUpdate); } Apr 25, 2013 at 18:12
  • It would appear from the exception message the opp.Account.Id is null for one of the Opportunities. Apr 26, 2013 at 2:58
  • Turned out I just needed to change it to opp.AccountId and it worked! Thanks for the help! Apr 26, 2013 at 23:01
2

Shouldnt

 if (opp.Opportunity_Order_Status__c = true ) 

be

  if (opp.Opportunity_Order_Status__c == true )

Otherwise i would agree with what Daniel says, check if other triggers are locking up the opp

1
  • Yes, as it is written it is trying to set the value to true rather than compare it. That is causing the error.
    – Doug B
    May 25, 2013 at 9:06
-1

SUDO CODE TO UNDERSTAND LOGIC

1.Based on Opportunity_Order_Status__c on opportunity, make changes Opportunity_Order_Status__c on account
A. trigger will be written on opportunity

2. Will it be before or after trigger?
A. After Trigger are used to perform logic on the related objects so it is after trigger

3. How are account and opportunity related ?
A.They are related based on accountid on opportunity

FINAL CODE

Trigger OpportunityOrderStatus on Opportunity(after update, after insert){
Set<Id> accidSet = new Set<id>();
Map<id,Boolean> mapAccountOpp= new Map<id,Boolean>();
List<Account> lstAccountToUpdate = new List<Account>();
if (Trigger.isInsert) {
for(Opportunity opp: Trigger.New){
    if(opp.Opportunity_Order_Status__c !=false && opp.accountid!=null){
        mapAccountOpp.put(opp.accountId,con.Opportunity_Order_Status__c);
        accidSet.add(opp.AccountId);
    } 
}
}

else {
for(Opportunity opp: Trigger.New){
    if(opp.Opportunity_Order_Status__c !=false && opp.Opportunity_Order_Status__c !=System.Trigger.oldMap.get(opp.id).Opportunity_Order_Status__c 
    && opp.accountid!=null){
        mapAccountOpp.put(opp.accountId,opp.Opportunity_Order_Status__c );
        accidSet.add(opp.AccountId);
    } 
}
}
for(Id accId:accidSet){
            Account acc = new Account();
            acc.Id = accid;
            if(mapAccountOpp.containskey(accid))
            acc.Opportunity_Order_Status__c = mapAccountOpp.get(accId);
            lstAccountToUpdate.add(acc);
        } 
        update lstAccountToUpdate;
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .