You need to sign in to do that
Don't have an account?
santhosh 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;
}
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;
}
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
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
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
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