You need to sign in to do that
Don't have an account?
Amit Jadhav 13
System.ListException: List index out of bounds: 3
This is my class
public with sharing class costsheet {
public List<Order> ordwrap {get;set;}
public List<Opportunity> oppwrap {get;set;}
public List<SOWrapper> wrapper {get;set;}
public costsheet()
{
ordwrap=[Select id from Order limit 5];
oppwrap=[select Id,name,Account.Name from opportunity limit 5];
wrapper = new List<SOWrapper>() ;
for(Integer i=0 ; i < 5 ; i++)
wrapper.add(new SOWrapper(ordwrap[i] , oppwrap[i])) ;
}
public class SOWrapper{
public Order order{get;set;}
public Opportunity opportunity{get;set;}
//public Cost_Price__c costprice{get;set;}
public SOWrapper(Order ord, Opportunity opp){
order= ord;
opportunity = opp;
//costprice = cp;
}
}
}
public with sharing class costsheet {
public List<Order> ordwrap {get;set;}
public List<Opportunity> oppwrap {get;set;}
public List<SOWrapper> wrapper {get;set;}
public costsheet()
{
ordwrap=[Select id from Order limit 5];
oppwrap=[select Id,name,Account.Name from opportunity limit 5];
wrapper = new List<SOWrapper>() ;
for(Integer i=0 ; i < 5 ; i++)
wrapper.add(new SOWrapper(ordwrap[i] , oppwrap[i])) ;
}
public class SOWrapper{
public Order order{get;set;}
public Opportunity opportunity{get;set;}
//public Cost_Price__c costprice{get;set;}
public SOWrapper(Order ord, Opportunity opp){
order= ord;
opportunity = opp;
//costprice = cp;
}
}
}
Greetings to you!
- I read your problem 'System.ListException: List index out of bounds: 3'.
- In this, if a list has no records or less than 5 records then it will give this error so please use the below code.
- I implemented in my org and sharing below code for your problem[Solved] : -
public with sharing class costsheet {
public List<Order> ordwrap {get;set;}
public List<Opportunity> oppwrap {get;set;}
public List<SOWrapper> wrapper {get;set;}
public costsheet()
{
try{
ordwrap=[Select id from Order limit 5];
oppwrap=[select Id,name,Account.Name from opportunity limit 5];
System.debug('oppwrap-->'+oppwrap);
System.debug('ordwrap-->'+ordwrap);
wrapper = new List<SOWrapper>() ;
for(Opportunity oppInst:oppwrap){
SOWrapper soWrapperObj=new SOWrapper();
System.debug('oppInst-->'+oppInst);
soWrapperObj.opportunity=oppInst;
wrapper.add(soWrapperObj);
}
for(Order ordInst:ordwrap){
SOWrapper soWrapperObj=new SOWrapper();
System.debug('ordInst-->'+ordInst);
soWrapperObj.order=ordInst;
wrapper.add(soWrapperObj);
}
System.debug('wrapper--->'+wrapper);
}catch (Exception e){
System.debug('Msg-->'+e.getMessage()+ 'Line number-->'+e.getLineNumber());
}
}
public class SOWrapper{
public Order order{get;set;}
public Opportunity opportunity{get;set;}
}
}
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks and Regards,
Deepali Kulshrestha.
All Answers
Greetings to you!
- I read your problem 'System.ListException: List index out of bounds: 3'.
- In this, if a list has no records or less than 5 records then it will give this error so please use the below code.
- I implemented in my org and sharing below code for your problem[Solved] : -
public with sharing class costsheet {
public List<Order> ordwrap {get;set;}
public List<Opportunity> oppwrap {get;set;}
public List<SOWrapper> wrapper {get;set;}
public costsheet()
{
try{
ordwrap=[Select id from Order limit 5];
oppwrap=[select Id,name,Account.Name from opportunity limit 5];
System.debug('oppwrap-->'+oppwrap);
System.debug('ordwrap-->'+ordwrap);
wrapper = new List<SOWrapper>() ;
for(Opportunity oppInst:oppwrap){
SOWrapper soWrapperObj=new SOWrapper();
System.debug('oppInst-->'+oppInst);
soWrapperObj.opportunity=oppInst;
wrapper.add(soWrapperObj);
}
for(Order ordInst:ordwrap){
SOWrapper soWrapperObj=new SOWrapper();
System.debug('ordInst-->'+ordInst);
soWrapperObj.order=ordInst;
wrapper.add(soWrapperObj);
}
System.debug('wrapper--->'+wrapper);
}catch (Exception e){
System.debug('Msg-->'+e.getMessage()+ 'Line number-->'+e.getLineNumber());
}
}
public class SOWrapper{
public Order order{get;set;}
public Opportunity opportunity{get;set;}
}
}
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks and Regards,
Deepali Kulshrestha.
Try to put the null check on every condition in your code.
Change your code by this code:
Follow this link also: https://www.biswajeetsamal.com/blog/salesforce-check-a-string-whether-it-is-null-empty-or-blank-using-apex/
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi
The reason of System.ListException: List index out of bounds: 3. You never know weather the order always have the size of 5 or opportunity always have the size of 5. So the code will fail in for loop.
Try Below code :
Hit Kudos if this solve you problem and if this is what you are looking for then please mark it as a solution for other benefits.
Thanks
Dheeraj Kumar
Thanks Dheeraj