This post is beneficial to you if you faced some user interface level requirement. Here we explained about the actionFunction tag. We move on the first step what it is.
ActionFunction:–
This is one of the AJAX support components in visual force page. This should be contained on parent tag like that apex: form tag. The main use of the actionfunction to calling a method from visual force to apex class through javascript.
Without using actionfunction, you can call a method to apex class it is possible by using the button with action attribute. Why we used this? Some time we need javascript support for User Interface, without button click. Here we used one example contains a checkbox for customer confirmation before insertion.
Syntax :-
<apex:actionFunction name=”Javascript-Returning-Function-Name” action=”{!Apex-Method-Name}”/>
For examples, Customer try to booking in Hotel, it will check the customer confirmation then it will inserted into the database by using checkbox.
Try the below code :-
JS method name – jsconfirm
JS return method name – jsresult
apex method name – booking
Visulforce code
<apex:page controller=”ActionFunctionController” sidebar=”false” id=”pg”>
<apex:form id=”frm”>
<apex:actionFunction name=”jsresult” action=”{!booking}” reRender=”con”/>
<table>
<tr>
<td>Booking Customer :</td>
<td><apex:inputText id=”tt” value=”{!txt}”/></td>
</tr>
<tr>
<td>Click this box</td>
<td><apex:inputCheckbox onclick=”jsconfirm()” id=”chkbox”/></td>
</tr>
</table>
<br/>
<apex:outputLabel value=”{!confirmationtext}” id=”con”></apex:outputLabel>
</apex:form>
<script type=”text/javascript”>
function jsconfirm(){
var c=document.getElementById(‘pg:frm:tt’).value;
if(c!=”){
var a=confirm(‘Are you sure to booking ? …’);
if(a==true){
jsresult();
}
}
else{
alert(‘Please fill the customer name’);
}
}
</script>
</apex:page>
Apex Class :-
public class ActionFunctionController{
public String confirmationtext{set;get;}
public String txt{set;get;}
public void ActionFunctionController(){}
public void booking(){
Hotel__c h=new Hotel__c();
system.debug(txt);
h.Booking_Customer__c=txt;
insert h;
confirmationtext=’You are booking success full’;
}
}
Result :-
This the starting page,
Click checkbox the error showing if you did not enter customer name,
Click checkbox the confirmation showing after you entered information,
Click ok then it will pop up success message and record added in your object.
We are the ISV Partners and Please reach us for custom development => www.merfantz.com
———————– We hope this it will help to you ———————