Skip to main content
Hi,

My trigger clones an opportunity when it is set to closed won or when the account program status is set to on Program. It works fine, but my problem is in data loader bulk update,  I am adding the values in list and inserting outside the loop. what it does is adding all opportunities to all the account all the accounts. All the opportunities in the list is inserted in all account for update made. How to associate an opportunity to corresponding account. Below is my code:

Opportunity[] oppsToClone = new Opportunity[]{};

List<Opportunity> opptsToInsert = new List<Opportunity>();

List<Opportunity> oppMap = new List<Opportunity>();

Set<Opportunity> oppSet = new Set<Opportunity>();  

for (Opportunity acc:record) 

        {

    Validatios takes place;

     And valid records added to 

    oppsToClone.add(acc);

    if (oppsToClone.size() > 0 )

                        { 

                            for (Opportunity record1:oppsToClone)

                            {

                                Opportunity theClone = record1.clone(false,false); 

                  Values are assigned

                  and inserted in list

                    opptsToInsert.add(theClone); 

             }

         }

        }

if(opptsToInsert.size() > 0) 

{

    insert opptsToInsert;

}
16 respuestas
  1. 16 feb 2015, 7:55
    Replace the above queries and try again

    AggregateResult[] shippingCounts = [SELECT Count(Id) c, OpportunityId FROM OpportunityLineItem WHERE OpportunityId in :ids and (pricebookEntry.product2.ProductCode = 'FedEx') GROUP BY OpportunityId];

    AggregateResult[] discountAmounts = [SELECT Count(Id) c, OpportunityId FROM OpportunityLineItem WHERE OpportunityId in :ids and (pricebookEntry.product2.ProductCode = 'DiscountAmount') GROUP BY OpportunityId];