0

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:

  1. How do I get the order items into a List in my trigger?

  2. 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)

1
  • You say order items but then talk about opportunities. Should it be OpportunityLineItem instead or Order instead of opportunities?
    – Eric
    Aug 21, 2015 at 15:35

1 Answer 1

0

An example using Opportunities:

To get children line items

Select ID, (Select ID, Account__c From OpportunityLineItems) From Opportunity

In iterating through the opps when you find one to create a new opp for, place them in a collection

Map<ID,OpportunityLineItem[]> accountTolineItems = New Map<ID,OpportunityLineItem>();
for(Opportunity o : oppQueryResults){
    if(o.StageName = 'Closed Won'){
        for(OpportunityLineItem oli : o.OpportunityLineItems){
            //Not clear on your check of the Account name being equal to what?? but you can add it here
            if(accountToLineItems.containsKey(oli.Account__c))
               accountToLineItems.get(oli.Account__c).add(oli);
            else
               accountToLineItems.put(oli.Account__c,New OpportunityLineItem[]{oli});
        }
     }
}

Then you can cycle through the accountToLineItems to created the new opportunities

2
  • Thanks Eric, how do i access Opportunity Line Item fields as I receive an error with Account__c? Is there somewhere I can view them?
    – Nik
    Aug 21, 2015 at 16:15
  • I just put that in as an example. You need to use the actual API name of the field on the opportunity that links it to an account. I only have your questions to go by. There is no standard field on the Opportunity line item that relates it to an account. The opportunity it related to an account and thus the line items are all by default related to the opportunities account
    – Eric
    Aug 21, 2015 at 16:16

You must log in to answer this question.

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