Creative Way to Capture Opportunity Contact Role

Search Layouts, Often Forgotten and Misunderstood
September 9, 2013
create salesforce record
Create a Salesforce Record with a Button
October 24, 2013

Creative Way to Capture Opportunity Contact Role

Applies to: Classic

Need a creative way to capture Opportunity Contact Roles?  My method uses a couple simple Visualforce pages to redirect the an Opportunity to the Contact Role if no roles have been added.  Technically it’s not making a Contact Roles required but it does go a long way toward annoying the Opportunity Owner until it’s entered.  Here’s how to set it up.

Create a VisualPage page called:  OpportunityView

<apex:page standardController="Opportunity"><br /><apex:detail inlineedit="true" showchatter="True"/><br /></apex:page><br />

This page will replace the standard opportunity page but continues to leverage your Page Layout forms for easy management.

Now create the following Apex Extension called: OpportunityRedirect

&amp;lt;br /&amp;gt;public with sharing class OpportunityRedirect {&amp;lt;br /&amp;gt;String oppId;&amp;lt;br /&amp;gt;String ownerId;&amp;lt;br /&amp;gt;private final Opportunity Oppty;&amp;lt;/p&amp;gt;<br>
&amp;lt;p&amp;gt;public OpportunityRedirect(ApexPages.StandardController stdController) {&amp;lt;br /&amp;gt;oppId = stdController.getId();&amp;lt;br /&amp;gt;Oppty = (Opportunity) stdController.getRecord();&amp;lt;br /&amp;gt;ownerId = oppty.OwnerId;&amp;lt;br /&amp;gt;}&amp;lt;/p&amp;gt;<br>
&amp;lt;p&amp;gt;public PageReference redirect() {&amp;lt;br /&amp;gt;PageReference pageDetail = new PageReference ('/p/opp/ContactRoleEditUi/e?oppid=' + oppId + '&amp;amp;amp;amp;amp;saveURL=' + oppId+ '&amp;amp;amp;amp;amp;cancelURL=/006/o');&amp;lt;br /&amp;gt;pageDetail.setRedirect(true);&amp;lt;/p&amp;gt;<br>
&amp;lt;p&amp;gt;OpportunityContactRole[] ocr = [select ContactId, Role, isPrimary from OpportunityContactRole where OpportunityId = :oppId];&amp;lt;br /&amp;gt;if (ocr.size() == 0 &amp;amp;amp;amp;amp;&amp;amp;amp;amp;amp; UserInfo.getUserId() == ownerId) return pageDetail;&amp;lt;/p&amp;gt;<br>
&amp;lt;p&amp;gt;PageReference pageOpportunity = new PageReference('/apex/OpportunityView');&amp;lt;br /&amp;gt;pageOpportunity.getParameters().put('id',oppId);&amp;lt;br /&amp;gt;pageOpportunity.setRedirect(true);&amp;lt;br /&amp;gt;return pageOpportunity;&amp;lt;br /&amp;gt;}&amp;lt;/p&amp;gt;<br>
&amp;lt;p&amp;gt;public static testmethod void test_OpportunityRedirect(){&amp;lt;br /&amp;gt;Account a = new Account();&amp;lt;br /&amp;gt;insert a;&amp;lt;/p&amp;gt;<br>
&amp;lt;p&amp;gt;Opportunity o = new Opportunity();&amp;lt;br /&amp;gt;o.AccountId = a.id;&amp;lt;br /&amp;gt;insert o;&amp;lt;/p&amp;gt;<br>
&amp;lt;p&amp;gt;ApexPages.StandardController sc = new ApexPages.standardController(o);&amp;lt;br /&amp;gt;OpportunityRedirect myPageCon = new OpportunityRedirect(sc);&amp;lt;br /&amp;gt;myPageCon.redirect();&amp;lt;br /&amp;gt;}&amp;lt;br /&amp;gt;}&amp;lt;br /&amp;gt;

Now that you have the Extension in place, create a second Visualforce page called: OpportunityRedirect

&amp;amp;amp;amp;lt;apex:page standardController=&amp;quot;Opportunity&amp;quot;&amp;lt;br /&amp;gt;extensions=&amp;quot;OpportunityRedirect&amp;quot; action=&amp;quot;{!redirect}&amp;quot;&amp;amp;amp;amp;gt;&amp;lt;br /&amp;gt;&amp;amp;amp;amp;lt;apex:outputfield value=&amp;quot;{!Opportunity.OwnerId}&amp;quot;/&amp;amp;amp;amp;gt;&amp;lt;br /&amp;gt;&amp;amp;amp;amp;lt;/apex:page&amp;amp;amp;amp;gt;&amp;lt;br /&amp;gt;

This page replaces the out of the box Opportunity View screen but its purpose is to intercept the screen to see if any Opportunity Contact Role record exist. If there are record, then the OpportunityView page is displayed. If no records exist then the extension opens the Contact Role page.

The final step is to then change the default Opportunity View page within Salesforce. To do this:

  • Navigate to Setup | Customize | Opportunities | Buttons, Links, and Actions.
  • Click Edit next to the View button link
  • Click the Visualforce Page option in the Override With field
  • Select OpportunityRedirect which should be listed as an option in the Visualforce Page picklist.

Hope my method helps you capture the data you need.

Terry Miller
Terry Miller
Terry has spent over 20 years focused on business leadership and information technology. As an independent consultant, he enjoys working with a variety of customers to help them solve business problems using the Salesforce® platform. His ability to quickly identify bottlenecks and provide understandable solutions has gained him the trust of his customers. If you're looking for expert guidance on your next Salesforce® project, click here to contact Terry today.

Leave a Reply