Error Creating Price books in a test utility class.

1,397 views
Skip to first unread message

Karthik

unread,
Jun 12, 2013, 9:45:34 PM6/12/13
to salesforce-p...@googlegroups.com
Hello Guys,

I am trying to create a test utility class which would allow creating pricebook entries for products. However when I call the following method from a test class and pass a list of products, it throws me an error at the highlighted.

System.DmlException: Insert failed. First exception on row 0; first error: DUPLICATE_VALUE, This price definition already exists in this price book: []


 public static list<PricebookEntry>  createPriceBookEntries(Integer NumberOfRecords, list<product2> products) {
        list<PricebookEntry> pbes=new list<PricebookEntry>();
        list <PriceBook2> pb2list = new list<PriceBook2>();
        //get standard pricebook
                Pricebook2 standardPB = [select id from Pricebook2 where isStandard=true limit 1]; 
                // create a standard pricebook before creating custom ones
                for(Integer i = 0; i<NumberOfRecords;i++){
                    PricebookEntry standardPrice = new PricebookEntry(Pricebook2Id = standardPB.Id, Product2Id = products.get(i).id, UnitPrice = ((Math.random() + 1)*100000).intValue(), IsActive = true, UseStandardPrice = false);
                    pbes.add(standardPrice);
                }
                //create a custom proce book
                  for (Integer i = 0; i<NumberOfRecords; i++){
                  pb2list.add(new Pricebook2 (name = 'Test Price book-001', Description = 'Test Custom Price Book', isactive = true));
                  }
                 insert pb2list;
                 // create a custom pbe
                 for(Integer i = 0; i<NumberOfRecords;i++){
                     pbes.add(new Pricebookentry(Pricebook2Id = pb2list.get(i).id, Product2Id = products.get(i).id, UnitPrice = ((Math.random() + 1)*1000000).intValue(), IsActive = true, CurrencyIsoCode = 'USD'));
                 }
                 insert pbes;
            system.debug('print size'+pbes.size());
            system.debug('print pbes'+pbes);
    return pbes;

Can anyone help me understand what needs to be changed in the pricebookentry record to get rid of this?

Thanks
Karthik

ashish yadav

unread,
Jun 14, 2013, 6:16:17 AM6/14/13
to salesforce-p...@googlegroups.com
try to enter unique or new record it will work 


Karthik

--
You received this message because you are subscribed to the Google Groups "salesforce professionals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to salesforce-profess...@googlegroups.com.
To post to this group, send email to salesforce-p...@googlegroups.com.
Visit this group at http://groups.google.com/group/salesforce-professionals?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Charan Vuyyuru

unread,
Jun 14, 2013, 6:17:28 AM6/14/13
to salesforce-p...@googlegroups.com
Karthik,


Just a wild guess. Degrade the class api version and try it.


Thanks



For more options, visit https://groups.google.com/groups/opt_out.
 
 



--


Thanks & Regards,

Charan Tej Vuyyuru

Salesforce.com Certified Force.com Developer


          


Shafi

unread,
Jun 14, 2013, 1:56:44 PM6/14/13
to salesforce-p...@googlegroups.com
Hi Karthik,

In your third for loop you are assigning pricebook to the PricebookEntry record that already got assigned with standard pricebook in the first loop.

are NumberOfRecords not same as number of products in the Porducts list?
do you want to create custom procebook for each product ?
Reply all
Reply to author
Forward
0 new messages