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
anu123anu123 

urgent:list index outof bounds0

Hi all  ,

      i have a custom vf page button.for this i wrote controller and vf page like this

 

public class accnext{public id idd{set;get;}public boolean error{set;get;}pagereference p;public accnext(ApexPages.StandardController controller) {
}public pagereference next(){
id aid=apexpages.currentPage().getParameters().get('id');
account[] acc=[select id,name,parentid from account where parentid=:aid];system.debug(acc[0].id);system.debug('***'+aid);system.debug(acc.size());idd=acc[0].id;system.debug(idd);if(acc.size()==0){error=true;}else{error=false;pagereference p1=new pagereference('/'+idd);return p1;}return null;}}

 

 

 

page

 

<apex:page standardController="account" sidebar="false" extensions="accnext" action="{!next}" >  <apex:pageMessages />  <apex:detail relatedlist="true"/>  <apex:outputpanel rendered="{!error}">  <apex:outputText >  child record not found:</apex:outputText> </apex:outputpanel></apex:page>

 

its not working showing an error like the listoutofbounds exception. any one please can u help me ..

 

thanks in advance.....

anu.....

rohitsfdcrohitsfdc

is your debug statements returning expected results?

Rahul SharmaRahul Sharma

Firstly you aren't checking if the id from page is coming or not,

And secondly, you are trying to access idd=acc[0].id when the list is blank.

So check both the things,

 

I have updated the code, please refer:

public with sharing class accnext 
{
	public id idd{set;get;}
	public boolean error{set;get;}
	pagereference p;
	public accnext(ApexPages.StandardController controller) 
	{
	}
	public pagereference next()
	{
	String aid=apexpages.currentPage().getParameters().get('id');
	if(aid !='')
	{
		account[] acc = [select id,name,parentid from account where parentid=:aid];
		system.debug('***'+aid);
		system.debug(acc.size());
		if(acc.size()==0)
		{
		    error=true;
		}
		else
		{
		    error=false;
		    idd=acc[0].id;
		    system.debug(idd);
		    pagereference p1=new pagereference('/'+idd);
		    return p1;
		}
	}
	return null;
	}
}

 

anu123anu123

Hi,

    Thank u for ur reply.

          its working but my requirement is when the related child record is not there it showing msg like child record not found.but its not showing the msg redirected to same current page only.can u please send me any changes in this code.

 

My page  is:

 

 

<apex:page standardController="account" sidebar="false" extensions="accnext" action="{!next}" >
  
  <apex:pageMessages />
  <apex:detail relatedlist="true"/>
  <apex:outputpanel rendered="{!error}">
  <apex:outputText >
  child record not found:</apex:outputText>
 </apex:outputpanel>
</apex:page>
 
 
 
controller is:public with sharing class accnext {public id idd{set;get;} public boolean error{set;get;} pagereference p; public accnext(ApexPages.StandardController controller) { } public pagereference next() { String aid=apexpages.currentPage().getParameters().get('id'); if(aid !='') { account[] acc = [select id,name,parentid from account where parentid=:aid];  system.debug('***'+aid);   system.debug(acc.size());   if(acc.size()==0)    {    error=true;   }    else    {     error=false;     idd=acc[0].id;     system.debug(idd);     pagereference p1=new pagereference('/'+idd);     return p1;      }       }        return null;        }         }
 
thanks in advance
 
anu.........
Rahul SharmaRahul Sharma

You have to check with proper condition and show appropriate Page messages.

I hope your checking with the url : /apex/accnext?Id='Some Id' .

 

Try below code.

public with sharing class accnext 
{
	public id idd{set;get;}
	public boolean error{set;get;}
	public accnext(ApexPages.StandardController controller) 
	{
	}
	public pagereference next()
	{
		error=false;
		String aid=apexpages.currentPage().getParameters().get('id');
		if(aid != '')
		{
			account[] acc = [select id,name,parentid from account where parentid=:aid];
			if(acc.size()==0)
			{
				error=true;
				ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'child record not found:'));
				return null;
			}
			else
			{
				pagereference p1=new pagereference('/'+acc[0].id);
				return p1;
			}
		}
		else
		{
			error = true;
			ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Please provide Account Id'));
			return null;
		}
	}
}

 

<apex:page standardController="account" sidebar="false" extensions="accnext" action="{!next}" >
  <apex:pageMessages rendered="{!error}"/>
  <apex:detail relatedlist="true" rendered="NOT{!error}"/>
</apex:page>