1

So I have a trigger on Order in a certain condition I need to create another opportunity based on the Order's current opportunity. It was pretty straight forward except for getting the products to copy over.

I know this is a high level description, but can anyone help with how I should go about doing this?

*removed code because it had unrelated issues. Will add back when resolved.

  • All you should need to do is make copies of the OpportunityLineItems, similar to how you've copied your Opportunity. What part specifically are you struggling with? – Alex Tennant Aug 15 '14 at 14:59
  • I don't have a opp id yet to associate copied products to the opp since it hasn't been inserted yet. I'm thinking of keeping two lists. One the opp list I'm inserting and one that is a list of product lists. Then after insert of opps, I can loop through and add opp ids to the products. It just feels like there should be an easier way. – SkipVV Aug 15 '14 at 15:17
  • That's exactly how you'll have to do it. Insert the Opportunity and then insert the OpportunityLineItems once you have the Id, there isn't another way in Apex. – Alex Tennant Aug 15 '14 at 15:20
3

Here is an outline solution that should meet your requirements and be bulkified:

  1. Get the Ids of the Opportunity records that meet your criteria from your quotes and store them in a Set<Id>
  2. Query for those Opportunities with the fields you want to clone and store them in a List<Opportunity>
  3. Loop through this list and add clones of your Opportunities to a Map<Id, Opportunity> which maps the original Opportunity Id to the newly cloned Opportunity
  4. insert your cloned Opportunity records (you can simple call insert on your Map values())
  5. Query for all OpportunityLineItem records with OpportunityId equal to the keySet() of your map and store them in a List<OpportunityLineItem>
  6. Loop through these and put them in a Map<Id, List<OpportunityLineItem>> which maps the OpportunityId to it's OpportunityLineItem records
  7. Loop through your Map<Id, Opportunity> from step 3, use the key of your map (the original Opportunity Id) to get the OpportunityLineItem records from your Map<Id, List<OpportunityLineItem>> in step 6, clone the records, update the Id from the value of your map (the Opportunity record your inserted earlier, which will now have an Id) in your Map<Id, Opportunity> and then add them to a List<OpportunityLineItem>
  8. Finally, insert your list of OpportunityLineItem
| improve this answer | |
0

Im guessing your "certain condition" refers to a trigger running After Update on Order.

If that is the case, you should have no problem to query for the Opp's OppLineItems, create a new set based on the properties of this old set (all the variables you want to keep, except the ones that are system created like ID and such) and link them properly.

But you already seem to know that, what is specifically your problem at "getting the products to copy over?" Do you have any sample code or anything that we could look at to help you?

As a wild guess, keep in mind that if you query an object you have to specify every field you want to fetch of it, for example, if you query an Account with the SOQL "[Select Name from Account Where SOMETHING]", and you create a new Account with the info you just fetched, you won't have anything else but the Name of it.

Hope any of this helped you and not complicated things more for you! Cheers!

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.