For everyone that attended my How to accurately track Marketing ROI with Salesforce and Pardot webinar last week, 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:
Create a Custom Field (Number) on the Opportunity Object called “Number of Contacts”.
Then Create a Custom Field (Checkbox) on the Opportunity Object called “Primary Contact Assigned”.
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:
Heres the code:
1 trigger updatecontactrolecount on Opportunity (before insert, before update)
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++)
12 isPrimary = False;
13 for (List oppcntctrle :[select OpportunityId from OpportunityContactRole where (OpportunityContactRole.IsPrimary = True and OpportunityContactRole.OpportunityId in :oppty_con.keySet())])
15 if (oppcntctrle .Size() >0)
17 isPrimary = True;
20 iCount = 0;
21 for (List oppcntctrle2 : [select OpportunityId from OpportunityContactRole where (OpportunityContactRole.OpportunityId in :oppty_con.keySet())])//Query for Contact Roles
23 if (oppcntctrle2 .Size()>0)
25 iCount= oppcntctrle2 .Size();
28 for (Opportunity Oppty : system.trigger.new) //Check if roles exist in the map or contact role isn't required
30 Oppty.Number_of_Contacts_Roles_Assigned__c = iCount;
31 Oppty.Primary_Contact_Assigned__c =isPrimary;
Add a validation rule to your Opportunity Object (search for a reference for this if you don’t already use it)
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.
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 Pro. We can’t wait to let you in on heaps more Salesforce secrets!