Hi Guys,'
Last week I was playing with Java script remoting.
What is java script remoting?
Last week I was playing with Java script remoting.
What is java script remoting?
In very simple words , use java script remoting in Visualforce to call methods in Apex controllers from javaScript.
So , lemme straight away take you to scenario on which I was working last week.
Say, You want to display a picklist value that you have just selected from a custom field of account object. it is one of the scenario where java script remoting comes handy and useful.
Here , is the code for you:-
Apex Controller:-
global with sharing class CopyFieldUsingRemoteActions{
public CopyFieldUsingRemoteActions() {
}
Account acct = [SELECT Id, AnkushTest__SLA__c from Account WHERE Id = :ApexPages.currentPage().getParameters().get('id')];
public static Account account { get; set; }
@RemoteAction
global static account getAccountSLA(string Fname) {
account= [Select id , name,TestMap__c,AnkushTest__samplePrefix__c, AnkushTest__SLA__c from account where AnkushTest__SLA__c =: Fname limit 1];
return account;
}
}
lemme first explain What I have done. So I have tried to querry Account id which is current page ID using :ApexPages.currentPage().getParameters().get('id')] which acytually returns the ID of
It actually returns the Id of the current page . You have to tell controller that a method is going top ber remoting action so you can use in Visualforce. Also , I have passed a parameter in of string type .
Now , lets go to visualforce section :-
<apex:page Controller="CopyFieldUsingRemoteActions">
<script>
function getRemoteAccount(){
alert(document.getElementById("j_id0:formid:pb:disc:myText").value);
var test1=document.getElementById("j_id0:formid:pb:disc:myText").value
Visualforce.remoting.Manager.invokeAction(
'{!$RemoteAction.CopyFieldUsingRemoteActions.getAccountSLA}',
test1,
function(result, event){
if (event.status) {
alert(document.getElementById("j_id0:formid:pb:acctSearch"));
// Get DOM IDs for HTML and Visualforce elements like this
document.getElementById("j_id0:formid:pb:acctSearch").innerHTML = result.AnkushTest__SLA__c ;
} else if (event.type === 'exception') {
document.getElementById("j_id0:formid:pb:acctSearch").innerHTML =
event.message + "<br/>\n<pre>" + event.where + "</pre>";
alert(event.message);
} else {
document.getElementById("j_id0:formid:pb:acctSearch").innerHTML = event.message;
alert('FAil');
}
},
{escape: true}
);
}
</script>
<apex:form id="formid">
<apex:pageBlock mode="edit" id="pb">
<apex:pageBlockButtons >
<button onclick="getRemoteAccount()">show </button>
</apex:pageBlockButtons>
<apex:outputText id="acctSearch" label="Output"/>
<apex:pageBlockSection title="My Content Section" columns="2" id="disc" >
<apex:inputField value="{!account.AnkushTest__SLA__c }" id="myText" onblur="alert('hii');getRemoteAccount();"/>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>
All the java script functionality should be defined under script tag. Now , I have defined a function name getRemoteAccount(). Interesting thing to know that in alert and in that i have used id from inspect element where i want it to be get print.
Happy Coding!!
Cheers
ankushsalesforce@gmail.com
No comments:
Post a Comment