The related list on opportunities contains records of order items which usually have an account name associated with them.
I have a requirement where if an opportunity's stage name is 'closed won', I need to create child opportunities with the account name and all the order items associated with it from the related list on that 'closed won' opportunity.
Here is how I thought I should do this:
- Create a trigger on opportunity with a loop to go through the opportunities
- If the stage name is closed won, get all the records from the order items related list into a new list called, for example, orderItemsList.
- Then loop through this list to create new opportunites with the account name = to the account name associated on the order item.
- The related list on this new opportunity will then contain all the
products that were in the previous closed won opp but only if they match on the account name.
Two challenges I face with this:
How do I get the order items into a List in my trigger?
How do I avoid creating multiple opps for the same account (If there are 2 order items in the related list and they both have the same account name then I would want there to be only one opp created with those 2 items added to the related list)