Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

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[] {'to@email.com'};
mail.setToAddresses(toAddresses);
mail.setReplyTo('emailservice@salesforce.com');
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 });
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?? –  sfdc_ninja Aug 27 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 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. –  DavinC Aug 27 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 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 at 14:11

2 Answers 2

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

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

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
     }
}
share|improve this answer

Your Answer

 
discard

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.