Skip to content

Salesforce Hack Using APEX and Validation Rule


Salesforce Admin Tips
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:

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 IDE (search for a reference for this if you don’t already use it) – With your Production instance setup as a project, you can simply copy that trigger into the workspace of your Production instance and then build the project 2) Using the 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 <; i++) 8 { 9 oppty_con.put([i].id, 10[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 : //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.


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!

Ricky Wheeler

Ricky Wheeler is CMO at Ebsta, responsible for all aspects of Brand, Product and Communications.

Related Posts

Give your Sales Reps access to the best data

salesforce-email-data-quality Long gone are the days where you had to rely on your Reps to add emails or data into Salesforce. Ebsta does it automatically. Reps now have access to 100% data accuracy without having to do a thing.