How to Automatically Sync new Quote with Opportunity in Apex Trigger

Here is the way to Automatically Sync new Quote with Opportunity in Apex Trigger. Before that some of the information about Quote and Opportunity Field Details that are,

In Quote object there is a field called IsSyncing, This is a Read Only field in salesforce. If you run the below snippet in developer console the field result such as isCreatable, isUpdatable is false. So you can’t create/update this field value.

1.    Schema.DescribeFieldResult dfr = Schema.sObjectType.Quote.fields.IsSyncing;
2.    System.debug(dfr);

In Opportunity object there is a field called SyncedQuoteID, If you run the below snippet in developer console the field result such as isCreatable, isUpdatable is True. I.e. you can do insert and update on this field.

1.    Schema.DescribeFieldResult dfr = Schema.sObjectType.Opportunity.fields.SyncedQuoteId;
2.    System.debug(dfr);
But if you update the Opportunity field SyncedQuoteID in a trigger, you will get the below error.

“The opportunity SyncedQuote field is read only within a trigger”

For security reason by default in salesforce trigger you can’t update the SyncedQuoteID field even if you move the update coding in to separate class.

To overcome this problem you need to go for @future, By using this it will go with the separate transaction and the DML operation allowed. The below code snippet will useful to this problem.


Apex Trigger


  1. trigger QuoteAutoSync on Quote (after insert)
  2. {
  3.     Map<Id, Id> quoteMap = new Map<Id, Id>();
  4.     for(Quote currentQuote : Trigger.New)
  5.     {
  6.         quoteMap.put(currentQuote.Id, currentQuote.OpportunityId);
  7.     }
  8.     QuoteAutoSyncUtil.syncQuote(quoteMap);
  9. }

Apex Class

  1. public class QuoteAutoSyncUtil
  2. {
  3.     @future
  4.     public static void syncQuote(Map<Id, Id> quoteMap)
  5.     {
  6.         List<Opportunity> oppList = new List<Opportunity>();
  7.         for(Id currentQuote : quoteMap.keyset())
  8.         {
  9.             Opportunity opp = new Opportunity();
  10.             opp.Id = quoteMap.get(currentQuote);
  11.             opp.SyncedQuoteId = currentQuote;
  12.             oppList.add(opp);
  13.         }
  14.         update oppList;
  15.     }
  16. }
Test Class

  1. @isTest
  2. private class TestQuoteAutoSync
  3. {
  4.     static testMethod void insertQuote()
  5.     {
  6.         Opportunity opp = new Opportunity();
  7.         opp.Name = ‘Test Opportunity’;
  8.         opp.StageName = ‘Prospecting’;
  9.         opp.CloseDate = system.today();
  10.         insert opp;
  11.        
  12.         Quote quo = new Quote();
  13.         quo.Name = ‘Test Quote’;
  14.         quo.OpportunityId = opp.Id;        
  15.        
  16.         Test.startTest();
  17.         insert quo;
  18.         Test.stopTest();
  19.        
  20.         Opportunity o = [select SyncedQuoteId from opportunity where id=:opp.Id];
  21.         system.assert(o.SyncedQuoteId != null);
  22.                
  23.     }
  24. }
This entry was posted in Apex, Trigger. Bookmark the permalink.

4 Responses to How to Automatically Sync new Quote with Opportunity in Apex Trigger

  1. Great. Thanks for this information

  2. DaNae! says:

    This works great but I am having trouble writing an associated test class(es) to go along with it. Can you help? This is what I have so far:

    @isTest
    public class AutoSyncQuote {

    public static testmethod void TestAutoSyncQuote(){
    Account a = new Account();
    a.Name = 'Test Account';
    a.salesReach__Agent_Account_Company_Name__c = '001a000001DbY9F';
    a.salesReach__Agent_Contact_Name__c = '003a000001iM38p';
    a.Account_Status__c = 'Active';
    a.Diamond_Account__c = True;
    a.Net_One__c = False;

    Insert a;

    Opportunity opp = new Opportunity();
    opp.Name = 'Telecom Product or Service';
    opp.CloseDate = Date.today();
    opp.StageName = 'Quote Requested By Client';
    opp.Opportunity_Type__c = 'Install New Service';
    opp.AccountId = a.Id;

    Insert opp;

    Quote q = new Quote();
    q.Name = 'Telecom Product or Service – Quote';
    q.ExpirationDate = opp.Quote_Expiration_Date__c;

    }

    }

  3. Arunkumar says:

    Hi DaNe, I have updated this post with test class. Hope this will solve your problem.

  4. DaNae! says:

    Perfect. Thank you!

Leave a comment