Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Salesforce by (11.9k points)

How can I call a controller method from the 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

1 Answer

0 votes
by (32.1k points)
edited by

he 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" 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;

    }

}

Want to become a Salesforce Expert? join Salesforce certification now! 

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...