Visualforce Action Function Example.
Hi,
In this post i am giving an example of simple usage of <apex:actionFunction> tag in visualforce page to call a apex method from Java script.
Controller Class:
public class ActionFunctionCLS {
public Account
actObj{get;set;}
PageReference
prf= null;
public
ActionFunctionCLS(){
actObj = new
Account();
}
public
pagereference Saverec(){
if(actobj.Name
!=''){
insert actobj;
}
else{
ApexPages.Message myMsg = new
ApexPages.Message(ApexPages.Severity.ERROR,'Error: Please Enter Name.');
ApexPages.addMessage(myMsg);
}
if(actobj.id
!=null){
// Send the
user to the detail page for the new account.
prf = new
PageReference('/'+actobj.id);
prf.setRedirect(true);
}
return prf;
}
}
Visualforce Page:
<apex:page
controller="ActionFunctionCLS" id="pg" >
<script>
function recSave(){
var accountType =
document.getElementById('pg:fm:pb:pbs:actType').value;
alert('accountType -->'+accountType);
if(accountType != 'Prospect'){
alert('You Should Select Prospect to Save
the Record');
return false;
}
else{
saveAccount(); //this is the function name
which calls our action function from java Script.
return true;
}
}
</script>
<apex:form id="fm">
<apex:actionfunction
name="saveAccount" action="{!Saverec}" />
<apex:pageBlock id="pb">
<apex:pagemessages
></apex:pagemessages>
<apex:pageblockButtons >
<apex:commandButton
value="Save" onclick="recSave();return false;"/>
</apex:pageblockButtons>
<apex:pageblockSection
id="pbs">
<apex:inputField
value="{!actobj.Name}" id="actName"/>
<apex:inputField
value="{!actobj.type}" id="actType"/>
</apex:pageblockSection>
</apex:pageBlock>
</apex:form>
</apex:page>
Once you select Account Type as "Prospect" Javascript method will call the <apex:actionfunction name="saveAccount" action="{!Saverec}" /> and controller method is going to execute.
That's it..
7 comments:
Salesforce Admin Corporate Training | Online Training by real time Experts with IT Support IND: 910-0934572. Online Training classes gives you complete knowledge by 21st Century. send ur enquiry to contact@21cssindia.com. or Call +917386622889 http://www.21cssindia.com/salesforce-admin-training Salesforce Admin Training| USA:201-210-8616|Online/Corporate training|21cssindia
hey, can you help me regarding the issues, i found during executing this code:-
Its not inserting any record , it just reload same vf page.
thanx for such useful post..
why return false in onclick event, can you please explain?
Can you please let me know the uses of actionfunction
Very Neat Explaination. Thank you !
You need personal informations from companies,family and friends that will better your life and you need easy access without them noticing just contact james or you’re financially unstable or you have a bad credit score, he will solve that without stress,he and his team can clear criminal records without leaving a trace and can also anonymously credit your empty credit cards with funds you need,all these are not done free obviously but I like working with James and his team cause they keep you updated on every step taken in order to achieve the goal and they also deliver on time,I tested and confirmed this I’m still happy on how my life is improving after my encounter with him ,you can send a mail to premiumhackservices@gmail.com
Post a Comment