1

I have an Apex command button in my Visual Force page. Using that button I want to call another visual force page, which in turn should call action function written in the controller.

Please advise.

  • You want to navigate to another VF page and execute action function in its controller or just call the action function from another VF's controller? – Vigneshwaran G Apr 8 '16 at 10:52
5

Put the below code in the controller action method that is being called on click of command button


PageReference pr = new PageReference('/apex/yourVFPageName');
pr.getParameters().put('key','value');
pr.setRedirect(true); // If you want a redirect. Do not set anything if you want a forward.
return pr;

Edit: In the constructor of the 'yourVFPageName' page controller you can call any method.

As @Vigneshwaran G has mentioned:

use action attribute in page to call that method. Otherwise if the method being called from constructor contains any DML statements it will throw error.

  • 1
    If the method being called from constructor contains any DML statements it will throw error. So use action attribute in page to call that method. – Vigneshwaran G Apr 8 '16 at 11:00
2

visual force page code:

 <apex:commandButton value="button" action="{!redirect}"/>

in the controller define this function:

public PageReference redirect()
{
   PageReference pr = new PageReference('/apex/yourVFPageName');
   return pr;
}
1

visual force code:

<apex:commandButton value="redirect" action="{!redirect}"/>

controller:

public PageReference redirect() {
    return Page.existingPageName;
}

Advantage of declaring this way is:

PageReference for a Visualforce page that has already been saved in your organization. By referring to a page in this way, the platform recognizes that this controller or controller extension is dependent on the existence of the specified page and will prevent the page from being deleted while the controller or extension exists.

PageReference Class

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.