0

I am adding an Opportunity Line Item within my Apex code. When I create a Quote, I would like that quote to sync automatically and use the Opportunity Line Item and copy it in the Quote Line Item. I can reproduce that if I am not using my Apex Code. Any idea why it will delete my opportunity Line Items when syncing?

Code:

for(Integer y = 0, aValue = recList3.size(); y < aValue; y++){
            OpportunityLineItem oli = recList3[y];
            List<PricebookEntry> pbe = [SELECT Id FROM PricebookEntry WHERE Pricebook2Id = '01s6g0000037ZybAAE' AND Product2Id =: recList3[y].Product2Id];
            oli.PriceBookEntryId = pbe[0].Id;
        }

This is what I am passing to recList3

[{"Name":"ProductXYZ","OpportunityId":"0068A...","Product2Id":"01t6g0...","Quantity":"1","UnitPrice":"1"}]

For each product in recList3, I am assigning the PricebookEntryId.

Let me know what I am missing. Thanks

4
  • What context is that code running? A trigger (on what object), button click, etc. Commented Apr 7, 2021 at 17:42
  • I have a LWC that is used to create a schedule. Many product (lookup on product) with amounts for different months: ex. Product x, March:2000$, April: 3000$ etc... Once I save that, I am adding a product to the opportunity. Once I create a Quote, I don't want to add that product again so I will sync and get the product from the opp. Hope that help
    – Eric
    Commented Apr 7, 2021 at 17:55
  • one last clarification - are you starting the sync of the quote through apex or manually? Commented Apr 7, 2021 at 17:59
  • It will be manually because there is some validations to be done before syncing and closed won.
    – Eric
    Commented Apr 8, 2021 at 17:37

1 Answer 1

2

The doc on how quote sync works doesn't really get into the nuances much.

There's some different scenarios not explicitly called out that occur below:

  1. If you create a Quote from an existing Opportunity with products already on the opportunity, the Quote Line Items will be created for the quote (although you still need to click "start sync" for it to be syncing). This only seems to apply through the UI when clicking on "New Quote"
  2. When you click on "Start Sync" from a quote, whatever on the quote overrides any existing products on the Opportunity. This applies even if you have nothing on the quote. This the screen you see when clicking on "start sync" on a quote with no line items. enter image description here

Once a quote is syncing, the updates to either object works both ways which is great.

On initial sync, however, it just copies over the quote line items to opportunity line items (one way) based on that quote.

Depending on your order of operations, you may have to ensure users are creating a new quote from an opportunity with your existing products through the UI or have a mechanism to copy it to the Quote before they click "start sync" if using apex in any capacity.

7
  • I see, but I doubt. I did a couple of tests where I am creating all manually and it works well. What I mean is I create an opp, add Opportunity line Items, create a Quote and sync. When doing that manually, the Quote will take the opportunity line items and copy it in the Quote line items. but when trying to do so when I use my code, it replace the opportunity lineitems with nothing... because there is nothing in the Quote.
    – Eric
    Commented Apr 8, 2021 at 19:32
  • I edited my answer to go over scenarios. I'm not clear on what exactly your code example in the question is in reference to? Are you inserting products on the Opportunity after the quote is syncing? If you create the quote in the UI from the Opportunity, the products will copy over to the quote. However, in apex (if you create the quote in code), this doesn't seem to hold true and you'd need to create/copy it yourself Commented Apr 8, 2021 at 21:11
  • let's put aside my code for a min. When I create an opportunity, add a line item, create a Quote, sync. Salesforce will create a Quote LineItem automatically, it does sync and works well. Let's look at my code now. I create an opp, fill my schedule, my code creates an opp LineItem, thats it. I go and create a Quote, sync the quote and boom, a pop up that says my oli will be deleted... Make sense?
    – Eric
    Commented Apr 9, 2021 at 1:41
  • I see no difference whether the Opp product was created manually or in apex - but, I don't have schedules or anything like that. As long as you're creating a "new quote" from the Opportunity manually - the opp products carry over to the created quote in my testing (with no other customizations/validation rules). In terms of your code, Name on OLI is not writeable - that was the only difference when I replicated this. is there more to your code (ex. are you just inserting the list as is?). Commented Apr 9, 2021 at 12:40
  • Hi Kris, I am not inserting the list as is. I am looking at the ID of that product. Sorry, it was not clear. I think the best will be to use Flow to create the Quote and the product. Basically when the status change to presented, I will create a quote copy the oli and add the Quote LIne Item. than once all lines are there I will sync automatically. It will be easier... what do you think?
    – Eric
    Commented Apr 11, 2021 at 23:47

You must log in to answer this question.

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