Salesforce Hack Using APEX & Validation Rule

I’ve put together this quick guide on how to force Contact Roles to be Added to an Opportunity before it is closed. Here’s how we do it:

Step 1

Create a Custom Field (Number) on the Opportunity Object called “Number of Contacts”. Screen Shot 2016-03-08 at 13.09.11 Then Create a Custom Field (Checkbox) on the Opportunity Object called “Primary Contact Assigned”.

Screen Shot 2016-03-09 at 12.33.33

Step 2

You’ll need to create the below Apex Trigger to update your new “Number of Contacts” field with the number of Contact Roles present on the Opportunity each time the Opportunity is updated. At Ebsta, we also update the Primary Contact Assigned checkbox for a separate process. NB:  Triggers have to be developed in a Dev Org. You can the deploy code to your production instance in a few ways but I think these two are the most commonly used methods. 1) Using the Force.com IDE (search for a reference for this if you don’t already use it) – With your Production instance setup as a Force.com project, you can simply copy that trigger into the workspace of your Production instance and then build the project 2) Using the Force.com Migration Tool (search for a reference for this if you don’t already use it) – This allows you to deploy code using ANT (this requires some setup)

Heres what the Trigger looks like:

Screen Shot 2016-03-08 at 12.24.08

Heres the code:

1 trigger updatecontactrolecount on Opportunity (before insert, before update) 2 { 3 4 Boolean isPrimary; 5 Integer iCount; 6 Map<String, Opportunity> oppty_con = new Map<String, Opportunity>();//check if the contact role is needed and add it to the oppty_con map 7 for (Integer i = 0; i < Trigger.new.size(); i++) 8 { 9 oppty_con.put(Trigger.new[i].id,
10 Trigger.new[i]);
11 }
12 isPrimary = False;
13 for (List oppcntctrle :[select OpportunityId from OpportunityContactRole where (OpportunityContactRole.IsPrimary = True and OpportunityContactRole.OpportunityId in :oppty_con.keySet())])
14 {
15 if (oppcntctrle .Size() >0)
16 {
17 isPrimary = True;
18 }
19 }
20 iCount = 0;
21 for (List oppcntctrle2 : [select OpportunityId from OpportunityContactRole where (OpportunityContactRole.OpportunityId in :oppty_con.keySet())])//Query for Contact Roles

22 {
23 if (oppcntctrle2 .Size()>0)
24 {
25 iCount= oppcntctrle2 .Size();
26 }
27 }
28 for (Opportunity Oppty : system.trigger.new) //Check if roles exist in the map or contact role isn't required
29 {
30 Oppty.Number_of_Contacts_Roles_Assigned__c = iCount;
31 Oppty.Primary_Contact_Assigned__c =isPrimary;
32 }
33 }

Step 3

Add a validation rule to your Opportunity Object  (search for a reference for this if you don’t already use it)
Screen Shot 2016-03-08 at 12.23.52
The validation rule we have says:  If the Opportunity Stage name is being set to Closed Won or Closed Lost and the Type is New Business, display a message “You must add a contact role” if the “Number of Contact roles” is zero.

Result

Now this is in place no New Business Opportunity can be closed by a Sales Person without a Contact Role being associated. This in turn helps us to accurately track Marketing ROI.
For more Salesforce hacks, join us for our next webinar with Empaua Founder & COO Nicklas Teicke on How to Use Salesforce Automation Tools Like a ProWe can’t wait to let you in on heaps more Salesforce secrets!]]>

Posted in

Speak with our experts and see how Ebsta will help improve your sales

New call-to-action
New call-to-action
Logo Icon (White)@2x

Newsletter Signup

Share this article

Related Content

How to Demonstrate ROI of Revenue Operations with Julian Hannabuss, Director of Revenue Operations at Procurify

In this episode of the Revenue Insights Podcast, host Lee Bierton is joined by Julian Hannabuss, Director of Revenue Operations at Procurify, a leading procurement and purchasing software company that lets teams track, control, and analyze all business spending so they can scale faster. Julian shares his insights on how revenue operations should present their revenue outcomes and can drive organizational value to the board. He also shares his insights on the difference between high and average performers in the sales and revenue teams. Julian shares some tips on how to mitigate churn at your company.

How to Build Your Pipeline Through Social Selling with Tim Hughes, CEO of DLA Ignite

In this episode of the Revenue Insights Podcast, host Lee Bierton is joined by Timothy Hughes, CEO of DLA Ignite, a strategic advisory and consultancy enterprise that enables organizations to leverage social selling to convert pipeline leads. Tim shares a three-step social selling process to build pipeline leads and highlights how conversations rather than content play a pivotal role in the conversion process. Conversions pivot around educating the prospects on their pain areas and then offering solutions to resolve the issues. The episode is also a gold mine for sales leaders looking for insights that are easy to adopt and implement.

The Four-Step Framework to Reimagine Sales Teams with Ben Stroup, President at Velocity Strategy Solutions

In this episode of the Revenue Insights Podcast, host Lee Bierton is joined by Ben Stroup, President at Velocity Strategy Solutions, an on-demand strategy and management consulting firm. Ben shares his insights on how Velocity uses people, processes, technology, and data to reimagine sales and revenue teams, and move the needle toward a modern-day revenue operation and management approach. Companies must move from monitoring lagging indicators like revenue to analyzing leading indicators like customer acquisition costs (CAC) and customer lifetime value (LTV). Ben also touches on the importance of aligning internal teams to a common goal.