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
santhosh konathala 26santhosh konathala 26 

Hi Team plz help me getting an error like list index out of bounds 1

For the below code while executing

public static list<Product2> ConstructProducts(Integer cnt){
        //ToDo: Ensure this method returns a list, of size cnt, of uniquely named Product2 records
        //  with all the required fields populated
        //  and IsActive = true
        //  an Initial Inventory set to 10
        //  and iterating through the product family picklist values throughout the list.
        list<Product2> products=new list<Product2>();
        list<Schema.PicklistEntry> pEntries = Constants.PRODUCT_FAMILY;
        Integer pVal = 0;
        for(Integer i=0;i<cnt;i++){
            
            Product2 pd2=new Product2();
            pd2.Name='Product-'+i;
            pd2.IsActive = true;
            pd2.Initial_Inventory__c = 10;
            if(pVal == 4){
                pVal = 0;
            }
            pd2.Family = pEntries.get(pVal).getValue();
            pVal++;
        
            products.add(pd2);
        }
        return products;
    }
 
Best Answer chosen by santhosh konathala 26
SwethaSwetha (Salesforce Developers) 
HI Santhosh,

The error "list index out of bounds 1" occurs when the code tries to access an index in a list that does not exist. This can happen when the index is greater than or equal to the size of the list, or when the list is empty

It looks like error is occuring in pd2.Family = pEntries.get(pVal).getValue();

Can you add a check to ensure that pVal is always less than the size of the pEntries list before accessing the list index like 
if(pVal >= pEntries.size()){
    pVal = 0;
}
pd2.Family = pEntries.get(pVal).getValue();
pVal++;

Related: 
https://www.infallibletechie.com/2022/06/sample-code-to-reproduce-issue-list.html
https://developer.salesforce.com/forums/?id=906F00000008yWaIAI
https://sfdctechie.wordpress.com/2018/06/15/system-listexception-list-index-out-of-bounds-explained-with-example/
https://salesforce.stackexchange.com/questions/206518/system-listexception-list-index-out-of-bounds-1-in-apex-share

If this information helps, please mark the answer as best. Thank you

All Answers

SwethaSwetha (Salesforce Developers) 
HI Santhosh,

The error "list index out of bounds 1" occurs when the code tries to access an index in a list that does not exist. This can happen when the index is greater than or equal to the size of the list, or when the list is empty

It looks like error is occuring in pd2.Family = pEntries.get(pVal).getValue();

Can you add a check to ensure that pVal is always less than the size of the pEntries list before accessing the list index like 
if(pVal >= pEntries.size()){
    pVal = 0;
}
pd2.Family = pEntries.get(pVal).getValue();
pVal++;

Related: 
https://www.infallibletechie.com/2022/06/sample-code-to-reproduce-issue-list.html
https://developer.salesforce.com/forums/?id=906F00000008yWaIAI
https://sfdctechie.wordpress.com/2018/06/15/system-listexception-list-index-out-of-bounds-explained-with-example/
https://salesforce.stackexchange.com/questions/206518/system-listexception-list-index-out-of-bounds-1-in-apex-share

If this information helps, please mark the answer as best. Thank you
This was selected as the best answer
santhosh konathala 26santhosh konathala 26
Hi Swetha now my code is working fine.Big Thanks to you..!