function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
JBabuJBabu 

Issue of "List index out of bounds: 0".

Hi,

 

I am trying to insert test data as shown below:

 

   CODE:

   ----------

 

     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;

 

I am getting the issue of "List index out of bounds: 0" nearthe below peice of code:

 

" stdPbelist.add(new PriceBookEntry(Pricebook2Id=standardPbList[0].id, Product2Id=prodlist.get(j).id, UnitPrice=99, isActive=true, UseStandardPrice=false));"

 

. Please help me on this.

 

Thanks,

JBabu.

Best Answer chosen by Admin (Salesforce Developers) 
SFDC_EvolveSFDC_Evolve

Hi JBabu....

 

Please make sure that You are getting the value in below code :-

 List<Pricebook2> standardPbList = [select id, name, isActive from Pricebook2 where IsStandard = true limit 1];

 

Thanks 

SFDC_Evolve

All Answers

SFDC_EvolveSFDC_Evolve

Hi JBabu....

 

Please make sure that You are getting the value in below code :-

 List<Pricebook2> standardPbList = [select id, name, isActive from Pricebook2 where IsStandard = true limit 1];

 

Thanks 

SFDC_Evolve

This was selected as the best answer
SFDC_EvolveSFDC_Evolve

One More thing .  . If  you are using  Limit 1 ... . 

 

It would suggest you .... Do Not  take List  List<Pricebook2> standardPbList 

JBabuJBabu

Hi,

 

I am getting a record from the query..

JBabuJBabu

I got the issue. I have one record but the version of the record is different, so the data is not being used in the test class.

I need to create a standard record in to pricebook2