I have a hyperlink in an email which will open an opportunity using the standard opportunity page. Before the opportunity page is open I want to carry out some business logic. I only want this business logic to be executed when the hyperlink is clicked in the email. If someone hits the opportunity any other way without hitting URL in the email, I do not want this particular business logic to execute.

My intended approach to do this is send the URL in the email to another page, carry out the business logic in a back end controller and then return a redirect to the opportunity page.

I want to use this approach as I was don't want a custom controller for the opportunity. I want to leave that all alone.

Is it possible to do redirects in force.com?



Create a new page, lets say FirstPage and add it to your URL. In that page use the action attribute of the apex:page, do your logic and then redirect the user to the opportunity page:

The page:

<apex:page controller="RedirectController" action="{!redirectToPage}">
    PLease wait...

Extention controller:

public with sharing class RedirectController {

    private Id id;

    public RedirectController() {
        // Getting the id of the current record
        this.id = ApexPages.currentPage().getParameters().get('opid');

    // Here do your logic and then redirect user to the opportunity page
    public PageReference redirectToPage() {
        PageReference page = new PageReference('/' + this.id);
        return page;

In your Email link use the new page and transfer opportunity-ID as URL parameter:

<apex:outputLink value="/apex/FirstPage&opid={!opportunityId}">Click me</apex:outputLink>
  • Note that this solution fails on pages shown in Salesforce Communities (due to the absolute URL used). See this question for details, and an improved RedirectController that works with Communities. – tomlogic Aug 31 '13 at 1:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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