I have a VisualForce page which displays some data from a standard controller, I would like to know how I can add a button which activates an apex class? I want to also pass data thats visible in the VF page to the apex class.

VF Page

<apex:page standardController="Opportunity" showHeader="false" sidebar="false">

    <apex:pageBlock >
        <apex:pageBlockSection title="Opportunity" columns="1">
            <apex:outputField value="{!Opportunity.Name}"/>
            <apex:pageBlockSectionItem >
            <apex:outputText value="Contacts"/>
            <apex:pageBlockTable title="Contacts" 
                                 value="{!Opportunity.Designer__r}" var="contact">
                <apex:column value="{!contact.Name}"/>
                <apex:column value="{!contact.Phone}"/>


Email Apex:

Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
String[] toAddresses = new String[] {'[email protected]'};
mail.setReplyTo('[email protected]');
mail.setSenderDisplayName('John Doe');
mail.setSubject('Opportunity Request : ' + Opportunity.Id); // at the moment Opportunity ID is not linked to anything
mail.setPlainTextBody('Your Opportunity: ' + Opportunity.Id +' request.');
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
  • What exactly do you mean by 'activates' an Apex class? Do you just mean using an extension that you can pass data to?? Aug 27, 2014 at 14:02
  • Sorry maybe be incorrect use of wording. I have an apex class which sends some emails, atm I dont know how to pass the data from the VF page to that class. When the button is pressed, that apex class should be called. Aug 27, 2014 at 14:03
  • 1
    Can you post up the code that you've already tried to achieve this with? It doesn't sound too complex but it's best if you post your code before we start making suggestions. Aug 27, 2014 at 14:06
  • look at the answer from "dphil". your extension class can make use of the standard sobject record to retrieve values that you need to use while sending the emails from your extension. Aug 27, 2014 at 14:11
  • Have you already looked at the apex or visualforce documentation ? It's not clear why this is an issue or what part is unclear for you. Aug 27, 2014 at 14:11

2 Answers 2


It sounds like a controller extension is actually what you need:

<apex:page standardController='Opportunity' extensions='MyExtension'>
    <apex:inputField value="{!Opportunity.Name}" />
     <apex:commandButton value="this button" value="{!doSomething}" />


public with sharing MyExtension
  private Opportunity opp;

  public MyExtension(ApexPages.StandardController cont)
    opp =(Opportunity) cont.getRecord();

  public PageReference doSomething()
    //do something here


  • How will this interface with my class? Aug 27, 2014 at 14:21
  • @Imran I created my answer before you added in your code. You should be able to look at my code and see how it could be applied to your code though. Basically just a less complex version of yours ATM.
    – dphil
    Aug 27, 2014 at 14:28

Based on your comment I believe you need to add a controller extension to your page with a method that your custom button calls. That method can then call a static method from your other Apex Class. Or you could add the logic directly to your extension method, but if this is code you may use in other places, I would recommend keeping it its own class so you can reuse the code.

Something like this


<apex:page standardController="SomeStandardObject" extensions="myCustomExtension">

    <apex:pageBlockButtons location="bottom">
        <apex:commandButton Value="Press Me" action="{!myExtensionMethod}"/>


public class myCustomExtension {

    public myCustomExtension {


    public void myExtensionMethod {
         MyHelperClass.Method1(param1, param2, param_x);

Your Custom Apex Class

public class MyHelperClass {

     public static void Method1(Some_Data_Type param1, Some_Data_Type param2, Some_Data_Type param_c) {

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .