Hands on 6 - invoking a Flow using an Inline Visualforce page

It is also possible to call a Flow through an Inline Visualforce page. Let's start with a business scenario.

Sara Bareilles, who is working as a system administrator with Universal Containers, receives a requirement as soon as a contact is added to an Opportunity (that is, using an Opportunity contact role) as a primary contact to the copy contact's lead source to the Opportunity lead source.

There are no out-of-the-box solutions for this. You can't achieve a preceding business requirement using Workflow Rule. You can achieve it through the Apex code. We will use Flow and an Inline Visualforce page to achieve it, the steps to create which are as follows:

  1. Navigate to Setup | Build | Create | Workflow & Approvals | Flows, and then click on the New Flow button; it will open the Flow canvas for you.
  2. Create three Text variables, VarTOppId (to pass the Opportunity ID from the Visualforce page), VarTConId (to save the primary contact ID), and VarTLeadSource (to store the primary contact lead source).
  3. The first task is to get the primary contact from the Opportunity; for this, we will use the Record Lookup element. Click on the Palette tab and drag and drop the Record Lookup element onto the canvas; it will open a new window for you, where you have to enter the following details:
    • Name: Enter the name for the Record Lookup element. Enter Get Primary Contact as the name.
    • Unique Name: This will be autopopulated based on the name.
    • Description: Write some meaningful text so that another developer/administrator can easily understand why this Record Lookup element is added to Flow.
    • Look up: Select the object for which you want to search the record. In this case, select the OpportunityContactRole object. The next task is to define the search criteria. If you want to add multiple fields for search criteria, then click on the Add Row link to add the new row. For the field, select IsPrimary equal to {!$GlobalCOnstant.True} and OpportunityId equal to {!VArTOppId}. You can get help from the following screenshot to define the search criteria.
    • Assign the record's fields to variables to reference them in your flow: Optionally, you can save the field's value into variables so that you can use it later in the Flow. Save ContactId into the varTConId variable.

Don't forget to select the Assign null values to the variable(s) if no records are found checkbox, as shown in the following screenshot:

  1. Once you are done, click on the OK button.
  2. The next task is to check whether Opportunity has any contact role or not. To check this, we will use the Decision element and check whether the ContactId variable contains any value or not. To do that, drag and drop the Decision element onto the Flow canvas. Enter the Name field as Verify Primary Contact Role and Unique Name will get autopopulated based on the name. Optionally, you can also add Description for the Decision element. Next, create two outcomes for the Decision element, which are as follows:
    • Exist: Select variable VarTConId does not equal to {!$GlobalConstant.EmptyString}.
    • Not Exist: Enter the name Not Found as DEFAULT OUTCOME.

It will look like what is shown in the following screenshot:

  1. Once you are done, click on the OK button.
  2. The next step is to find the lead source for the Opportunity primary contact role. For this, we will use the Record Lookup element. Drag and drop the Record Lookup element onto the canvas; it will open a new window for you, where you have to enter the following details:
    • Name: Enter the name for the Record Lookup element. Enter Get Contact Lead Source as the name.
    • Unique Name: This will be autopopulated based on the name.
    • Description: Write some meaningful text so that another developer/administrator can easily understand why this Record Lookup element is added to Flow.
    • Look up: Select the object for which you want to search the record. In this case, select the Contact object. The next task is to define the search criteria. Add Id of Contact equals the VarTConId variable.
    • Assign the record's fields to variables to reference them in your flow: Optionally, you can save the field's value into variables, so you can use it later in the Flow. Save LeadSource into the varTLeadSource variable.

Don't forget to select the Assign null values to the variable(s) if no records are found checkbox; it will look like what is shown in the following screenshot:

  1. Once you are done, click on the OK button.
  2. The final task is to add the Record Update element to update the Opportunity lead source. Drag and drop the Record Update element onto the canvas, and it will open a new window for you, where you have to enter the following details:
    • Name: Enter the name for the Record Update element. Enter Update Opportunity Lead Source as the name.
    • Unique Name: This will be autopopulated based on the name.
    • Description: Write some meaningful text so that another developer/administrator can easily understand why this Record Update element was added to a Flow.
    • Update: Select the object for which you want to update the record. In this case, select the Opportunity object. The next task is to set the filter criteria to identify which record gets updated; in this case, select Id of Opportunity equals VarTOppId. Then, select the fields from the object that you want to update and assign the value or resource (input, constant, and so on ) to it; (data types must match) in this case, select the LeadSource field and for the value from drop-down variables, select VarTLeadSource. To add multiple fields, click on the Add Row link.

The Record Update element should look like what is shown in the following screenshot:

  1. Once you are done, click on the OK button.
  2. Save your Flow with the name Update Opportunity Lead Source and close the canvas using the Close button; it will redirect you to the Flow detail page. Don't forget to activate the Flow.
  3. The next task is to add the Flow to a Visualforce page. Navigate to Setup | Build | Develop | Pages and click on New to add a new Visualforce page, embed the Flow into the Visualforce page, and pass the value the VarTOppId variable using the standard controller. In the following code, apex:param is used to pass the value in the VarTOppId variable:
        <apex:page standardController="Opportunity"> 
<flow:interview name="Update_Opportunity_Lead_Source">
<apex:param name="VarTOppId" value="{!Opportunity.id}"/>
</flow:interview>
</apex:page>
  1. Save the Visualforce page with the name Update Opportunity Lead Source.
  2. Now we have to add the Visualforce page to the Opportunity page layout. Navigate to Setup | Build | Customize | Opportunities | Page Layouts, click on the Edit link and drag and drop the Visualforce page Update Opportunity Lead Source on to the page layout, and set the Visualforce page width to 0 px and height to 0. Once you are done, click on the Save button to save the changes.

It will look like what is shown in the following screenshot:

Now onward, if the user adds or updates the opportunity contact role as primary, then Flow will fire and automatically update the Opportunity lead source to be the same as the primary contact role lead source.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.137.143.219