3

How can I call a controller method from VisualForce page without any onclick event happening? My VisualForce page would be like

<apex:page standardController="Account"> 

    <script>
     /* Here i need to call a controller class with out any onclick event. It should load by itselef */

    </script>

</apex:page>

and my controller class will be like

public class OrderPadController {
    /* this will be the constructor */

    public PageReference openPage() {
         PageReference newpage = new Pagereference('/apex'+'/pagetoopen'+'?aid='+a.id); 
         openorderpadembed.setRedirect(false); 
         return openorderpadembed;      
    }

From my VisualForce page I need to call the method openPage().

Please help me out

2 Answers 2

4

You can use JavaScript Remoting for Apex Controllers in the following manner:

<apex:page standardController="Account" extensions="OrderPadController">
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
    <script>
      window.$j = jQuery.noConflict();
      $j( document ).ready(function() {
        OrderPadController.openPage(function(result, event){

            console.log(result);
            window.open(result,"_self");

        });
      });

    </script>
</apex:page>
public class OrderPadController {
    //

    @remoteAction
    public  PageReference openPage() {
        PageReference newpage = NEW Pagereference('/apex' + '/pagetoopen' + '?aid=' + a.id);
        openorderpadembed.setRedirect(false);
        return openorderpadembed;
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

Hi thanks for the reply. In the script OrderPadController.openPage(function(result, event){ }); what should be the result and event in the function ? I dont have to send anything as a parameter. I am new to this Please explain.
Sorry, I've missed that the return values is page reference, I'll go through your case in a few hours, because now I'm on a way to home
@Ramesh please see my answer again, I've edited it. But please describe your case, I guess that there are options for covering your requirements in more convenient and straightforward manner.
@Pavel Could you please look at my question? I did what you said and it didn't work. It is about the same issue: stackoverflow.com/questions/28410030/…
1
<apex:page standardController="Account" extensions="OrderPadController" 
    action="{!openPage}">
</apex:page>

the action parameter will call the method you want as soon as the page loads. It is a dangerous attribute, one that will be pointed out to you if you'll go through Salesforce's security review process. The called action can manipulate database values for example (whereas the OrderPadController constructor can't) and the philosophy is that act of visiting a page shouldn't have any side effect.

In your specific scenario you can do it even without any Apex code at all:

<apex:page standardController="Account" 
    action="/apex/Gantt?aid={!$CurrentPage.parameters.id}">
    Look Mom, no hands!
</apex:page>

As Pavel mentions - please write more about your requirement. I suspect that you don't even need this visualforce page acting as redirect, that a simple custom link or button (type = url instead of Visualforce or Javascript) could do the trick...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.