[SalesForce] How to create a button on related list that gets parent Account ID

I am trying to create a custom "Log a phone call" button, the button will be displayed on the related list of Activities on an Account Record.

The button will use a Visual Force page as its source so that it can trigger a visual work flow.

The issue is I need to pass the AccountId of the parent Account that the related list is on as a parameter to the flow.

But when I select Visualforce page as the source it will only let me select the Visualforce page if it uses the standard Task controller.

How can I get the Account Id of the record I am on when I click the custom button on the related Activities list?

Best Answer

Your VF page could use a StandardController for Activity and then your VF page's action attribute could execute a method which takes the id from the record and performs a redirect to the flow URL providing that ID as a parameter.

The VF page needs no markup in it other than the <apex:page standardController="Task" extension="yourClass" action="yourMethod" > since no content will be rendered by this page to the end user.

It's just acting as a router to create the URL to the flow and redirect the user.

https://developer.salesforce.com/docs/atlas.en-us.salesforce_vpm_guide.meta/salesforce_vpm_guide/vpm_url_setvar.htm


The VF page

<apex:page StandardController="Task" extensions="activityRouter" action="{!routeToFlow}" >
  <!-- Begin Default Content REMOVE THIS -->
  <h1>Congratulations</h1>
  This is your new Page
  <!-- End Default Content REMOVE THIS -->
</apex:page>

The controller

public with sharing class activityRouter {

    public activityRouter(ApexPages.StandardController controller) {

    }

    public PageReference routeToFlow() {

        // get the parameters from this page
        Map<String, String> pageParams = ApexPages.CurrentPage().getParameters();
        system.debug(pageParams);

        // create a new URL to the flow and append our params to it    
        return new PageReference('/someflowUrl?title=' + pageParams.get('title') + '&what_id=' + pageParams.get('what_id'));
    }

}

Activity "Log a Call" button override

enter image description here


Resulting URL after clicking "Log a Call" button on an Account's Activity History related list https://domain.my.salesforce.com/someflowUrl?title=Call&what_id=001G00000229ESC