1

Error: Invalid Data. Review all error messages below to correct your data. Apex trigger QuoteTriggers caused an unexpected exception, contact your administrator: first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, SyncQuote: execution of AfterUpdate caused by: System.FinalException: Record is read-only Trigger.SyncQuote: line 8, column 1: []: Trigger.QuoteTriggers: line 13, column 1

Code:

Trigger SyncQuote on Quote(after insert, after update)
 {
   List<Opportunity> oppsToSync = new List<Opportunity>();
   for(Quote newQuote : Trigger.new)
    {
       Quote oldQuote = Trigger.oldMap.get(newQuote.Id);
       if(newQuote.Finalization__c = True)
         {
           oppsToSync.add(new Opportunity(Id = newQuote.OpportunityId,SyncedQuoteId = newQuote.Id));
         }
    }
     update oppsToSync;
 }
0

2 Answers 2

1

You cannot update the SyncedQuoteID of the Opportunity in the same transaction of Quote being inserted. Its one of the SF Limitations. The solution here is to move the update of opportunity into a different transaction. You can do that using future/queuable.

public class OpportunitySynerService{

    @future
    public static void updateOppWithSyncedQuoteId(Map<Id,Id> quoteToOppId ){
        List<Sobject> tobeUpdatedList  = new List<Sobject>();
        for(Id quoteId : quoteToOppId.keySet()){
             tobeUpdatedList.add(new Opportunity(Id=quoteToOppId.get(quoteId) ,SyncedQuoteId =quoteId ));
        }
        update tobeUpdatedList;

    }
}

Trigger:

Trigger SyncQuote on Quote(after insert,after update) 
 {
    Map<Id,Id> quoteToOppId = new Map<Id,Id>();
   for(Quote newQuote : Trigger.new)
    {

       if(newQuote.Finalization__c == True) 
         {
             quoteToOppId.put(newQuote.ID,newQuote.oPPORTUNITYiD);

         }
    }
     if(!quoteToOppId.isEmpty()) 
     OpportunitySynerService.updateOppWithSyncedQuoteId(quoteToOppId ); 
 }

https://nefdev.wordpress.com/2012/05/30/automatically-sync-new-quote-with-opportunity/

3
  • 2
    Safer side is if(newQuote.Finalization__c) which always evaluates condition as true Commented Nov 9, 2018 at 10:20
  • I already tried this to fix an error but still it shows an same error message while saving the quote with finalization check box checked.
    – Heisenberg
    Commented Nov 9, 2018 at 10:22
  • @Heisenberg updated code with the fix and reason. Commented Nov 9, 2018 at 11:00
0

To update the Opportunity for Synced Quote value, just use Process Builder which works fine for me. Here I didn't mention any criteria for sake of testing but you should mention the criteria as Finalization to be true.

No trigger is required.

Update Oppty

0

You must log in to answer this question.

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