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
Amit Jadhav 13Amit 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;
        }
    }
    
}
Best Answer chosen by Amit Jadhav 13
Deepali KulshresthaDeepali Kulshrestha
Hi Amit,
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

Deepali KulshresthaDeepali Kulshrestha
Hi Amit,
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.
This was selected as the best answer
Ajay K DubediAjay K Dubedi
Hi Amit,
Try to put the null check on every condition in your code.
Change your code by this code:
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++) {
            if(ordwrap[i].Id != null  && oppwrap[i].Id != null) {
                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;
        }
    }
}

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
DheerajKumar@SalesforceDheerajKumar@Salesforce
Hi Amit

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 :
 
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>() ;
		Integer size = ordwrap.size() >= oppwrap.size() ? ordwrap.size() : oppwrap.size();
		
		for(Integer i=0 ; i < size ; i++){
			Order ord = null;
			Opportunity opp = null;
			
			if(ordwrap.size() > i) ord = ordwrap[i];
			if(oppwrap.size() > i) opp = oppwrap[i];
			
			wrapper.add(new SOWrapper(ord , opp));
		}
	}

  
	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;
		}
    }
}
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
 
Amit Jadhav 13Amit Jadhav 13
thanks Deepali
 
Amit Jadhav 13Amit Jadhav 13
Thanks Ajay
Thanks Dheeraj