3

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}"/>
            </apex:pageBlockTable>
            </apex:pageBlockSectionItem>
        </apex:pageBlockSection>
    </apex:pageBlock>

</apex:page>

Email Apex:

Messaging.reserveSingleEmailCapacity(1);
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
String[] toAddresses = new String[] {'[email protected]'};
mail.setToAddresses(toAddresses);
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.setBccSender(false);
mail.setUseSignature(false);
mail.setPlainTextBody('Your Opportunity: ' + Opportunity.Id +' request.');
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
6
  • What exactly do you mean by 'activates' an Apex class? Do you just mean using an extension that you can pass data to?? Commented 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. Commented 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. Commented 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. Commented 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. Commented Aug 27, 2014 at 14:11

2 Answers 2

4

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

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

Extension:

public with sharing MyExtension
{
  private Opportunity opp;

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

  public PageReference doSomething()
  {
    //do something here
  }
}

http://www.salesforce.com/us/developer/docs/pages/Content/pages_controller_extension.htm

2
  • How will this interface with my class? Commented 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
    Commented Aug 27, 2014 at 14:28
1

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

VF

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

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

Extension

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) {
          //YOUR CUSTOM LOGIC HERE
     }
}

You must log in to answer this question.

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