+ Start a Discussion
JBabuJBabu 

Need urgent help on pricebook entry test records insertion

Hi,

 

I am trying to insert test data as shown below: (but I am getting error message "System.DmlException: Insert failed. First exception on row 0; first error: STANDARD_PRICE_NOT_DEFINED, No standard price defined for this product: []")

 

CODE:

--------

  public static List<PriceBookEntry> pbentries(Integer quantity) {
     
     List <PriceBook2> pb2list = new List<PriceBook2>();
     List <Product2> prodlist = new List<Product2>();
     List <PriceBookEntry> pbelist = new List<PriceBookEntry>();
     
     for (Integer j = 0; j<quantity; j++){
       pb2list.add(new PriceBook2(Name = 'Test Price Book' +j, isActive = true));
     }
     insert pb2list;
     
     for (Integer j = 0; j < quantity; j++) {
       prodlist.add(new Product2(Name = 'Test Product' +j, productCode = 'ABC', isActive = true));
     }
     insert prodlist;
     
     for (Integer j = 0; j < quantity; j++) {
       pbelist.add(new PriceBookEntry(Pricebook2Id=pb2list.get(j).id, Product2Id=prodlist.get(j).id, UnitPrice=99, isActive=true, UseStandardPrice=false));
     }
     
     insert pbelist;
     
     return pbelist;
  }

 

Please help me how to resolve this issue.

 

Thanks,

JBabu  


Best Answer chosen by Admin (Salesforce Developers) 
ShamilShamil

That's strange. Can you try executing the following query from the Force.com IDE:

'select id, name, isActive from Pricebook2 where IsStandard = true limit 1'?

Does it return anything? If not then for whatever reason the standard price book is missing.

What edition are you using, Developer?

All Answers

ShamilShamil

You're having the problem because Salesforce requires having standard prices defined before you can use custom prices.

Technically it means that for the products you're planning to use you need to create price book entries linked to the standard price book. There's only 1 standard pricebook in SFDC.

 

The solution is

 

 List <PriceBook2> pb2list = new List<PriceBook2>();
     List <Product2> prodlist = new List<Product2>();
     List <PriceBookEntry> pbelist = new List<PriceBookEntry>();
     List <PriceBookEntry> stdPbelist = new List<PriceBookEntry>();
     
     //get standard pricebook
     List<Pricebook2> standardPbList = [select id, name, isActive from Pricebook2 where IsStandard = true limit 1];
     
     for (Integer j = 0; j<quantity; j++){
       pb2list.add(new PriceBook2(Name = 'Test Price Book' +j, isActive = true));
     }
     insert pb2list;
     
     for (Integer j = 0; j < quantity; j++) {
       prodlist.add(new Product2(Name = 'Test Product' +j, productCode = 'ABC', isActive = true));
     }
     insert prodlist;
     //before you create custom pricebooks, create standard ones
     for (Integer j = 0; j < quantity; j++) {
       stdPbelist.add(new PriceBookEntry(Pricebook2Id=standardPbList[0].id, Product2Id=prodlist.get(j).id, UnitPrice=99, isActive=true, UseStandardPrice=false));
     }
     insert stdPbelist;
     
     for (Integer j = 0; j < quantity; j++) {
       pbelist.add(new PriceBookEntry(Pricebook2Id=pb2list.get(j).id, Product2Id=prodlist.get(j).id, UnitPrice=99, isActive=true, UseStandardPrice=false));
     }
     
     insert pbelist;
     
     return pbelist;

 

JBabuJBabu

Hi Shamil,

 

Thanks for providing the useful info.

 

When used the new code I am getting following error message:

"System.ListException: List index out of bounds: 0"

 

Please help me on this.

 

Thanks,

JBabu.

ShamilShamil

Make sure your standard pricebook is active. Go to Products tab -> Manage Pricebooks -> Activate the standard pricebook

JBabuJBabu

Hi Shamil,

 

I have activated it and ran the test class. I am getting the same issue.

 

 

ShamilShamil

That's strange. Can you try executing the following query from the Force.com IDE:

'select id, name, isActive from Pricebook2 where IsStandard = true limit 1'?

Does it return anything? If not then for whatever reason the standard price book is missing.

What edition are you using, Developer?

This was selected as the best answer
JBabuJBabu

The query has rows but they are not identifiable because of the verion difference. I have used a function to see all data and I got records.

 

Thanks..