function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Elizabete FonsecaElizabete Fonseca 

Sync Quote through apex class deleting Opportunity Line Items

Hi there! 

I'm trying to automate the sync on Quote creation through apex code and I am able to sync the Opportunity with the Quote BUT instead of syncing OpportunityLineItems and QuoteLineItems, it's deleting the OpportunityLineItems from the Opportunity. 

Does anyone knows how to auto-sync but avoid deletion of OpportunityLineItems?

Here's the code: 
insert quote;
		
if(autosync){
			
	opportunity.SyncedQuoteId = quote.Id;
	update opportunity;
		
}


Cheers,
EF

GauravGargGauravGarg
Hi Elizabete,

Please use below approach to achieve it.
 
insert quote;
		
if(autosync){
			
	opportunity.opp.setSyncedQuoteId(new ID(quote.id));
	update opportunity;
		
}


Hope this helps!!!

Thanks,

Gaurav
Skype: gaurav62990

Elizabete FonsecaElizabete Fonseca
Hi Gaurav!

Thanks for your response, but it did not work for me because...
- IDs don't have constructors, so this won't work:
new ID(quote.id)
- and if I changed to this:
opportunity.opp.setSyncedQuoteId(quote.id);
or this:
opportunity.setSyncedQuoteId(quote.Id);
also does not work, since it doesn't seem like this method exists - setSyncedQuoteId().

Did you try this and it worked? Would you be able to share?

Appreciated :)

Best regards
EF
Bruno AraújoBruno Araújo
Hi Elizabete!

This error hapens because when you create a quote via apex, it does't bring the products from the opportunity automatically.

So, when you synchronize the quote with the opportunity, it clears the opportunity products because the quote has no items.

However, in manual creation, it already brings all items manually.

To solve this, you must create the quote line items via apex too.

Best Regards!

Bruno