Salesforce page : Javascript remoting with visualforce pages
Visulaforce remoting is the one of the way of calling Apex Method from javascript:
Call Apex Method from java script? < see actionFuntion also> which ever suites to you :)
======> Remote method
global with sharing class AtestClass {
@RemoteAction
global static Account getAccountCity(String actName){
try{
return [select BillingStreet,BillingCity from Account where name = :actName][0];
}
catch(Exception e){
return null;
}
}
}
======> Visualforce Page ( this simple, just compile and execute code
<apex:page controller="AtestClass">
<script type="text/javascript">
function getRemoteAccountAddress() {
var actName = document.getElementById('acctSearch').value;
Visualforce.remoting.Manager.invokeAction(
'{!$RemoteAction.AtestClass.getAccountCity}',actName,
function(result, event){
if (event.status) {
if(result != null){
alert(result.BillingStreet + ' - ' + result.BillingCity);
document.getElementById('actAddressDisplay').innerHTML
= result.BillingStreet + ' - ' + result.BillingCity;
}
else{
document.getElementById('actAddressDisplay').innerHTML = 'No Data Found';
}
} else if (event.type === 'exception') {
document.getElementById("responseErrors").innerHTML = event.message;
} else {
document.getElementById("responseErrors").innerHTML = event.message;
}
},
{escape: true}
);
}
</script>
<input type="text" id="acctSearch" size="100"/>
<a href="javascript:getRemoteAccountAddress();">Find Address</a>
<br/><br/>
<div id="actAddressDisplay"></div>
</apex:page>
Call Apex Method from java script? < see actionFuntion also> which ever suites to you :)
======> Remote method
global with sharing class AtestClass {
@RemoteAction
global static Account getAccountCity(String actName){
try{
return [select BillingStreet,BillingCity from Account where name = :actName][0];
}
catch(Exception e){
return null;
}
}
}
======> Visualforce Page ( this simple, just compile and execute code
<apex:page controller="AtestClass">
<script type="text/javascript">
function getRemoteAccountAddress() {
var actName = document.getElementById('acctSearch').value;
Visualforce.remoting.Manager.invokeAction(
'{!$RemoteAction.AtestClass.getAccountCity}',actName,
function(result, event){
if (event.status) {
if(result != null){
alert(result.BillingStreet + ' - ' + result.BillingCity);
document.getElementById('actAddressDisplay').innerHTML
= result.BillingStreet + ' - ' + result.BillingCity;
}
else{
document.getElementById('actAddressDisplay').innerHTML = 'No Data Found';
}
} else if (event.type === 'exception') {
document.getElementById("responseErrors").innerHTML = event.message;
} else {
document.getElementById("responseErrors").innerHTML = event.message;
}
},
{escape: true}
);
}
</script>
<input type="text" id="acctSearch" size="100"/>
<a href="javascript:getRemoteAccountAddress();">Find Address</a>
<br/><br/>
<div id="actAddressDisplay"></div>
</apex:page>
Comments
Post a Comment