1

Im new to salesforce. I have just started with apex and Vf. I have built one vf where on page load it fetches account list and displays it in table. Before displaying it in table it calls another function Details(). I have one button in page. When i click that button it calls the method submit() and performs some operations and navigates to another vf. But the problem is When i click button it executes Details() method first then executes Submit() method. But I want to call only submit() method not details(). Why it calls Details method every time i click submit? How can i prevent calling details method on submit?

Thanks in advance.

I have attached sample vf and apex controller

<apex:page controller="SampleCode">
<apex:form>
    <apex:repeat value="{!AccountList}" var="a">
        <apex:outputField value="{! a.Name}"/>  
    </apex:repeat>
    <apex:commandButton value="Submit" action="{! submit}"/>
</apex:form>

public class SampleCode {
public List<Account> Accounts=new List<Account>();
public List<Account> getAccountList(){

    Accounts=[Select Id,Name from Account];
    Details();
    Return Accounts;
}
public pageReference submit(){
    system.debug(2);
    //code here to perform operation;
    //navigate to another page
}

public void Details(){
    //Details code here
}

}

  • 2
    Welcome to SFSE. Please visit the tour to learn on how to ask a good question. Currently its not clear on where you are exactly stuck and to avoid xy problem, please edit your question with minimal code for us to replicate the problem. – Raul Jun 25 '19 at 9:48
1

When you click on the button, it calls the submit method in your controller and then does a full refresh of the page. When the page is loaded (after the refresh) the constructor gets called and in turn calls your details method. To avoid this, add an id attribute to the apex:form and in your apex:commandButton provide a rerender attribute with the above id.

<apex:form id="formid">

<apex:commandButton value="Submit" action="{! submit}" rerender="formid"/>

Edit: The problem is with your getter method getAccountList. I would suggest to use lazy load as explained in this answer.

Hope it helps

|improve this answer|||||
  • Thanks for your reply. But here details() method is executing before submit() method. And also there is no refresh of page. Because in submit method im navigating to another page. – yakshith bangera Jun 25 '19 at 9:48

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