+ Start a Discussion
k2sfdevk2sfdev 

Controller function fired twice on every postback

Who can explain this?

 

visual page:

 

<apex:page controller="TestBinding" >

<apex:form>

<apex:pageBlock title="Test Binding">

<apex:pageblockButtons>

<apex:commandButton value="Login"/>

</apex:pageblockButtons>

<apex:pageblockTable value="{!Accounts}" var ="a">

</apex:pageblockTable>

</apex:pageBlock>

</apex:form>

</apex:page>

 Controller

 

 

public class TestBinding {

public List<Account> getAccounts(){

List<Account> li = new List<Account>();

return li;

}

}

 Apex log:

 

 

10:20:11 DEBUG -

***Begining Page Log for /apex/TestBinding 20090901152010.938:

External entry point: returning LIST:SOBJECT:Account from method public LIST:SOBJECT:Account getAccounts() in 0 ms 20090901152010.938:

External entry point: returning LIST:SOBJECT:Account from method public LIST:SOBJECT:Account getAccounts() in 0 ms

Cumulative profiling information: No profiling information for SOQL operations.

No profiling information for SOSL operations.

No profiling information for DML operations. 1 most expensive method invocations: Class.TestBinding: line 2, column 25:

public LIST:SOBJECT:Account getAccounts(): executed 2 times in 0 ms  

***Ending Page Log for /apex/TestBinding?core.apexpages.devmode.url=1

 Any idea to avoid calling getAccounts twice?

 

 

 

 

 

Message Edited by k2sfdev on 09-01-2009 08:35 AM
k2sfdevk2sfdev

I guess one is init the page, the other is rerender the page.

 

Is there something like

 

 

 if ( Apex.PagesPostbackBy('controlID')) then....

 

 

?

 

Thank you  

JimRaeJimRae

You could try not rendering the form on init, and then updating the render, and refresh the pageblocktable on the submit of your button.

 

 

 

<apex:page controller="TestBinding" > <apex:form> <apex:pageBlock title="Test Binding"> <apex:pageblockButtons> <apex:commandButton value="Login" rerender="bTable" /> </apex:pageblockButtons> <apex:pageblockTable value="{!Accounts}" var ="a" rendered="{!showblock}" id="bTable"> </apex:pageblockTable> </apex:pageBlock> </apex:form> </apex:page>

 

public class TestBinding { public boolean showblock=false; public List<Account> getAccounts(){ List<Account> li = new List<Account>(); showblock=true; return li; } }

 

 

 

 

 

k2sfdevk2sfdev

Hi Jim,

 

Thankyou for your reply,

 

 

 getAccounts will not be called when page is loading. ( my version it will be fired once.)

 

 But it's still fired twice when you click commandButton.

 

 

 


Message Edited by k2sfdev on 09-01-2009 01:57 PM
HarmpieHarmpie
It is not executed twice. If you have a good look at the debug log you will see that the line marked in red is not an execution, but a 'summary' line, telling you the the getAccounts method executed in xx ms
JimRaeJimRae
Actually, it does say executed 2 times in 0 MS
HarmpieHarmpie

You're right, misread the post/log.

 

Could you post your entire apex code?

k2sfdevk2sfdev

Hi Harmpie & Jim,

 

My code are actually kind of complicated. I just simplified it as my first post to proof the concept.

If you add counter,you will see it more clearly.

 

 

I tuned my code many times, finally I realized it's force.com framework's problem and no official 

safe methods to avoid it. 

 

 

Thank you