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
Apex Trigger
- trigger QuoteAutoSync on Quote (after insert)
- {
- Map<Id, Id> quoteMap = new Map<Id, Id>();
- for(Quote currentQuote : Trigger.New)
- {
- quoteMap.put(currentQuote.Id, currentQuote.OpportunityId);
- }
- QuoteAutoSyncUtil.syncQuote(quoteMap);
- }
Apex Class
- public class QuoteAutoSyncUtil
- {
- @future
- public static void syncQuote(Map<Id, Id> quoteMap)
- {
- Map<Id, Opportunity> oppMap = new Map<Id, Opportunity>();
- for(Id currentQuote : quoteMap.keyset())
- {
- Opportunity opp = new Opportunity();
- opp.Id = quoteMap.get(currentQuote);
- opp.SyncedQuoteId = currentQuote;
- oppMap.put(opp.Id, opp);
- }
- update oppMap.values();
- }
- }
Test Class
- @isTest
- private class TestQuoteAutoSync
- {
- static testMethod void insertQuote()
- {
- Opportunity opp = new Opportunity();
- opp.Name = 'Test Opportunity';
- opp.StageName = 'Prospecting';
- opp.CloseDate = system.today();
- insert opp;
- Quote quo = new Quote();
- quo.Name = 'Test Quote';
- quo.OpportunityId = opp.Id;
- Test.startTest();
- insert quo;
- Test.stopTest();
- Opportunity o = [select SyncedQuoteId from opportunity where id=:opp.Id];
- system.assert(o.SyncedQuoteId != null);
- }
- }
Great. Thanks for this information
ReplyDeleteThis 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:
ReplyDelete@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;
}
}
Hi DaNe, I have updated this post with test class. Hope this will solve your problem.
ReplyDeletePerfect. Thank you!
ReplyDeleteSo great. Thank you!
ReplyDeleteWorks great! Thank you!!!
ReplyDeleteDo you have one that is similar for Opportunity Line Item with Quote Line Item?
ReplyDeleteStill I am getting this error
ReplyDeleteOppty_renewal: execution of BeforeUpdate caused by: System.UnexpectedException: The opportunity SyncedQuote field is read only within a trigger.:
Nice to be visiting your blog again, it has been months for me. Well this article that i've been waited for so long. I need this article to complete my assignment in the college, and it has same topic with your article. Thanks, great share. urdughr
ReplyDeletehttps://dimpoetry.com/sad-poetry-sad-poetry-in-urdu-sad-poetry-images/
ReplyDeleteTHANK YOU SO MUCH FOR YOUR INFORMATION AND GUIDE AND HELP. I REALLY ENJOY THIS CONTENT. I HOPE YOU PROVIDE US AGAIN SAME THING.
ReplyDeleteUrduSadPoetry
amazing bhai
ReplyDeleteThanks for you kind information and help. This is really a interesting information regarding the topic if you want to you know about more check below.
ReplyDeleteLove Poetry In Urdu
Khalil Ur Rehman Qamar Poetry
Attitude Poetry In Urdu
THANK YOU SO MUCH FOR YOUR INFORMATION AND GUIDE AND HELP. I REALLY ENJOY THIS CONTENT. I HOPE YOU PROVIDE US AGAIN SAME THING.
ReplyDeleteUrdu Jokes
Thank you so much for such a great information. Sad Punjabi Status
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteurdu sad shayari in urdu click here
ReplyDeleteIts a very informative blog and helpful Desi status in punjabi
ReplyDelete