Activity Forums Salesforce® Discussions How to add javascript remoting to a Salesforce visualforce page?

  • Prachi

    Member
    August 14, 2018 at 8:30 am

    Hello avnish,

    To use javascript remoting in a vf page , add the request as a java script invocation with the following from.

    [namespace.] controller.method (
    [parameters….,]
    Call back Function,
    [configuration]
    );
    Name space is the namespace of the controller class
    Controllers is the name of your apex controller.
    Method is the name of your apex controller method you are calling.
    Parameters is the comma-separated list of parameters that your method takes.
    Callback function is the name of the javascript function that will handle the response from the controller.
    Configuration configures the handling of remote call and response.

    Thanks.

  • Parul

    Member
    September 7, 2018 at 6:05 pm

    Hello Avnish,

    To use javascript remoting in a Visualforce page use Romote objecst or @RemoteAction  in Visualforce page to insert record in Salesforce.

     

     

    Thanks.

  • shariq

    Member
    September 17, 2018 at 8:41 pm

    Hi,

    Try this -

    Apex code - 

    global with sharing class AccountRemoter {

    public String accountName { get; set; }
    public static Account account { get; set; }
    public AccountRemoter() { } // empty constructor

    @RemoteAction
    global static Account getAccount(String accountName) {
    account = [SELECT Id, Name, Phone, Type, NumberOfEmployees
    FROM Account WHERE Name = :accountName];
    return account;
    }
    }

    Visualforce page - 

    <apex:page controller="AccountRemoter">
    <script type="text/javascript">
    function getRemoteAccount() {
    var accountName = document.getElementById('acctSearch').value;

    Visualforce.remoting.Manager.invokeAction(
    '{!$RemoteAction.AccountRemoter.getAccount}',
    accountName,
    function(result, event){
    if (event.status) {
    // Get DOM IDs for HTML and Visualforce elements like this
    document.getElementById('remoteAcctId').innerHTML = result.Id
    document.getElementById(
    "{!$Component.block.blockSection.secondItem.acctNumEmployees}"
    ).innerHTML = result.NumberOfEmployees;
    } else if (event.type === 'exception') {
    document.getElementById("responseErrors").innerHTML =
    event.message + "<br/>\n<pre>" + event.where + "</pre>";
    } else {
    document.getElementById("responseErrors").innerHTML = event.message;
    }
    },
    {escape: true}
    );
    }
    </script>

    <input id="acctSearch" type="text"/>
    <button onclick="getRemoteAccount()">Get Account</button>
    <div id="responseErrors"></div>

    <apex:pageBlock id="block">
    <apex:pageBlockSection id="blockSection" columns="2">
    <apex:pageBlockSectionItem id="firstItem">
    <span id="remoteAcctId"/>
    </apex:pageBlockSectionItem>
    <apex:pageBlockSectionItem id="secondItem">
    <apex:outputText id="acctNumEmployees"/>
    </apex:pageBlockSectionItem>
    </apex:pageBlockSection>
    </apex:pageBlock>
    </apex:page>

    JavaScript remoting is a tool that front-end developers can use to make an AJAX request from a Visualforce page directly to an Apex controller. JavaScript remoting allows you to run asynchronous actions by decoupling the page from the controller and to perform tasks on the page without having to reload the entire page.

    Hope this helps.

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos