Activity › Forums › Salesforce® Discussions › How to add javascript remoting to a Salesforce visualforce page?
Tagged: Callback, Controller Class, Javascript Functions, Javascript Remoting, Salesforce Apex Controller, Salesforce Configuration, Salesforce Visualforce, Salesforce Visualforce Page
-
How to add javascript remoting to a Salesforce visualforce page?
Posted by Avnish Yadav on August 14, 2018 at 7:26 AMHow to add javascript remoting to a Salesforce visualforce page?
shariq replied 7 years, 8 months ago 4 Members · 3 Replies -
3 Replies
-
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.
- [adinserter block='9']
-
Hello Avnish,
To use javascript remoting in a Visualforce page use Romote objecst or @RemoteAction in Visualforce page to insert record in Salesforce.
Thanks.
-
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.