Sunday, November 1, 2015

Salesforce: How to call Flow from Visualforce page

In this blog, we are going to explain how to use Visualforce page to call Autolaunched Flow.

Use case: business would like to record Contacts deleted by user from Salesforce page layout to a custom object.

With apex trigger, this should be no issue, but is there any other way?

Let's using Autolaunched Flow, Autolaunched Flow is famous to be used by Process Builder, but Process Builder cannot catch deletion process, it only support record creation and update.

Use Autolaunched Flow called from Visualforce page, and the Visualforce page to overwrite the Delete button.

1. Prepare Custom Object

2. Create Flow
  • Flow Unique Name = Delete_Contact_Flow
  • Type = Autolaunched Flow

A. Create 2 variables and 1 constant

Variable varContactId use to receive Contact Id from screen, type = Input Only

Variable varContactName use to query Contact Name using varContactId, type = Private

Constant consObject use to store the process for Contact object

B. Create 3 record processes: lookup, delete, create

Overall Flow Processes

Query Contact with variable varContactId to get Contact Name and store it to varContactName
** you may to skip this process if Contact Name passed in as parameter, but for this blog, we would like to show record lookup process.

Delete Contact record with variable varContactId

Create a custom Object record with necessary fields to record the Contact deleted

Save the Flow and remember to Activate it.

3. Create Visualforce Page
This would be a simple page to pass Contact Id and call the Flow. Name the page as Overwrite_Contact_Delete.

 <apex:page standardController="Contact">  
   <flow:interview name="Delete_Contact_Flow" finishLocation="{!URLFOR('/003')}">  
     <apex:param name="varContactId" value="{!Id}" />  
     <apex:param name="varContactName" value="{!Contact.Name}" />  

4. Enable Visualforce Page for Profile
Go to users Profile and enable Overwrite_Contact_Delete Visualforce page.

5. Overwrite Delete button for Contact

6. Deleted Records log
Use custom tab or report to monitor deleted Contacts.