+ Start a Discussion

Action function on <apex:page>

I am trying execute action when the page loads. I want to display the details of a record Id (for testing purposes i have hardcoded the Id).


public class MileageExtension {
private final Mileage__c mileageObj;
public MileageExtension(ApexPages.StandardController controller) {
this.mileageObj = (Mileage__c)controller.getRecord();
public Mileage__c[] getTodaysMileageRecords() {
String createdbyId = UserInfo.getUserId();
Mileage__c[] mileageList =
[SELECT name, miles__c
FROM Mileage__c
WHERE Date__c <= TODAY
AND createdbyid = :createdbyId];
return mileageList;

public PageReference InitMileageCustomview() {
PageReference secondPage = Page.MileageCustomview;
return secondPage;



 And My Page is




<apex:page standardController="Mileage__c" extensions="MileageExtension" action="{!InitMileageCustomview}">
<h1>{!$User.FirstName}'s Mileage Page</h1>
<apex:pageBlock >
<apex:pageBlockSection title="Today’s Mileage Records">
<apex:dataTable value="{!TodaysMileageRecords}"
var="mileage" styleClass="list">
<apex:column >
<apex:facet name="header">Name</apex:facet>
<apex:outputText value="{!mileage.Name}"/>
<apex:column >
<apex:facet name="header">Miles</apex:facet>
<apex:outputText value="{!mileage.Miles__c}"/>
<apex:detail subject="{!Mileage__c.Contact__c}" relatedList="false"/>




When I click the visualforce tab it keeps running and running (infinite loop....).  How can I get the detail record whenever the page gets loaded?


Thank you.





There is no need for an action method in your page. If you are using the standard controller and are passing the Id attribute you don't even need the query as you can access values my using merge syntax {!Milage__c.SomeField}.
Message Edited by TehNrd on 07-28-2009 10:04 AM

It loads Mileage Records. I am trying to get the related contact detail (i am hardcoding ContactID for demo purpose) record of a specific Mileage Record  also be populated when I click the visualforce page.


<apex:detail subject="{!Mileage__c.Contact__c}" relatedList="false"/>


Rajesh ShahRajesh Shah
In the InitMileageCustomView function, you are redirecting the user back to the MileageCustomView page. The page gets loaded again which again calls the InitMileageCustomView function. Hence the infinite loop.