Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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[] {''};
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 });
share|improve this question
What exactly do you mean by 'activates' an Apex class? Do you just mean using an extension that you can pass data to?? – Chris Duncombe Aug 27 '14 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. – Imran Aug 27 '14 at 14:03
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. – Davin Casey Aug 27 '14 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. – Chetan Gupta Aug 27 '14 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. – Samuel De Rycke Aug 27 '14 at 14:11
up vote 3 down vote accepted

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

share|improve this answer
How will this interface with my class? – Imran Aug 27 '14 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 '14 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) {
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.