@RemoteAction in Visual force page

@RemoteAction in Visual force page

JavaScript remoting in Visualforce provides support for some methods in Apex controllers to be called via JavaScript.

JavaScript remoting has three parts:

  • The remote method invocation you add to the Visualforce page, written in JavaScript.
  • The remote method definition in your Apex controller class. This method definition is written in Apex, but there are few differences from normal action methods.
  • The response handler callback function you add to or include in
    your Visualforce page, written in JavaScript.

 To use JavaScript remoting in a Visualforce page, add the request as a JavaScript invocation with the following
form:

[namespace.]controller.method(
    [parameters...,]
    callbackFunction,
    [configuration]
);
  • namespace is the namespace
    of the controller class. This is required if your organization has
    a namespace defined, or if the class comes from an installed package.
  • controller is the name of
    your Apex controller.
  • method is the name of the Apex method you’re calling.
  • parameters is the comma-separated
    list of parameters that your method takes.
  • callbackFunction is the name
    of the JavaScript function that will handle the response from the
    controller. You can also declare an anonymous function inline. callbackFunction receives the status
    of the method call and the result as parameters.
  • configuration configures
    the handling of the remote call and response. Use this to specify
    whether or not to escape the Apex method’s response. The default value is {escape: true}.

Visualforce Page:

<apex:page controller=”sample”>
    <script type=”text/javascript”>
    function getAccountJS()
    {
        var accountNameJS = document.getElementById(‘accName’).value;       
        sample.getAccount( accountNameJS,
        function(result, event)
        {
            if (event.status)
            {
                // demonstrates how to get ID for HTML and Visualforce tags
                document.getElementById(“{!$Component.theBlock.thePageBlockSection.theFirstItem.accId}”).innerHTML = result.Id;
                document.getElementById(“{!$Component.theBlock.thePageBlockSection.theSecondItem.accNam}”).innerHTML = result.Name;
            }
            else if (event.type === ‘exception’)
            {
                document.getElementById(“errors-js”).innerHTML = event.message;
            } else
            {
                document.getElementById(“errors-js”).innerHTML = event.message;
            }
        }, {escape:true});
    }
    </script>
    Account Name :<input id=”accName” type=”text” />
    <button onclick=”getAccountJS()”>Get Account</button>
    <div id=”errors-js”> </div>
    <apex:pageBlock id=”theBlock”>
        <apex:pageBlockSection id=”thePageBlockSection” columns=”2″>
            <apex:pageBlockSectionItem id=”theFirstItem”>
                <apex:outputText id=”accId”/>
            </apex:pageBlockSectionItem>
            <apex:pageBlockSectionItem id=”theSecondItem” >
                <apex:outputText id=”accNam” />
            </apex:pageBlockSectionItem>
        </apex:pageBlockSection>
    </apex:pageBlock>
</apex:page>



Apex Controller:


global class sample
{
    public String accountName { get; set; }
    public static Account account { get; set; }
    public sample() { }
   
    @RemoteAction
    global static Account getAccount(String accountName)
    {
        account = [select id, name, phone, type, numberofemployees from Account where name = :accountName ];
        return account;
    }
}

Ouptut:

Cheers!!!

Leave a Reply