function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Yamini BathulaYamini Bathula 

Call Controller method in visualforcecomponent

Hi guys,

Can anyone please provide me an example on how to call a apex controller method in a visualforce custom component using a command button?
Best Answer chosen by Yamini Bathula
atul patil 7atul patil 7
Hello Yamini,
      You can call controller method on click of command button by using action attribute 
here is the code:
VF Component 
 
<apex:component controller="DemoController">
    <apex:form>
    	<apex:input label="Employee Name" value="{!emp.Name__c}" />
        <apex:input label="Employee company" value="{!emp.company__c}" />
        <apex:commandButton action="{!saveDetails}"/>
    </apex:form>
</apex:component>

Controller code:-
public class DemoController {
    public Employee__c emp{get;set;}
    public void saveDetails(){
        insert emp;
    }
}

Thanks,
Atul patil
salesforce developer
www.zen4orce.com​

All Answers

atul patil 7atul patil 7
Hello Yamini,
      You can call controller method on click of command button by using action attribute 
here is the code:
VF Component 
 
<apex:component controller="DemoController">
    <apex:form>
    	<apex:input label="Employee Name" value="{!emp.Name__c}" />
        <apex:input label="Employee company" value="{!emp.company__c}" />
        <apex:commandButton action="{!saveDetails}"/>
    </apex:form>
</apex:component>

Controller code:-
public class DemoController {
    public Employee__c emp{get;set;}
    public void saveDetails(){
        insert emp;
    }
}

Thanks,
Atul patil
salesforce developer
www.zen4orce.com​
This was selected as the best answer
Mustafa JhabuawalaMustafa Jhabuawala
Hi,

You can call apex controller method from command button by following ways - 

1. You can use action attribute of command button to call apex controller method

For example - 
<!-- Page: -->
<apex:page controller="sampleController">
<apex:commandButton action="{!save}" value="Save" />
</apex:page>
 
//Apex Class
public with sharing class sampleController{
    public void save(){
    //Business logic
    }
}
2. You can use action function too. Visit this link for more details and example https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_compref_actionFunction.htm?search_text=apex:actionfunction

Hope you find this helpful.

Note - Kindly mark this answer as best answer if this helped you, so that other's can also get benefit from it.

Thanks,
Mustafa Jhabuawala
Technical Lead at Zen4orce (http://www.zen4orce.com)

 
Yamini BathulaYamini Bathula
Thank you so much guys. 
Amol Salve 13Amol Salve 13
Hello Yamini,
     
    You can controller method in diffrenet way
1 Using CommandButton with action attribute
    
    in action attribute specify your controller method like
    <apex:commanbutton value="XYZ" action="{!yourControllerMethod}"/>
2 Using action functon 
    
    <apex:commandButton  value="XYZ" onclick="callMethod()"/>
    <apex:actionFunction id="setState" action="{!yourControllerMethod}; return false;" name="callMethod" />
    using action fuction you can pass parameter to controller
3 Using action support
    
    </apex:outputpanel>
        <apex:outputText value="Enter Value"/>
        <apex:actionSupport event="onclick" action="{!yourControllerMethod}"/>
    </apex:outputpanel>
    
Thank you,
Amol Salve
Salesforce Developer