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
Nazneen ArabNazneen Arab 

Apex Exception in sandbox

Hi Everyone,

There is an apex code that throws an exception 'System.ListException: List index out of bounds: 0
Class.NewEventsBudget.<init>: line 14, column 1' .

This exception occurs only in the sandbox and is working completely fine in production. I have pasted the code below. Line marked in bold is line 14 of the code. Can some body please help me with this? 


This is the code:
   public class NewEventsBudget {
    public Events_Budget__c eventBudget {get; set;}
    public Order order {get; set;}
    public List<selectOption> suppliers {get; set;}
    
    public string newSectionType {get; set;}
    public boolean isSalesforce {get; set;}
    
    public List<WrapperMainSection> wmainSectionList {get;set;}
    public NewEventsBudget(ApexPages.StandardController stdController){
        this.eventBudget = (Events_Budget__c)stdController.getRecord(); 
        Id proEventId = apexpages.currentpage().getparameters().get('peid');
        
        order = [select Id,Job_Number__c,Project_Name__c,Name from Order where id = :proEventId][0];
        eventBudget.Project_Event_Number__c = order.Id;
        eventBudget.Conversion_Rate__c = 1.0;
        string jobNumber = order.Job_Number__c;
        wmainSectionList = new List <WrapperMainSection>();
        suppliers = new List<selectOption>();
        suppliers.add(new SelectOption('','-'));
        for(Account a : [select name,RecordType.Name, id from Account where RecordType.Name = 'IE Supplier' order by Name]){
            suppliers.add(new selectOption(a.Name, a.Name)); 
        }

Thanks,
Nazneen
 
vijay kumar kvijay kumar k
Hi nazneen

Add if condition above order queried line. 

If(proEventId !=null){
// Give you are entire logic here..
//Query  and remaining logic ..
}

This error because of your getting proEventId null value that's why your not getting record. And 
vijay kumar kvijay kumar k
If you click preview button from developer console obviously your not passing proEventId id. But if you want call same using button or url you just pass proEventId there it will work fine.

Regards Vijay
Nazneen ArabNazneen Arab
Thanks for your response Vijay. 
I would like you to know that 'order = [select Id,Job_Number__c,Project_Name__c,Name from Order where id = :proEventId][0];' , this query gives perfect result if run in the query editor of the sandbox. I get the value of proEventId. 
Also, I am not sure why does the same code work in prod and is giving exception only in sandbox. Is this environment issue? Or I am missing on something here.
Please help!

Thanks,
Nazneen
vijay kumar kvijay kumar k
Hi Nazeen

It is not an environmental issue. The place where opening the page. Whenever the page calls the if apex controller is there automatically it will call the constructor of the class. So if call the page from any preview button here no way to pass 'peid' (Id proEventId = apexpages.currentpage().getparameters().get('peid')) then you will get a list out of box error. if you calling the same from the detail page button or URL link definitely you should pass 'peid ' for your result.

 
Nazneen ArabNazneen Arab
Thanks Vijay.

The problem is solved now. The issue was on the button action. The URL used was absolute and when viewing it in Sandbox, the absolute URL dint work. I changed the button URL to relative and it works now.

Thanks,
Nazneen