0

I am trying to retrieve the list variable into visualforce page which i have defined in a method inside apex extension class. I am getting error, could anyone please help. In the below code in vf page i am trying to access the list dRange which i have defined in extension class.

VF Page:

<apex:pageBlockSection >
            <table border="1" name="LeaveView" value="{!Submit}">
                <tr><th>LeaveView</th>
                    <apex:repeat value="{!dRange}">
                        <th> </th>
                    </apex:repeat>
                </tr>        
            </table>
        </apex:pageBlockSection>

Apex Extension Class:

public string Submit(){

    this.l = (LeaveTracker__c)controller.getRecord();        
    Date spDate=l.Sprint_Start_Date__c;
    String tm=l.Team_Names__c;
    List<Date> dRange=new List<Date>();
    for(Integer i = 0; i<=13; i++){
        dRange.add(spDate);
        spDate=spDate+1;
    }
2

You are not able to access dRange because it is the local variable inside your submit method. You can access only those variables in visualforce page which are declared as properties i.e. with getters and setters

So, you will have to declare dRange as a property in your controller in order for you to access it in visualforce page

public List<Date> dRange {get;set;}

And in your method, you should initialize it wherever you will be using it first, and then add values to it.

dRange=new List<Date>();
for(Integer i = 0; i<=13; i++){
    dRange.add(spDate);
    spDate=spDate+1;
}

Also, you cannot call the submit method from <table>, I would suggest you can move all your code from submit method to controller's constructor, so your controller's constructor code will be

public List<Date> dRange {get;set;}
public controllerName(ApexPages.StandardController controller){
    this.l = (LeaveTracker__c)controller.getRecord();        
    Date spDate=l.Sprint_Start_Date__c;
    String tm=l.Team_Names__c;
    dRange=new List<Date>();
    for(Integer i = 0; i<=13; i++){
        dRange.add(spDate);
        spDate=spDate+1;
    }
}

And regarding visualforce page, not sure how you are trying to display but you can directly use the property in repeat tag without using the submit method.

<apex:repeat value="{!dRange}" var="d">
    <th>{!d}</th>
</apex:repeat>
| improve this answer | |
  • Yes you are right, in a jiffy I forgot about setter and getters. – Pranay Jaiswal Jun 3 '19 at 18:39
2

List<Date> dRang is a local variable to the Submit() method. You cannot access it in VF page.

To access it in VF page, declare it as a class level variable.

public class MyExtension{
    public List<Date> dRange {get;set;}

    public MyExtension(){
        dRange = new List <Date>();
    }


    public string Submit(){

        this.l = (LeaveTracker__c)controller.getRecord();        
        Date spDate=l.Sprint_Start_Date__c;
        String tm=l.Team_Names__c;
        for(Integer i = 0; i<=13; i++){
            dRange.add(spDate);
            spDate=spDate+1;
        }

    }
}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.