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
pintoo rajputpintoo rajput 

can we call a class through command button????

Best Answer chosen by pintoo rajput
Abi DuthuAbi Duthu
It is possible to do this, you will need to have a function in your controller which is called by your button. This function can then call the Apex Class function. 

You can call a class from a Visual force page button like this: 

Example :

Visual force Page code :

<apex:page controller="myController">
<apex:commandbutton action="{!saveItem}" value="Save"></apex:commandbutton>
<apex:commandbutton action="{!cancelAction}" value="Cancel"></apex:commandbutton>
<apex:commandButton action="{!myAction}" value="myaction"></apex:commandbutton>
</apex:page>


Apex Class code:

public class myController{

     public pageReference saveItem(){
         // Some code goes here
         }
 
    public pageReference cancelAction(){
         // Some code goes here
         }

    public pageReference myaction(){
        // Some code goes here
        }



The commandbutton attribute, action="{!saveItem}" it refers the save method in the controller and the attribute value="Save" is the text displayed on the commandButton as button label.

All Answers

kiranmutturukiranmutturu
You can call a method in a class from command button using action attribute

some thig like this <apex:commandButton action="{!doThis}" value="Save" id="theButton"/>  doThis is the method

In order to call a method in a class that should be used as a value in controller or extensions attribute in apex:page component
pintoo rajputpintoo rajput
Thanks but dear sir i want to know how can we call a class from visualforce page(apex tag)
Abi DuthuAbi Duthu
It is possible to do this, you will need to have a function in your controller which is called by your button. This function can then call the Apex Class function. 

You can call a class from a Visual force page button like this: 

Example :

Visual force Page code :

<apex:page controller="myController">
<apex:commandbutton action="{!saveItem}" value="Save"></apex:commandbutton>
<apex:commandbutton action="{!cancelAction}" value="Cancel"></apex:commandbutton>
<apex:commandButton action="{!myAction}" value="myaction"></apex:commandbutton>
</apex:page>


Apex Class code:

public class myController{

     public pageReference saveItem(){
         // Some code goes here
         }
 
    public pageReference cancelAction(){
         // Some code goes here
         }

    public pageReference myaction(){
        // Some code goes here
        }



The commandbutton attribute, action="{!saveItem}" it refers the save method in the controller and the attribute value="Save" is the text displayed on the commandButton as button label.
This was selected as the best answer
pintoo rajputpintoo rajput
can I call a Visual Force Page through command button???/