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
&amp;lt;apex:page standardController="Opportunity"&amp;gt;<br />&amp;lt;apex:detail inlineedit="true" showchatter="True"/&amp;gt;<br />&amp;lt;/apex:page&amp;gt;<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
<br />public with sharing class OpportunityRedirect {<br />String oppId;<br />String ownerId;<br />private final Opportunity Oppty;</p> <p>public OpportunityRedirect(ApexPages.StandardController stdController) {<br />oppId = stdController.getId();<br />Oppty = (Opportunity) stdController.getRecord();<br />ownerId = oppty.OwnerId;<br />}</p> <p>public PageReference redirect() {<br />PageReference pageDetail = new PageReference ('/p/opp/ContactRoleEditUi/e?oppid=' + oppId + '&amp;amp;saveURL=' + oppId+ '&amp;amp;cancelURL=/006/o');<br />pageDetail.setRedirect(true);</p> <p>OpportunityContactRole[] ocr = [select ContactId, Role, isPrimary from OpportunityContactRole where OpportunityId = :oppId];<br />if (ocr.size() == 0 &amp;amp;&amp;amp; UserInfo.getUserId() == ownerId) return pageDetail;</p> <p>PageReference pageOpportunity = new PageReference('/apex/OpportunityView');<br />pageOpportunity.getParameters().put('id',oppId);<br />pageOpportunity.setRedirect(true);<br />return pageOpportunity;<br />}</p> <p>public static testmethod void test_OpportunityRedirect(){<br />Account a = new Account();<br />insert a;</p> <p>Opportunity o = new Opportunity();<br />o.AccountId = a.id;<br />insert o;</p> <p>ApexPages.StandardController sc = new ApexPages.standardController(o);<br />OpportunityRedirect myPageCon = new OpportunityRedirect(sc);<br />myPageCon.redirect();<br />}<br />}<br />
Now that you have the Extension in place, create a second Visualforce page called: OpportunityRedirect
&amp;lt;apex:page standardController="Opportunity"<br />extensions="OpportunityRedirect" action="{!redirect}"&amp;gt;<br />&amp;lt;apex:outputfield value="{!Opportunity.OwnerId}"/&amp;gt;<br />&amp;lt;/apex:page&amp;gt;<br />
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:
Hope my method helps you capture the data you need.