0

I'm coding a tringger which takes all the new products and inserts them automatically in the Org's pricebooks. Now: -newProd is a list of new Products -notStandardPB is the list of Org's pricebooks Below you find my code:

PricebookEntry notStandardPBE = new PricebookEntry();
        for(Pricebook2 p:notStandardPB){
            for(Product2 prodTopbb : newProd){
                System.debug(prodTopbb.Id);
                notStandardPBE.Pricebook2Id = p.Id;
                notStandardPBE.Product2Id = prodTopbb.Id;
                notStandardPBE.UseStandardPrice = true;
                notStandardPBE.IsActive=true;            
                pbeNotStandardList.add(notStandardPBE);
            }  
        }
        System.debug('non standard pb: '+pbeNotStandardList);
        System.debug('non standard pb: '+pbeNotStandardList.size());
        insert pbeNotStandardList;

When trigger fires the System throws the error: "System.ListException: Before Insert or Upsert list must not have two identically equal elements". Infact seems like the trigger takes only the first product's Id and I don't understand why. Could someone help me please?

1 Answer 1

2

you are not re-initialising the elements you are adding to the collection.

move this line inside the for loop and it should resolve the error

PricebookEntry notStandardPBE = new PricebookEntry();
1
  • Shame on me :P. Thank you, I don't know why but I don't see it Commented Nov 18, 2016 at 11:21

You must log in to answer this question.

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