function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion

Get JSRemoting to execute on page load

Here is the example I got, but it triggers off of ONCLICK. I want to be able to use the JSRemoting to run an APEX class which would return data and ultimately the data would be shown on the first rendering of the visualforce page. I've tried a dozen different things, but no luck. What do I need to do to this code to get the desired result ...


<apex:page controller="MyJSRemoting">
<script type="text/javascript">
function getAccountJS() {
var accountNameJS = document.getElementById('accountname').value;

MyJSRemoting.getAccount( accountNameJS, function(result, event)
    if (event.status)
        .innerHTML = result.Id;
        .innerHTML = result.Name;
}, {escape:true});

<input id="accountname" type="text"/>

    <button onclick="getAccountJS();">Get Account</button>

      <apex:pageblock id="theBlock">
        <apex:pageblocksection columns="2" id="pbs">

               <apex:pageblocksectionitem id="pbsi1">
               <apex:outputtext id="name">

        <apex:pageblocksectionitem id="pbsi2">
                  <apex:outputtext id="accId">



TIA, Mike

Best Answer chosen by Admin (Salesforce Developers) 

You might try to use the action="" keyword on the Page header.  That will call a method in the controller that is called before the constructor is called. 

All Answers


You might try to use the action="" keyword on the Page header.  That will call a method in the controller that is called before the constructor is called. 

This was selected as the best answer

Okay, I'll try that. Actually, I'm not wedded to the idea of using JavaScript, but as far as I can tell, it's the only way to invoke an APEX class from within a visualforce page. Probably not true, but I'm very new to this. 


The action="{!method}" attribute does just that: it calls Apex Code on the server after the page's constructor, but before rendering.


You can also use the constructor of your class to call other code before the page loads.


public class MyController {
    public MyController() {
        // Do stuff here

Finally, when using extensions, you can also call code in their constructors:


<apex:page standardcontroller="account" extensions="MyExtension" ...
public class MyExtension {
    public MyExtension(ApexPages.StandardController controller) {
        // Do stuff here

The "controller" in this case gives you access to the current record you're working on.


Okay, I've ditched the JSRemoting and am now trying to use the action like this.


<apex:page standardController="Opportunity"
                                     showHeader="false" renderAs="{!$CurrentPage.parameters.renderAs}"


here is the class



public class JSONParserUtil {
    public static string parseJSONResponse() {        
        Http httpProtocol = new Http();
        // Create HTTP request to send. 
        HttpRequest request = new HttpRequest();
        // Set the endpoint URL. 
        String endpoint = '';
        // Set the HTTP verb to GET. 
        // Send the HTTP request and get the response. 
        // The response is in JSON format. 
        HttpResponse response = httpProtocol.send(request);
        // Parse JSON response to get all the totalPrice field values. 
        JSONParser parser = JSON.createParser(response.getBody());
        Double grandTotal = 0.0;
        while (parser.nextToken() != null) {
            if ((parser.getCurrentToken() == JSONToken.FIELD_NAME) && 
                (parser.getText() == 'totalPrice')) {
                // Get the value. 
                // Compute the grand total price for all invoices. 
                grandTotal += parser.getDoubleValue();
        system.debug('Grand total=' + grandTotal);
        return response.getBody();
    public JSONParserUtil (ApexPages.StandardController controller) {
        // Do stuff here

The question is: how do I return response.getBody() to be displayed on my visualforce page?




Nevermind, I think I found some examples.