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

Get the selected related list Records..

   List<obj> arry=new List<obj>();
    List<objc> sel=new List<obj>();   


public vfpg(ApexPages.StandardSetController controller) {
    arry = (List<obj>) controller.getSelected();
        sel=[select Id,Total_General_Amount__c,Name from FTS__c where Id IN :obj];

        System.debug('arr size'+sel.size()); // returning size correctly

        System.debug('first rec'+sel[0]); // Getting Error here - List Index out of bounds exception.



When you say it returns the size correctly - how many records does it think are in the array?
2 reocrds..

If I print Array alone,it's returning the selected query record properly..


Why the prob when specify [0]..


Looking at your example code, I'm assuming this isn't the real code as it wouldn't compile.


Can you post the real code?  There's no reason why the element of the array wouldn't be available from a conceptual point of view.


Real code:

  public PVS__c PVS{get;set;}

List<PVS__c> arryPVS=new List<PVS__c>();
 List<PVS__c> selPVS=new List<PVS__c>();
Decimal TotAmt=0.0;

public VFTestPage(ApexPages.StandardSetController controller) {
    arryPVS= (List<PVS__c>) controller.getSelected();
    System.debug('Selected PVS******'+arryFTS); -> returning the selected 2 records of related list
        selPVS=[select Id,Total__Amt__c,Name from PVS__c where Id IN :arryPVS];

       System.debug('Selected PVS******'+selPVS); ---> returning the selected 2 records

System.debug('Selected PVS******'+selPVS[0]); ---> Getting Error -List Index out of bounds exception
         for(integer i=0;i<=selPVS.size();i++){
 PVS=[select Id,Total__Amt__c,Name from FTS__c where Id=:selPVS[i].Id];-> ---> Getting Error -List Index out of bounds exception


catch(Exception e){}




I'd expect a list out of bounds exception from this loop:


 for(integer i=0;i<=selPVS.size();i++){

 as if you have 2 records, your iterator will retrieve elements at index 0, 1 and 2.  You need to change this so that the index is less than the size of the list:

 for(integer i=0;i<selPVS.size();i++){

I have no idea why you are getting a list out of bounds exception when accessing the zeroth element of the array - that makes no sense to me.


There's also a couple of areas that I don't understand in the code:


System.debug('Selected PVS******'+arryFTS); -> returning the selected 2 records of related list

 Where does arryFTS come from?  This doesn't appear to be declared anywhere.  Is this the code cut/pasted from your dev org?  It looks like you've edited it again to make the errors clear, but could that mean you have inadvertently changed some of the code?