function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
KenAllredKenAllred 

Need help writing an Opportunity Apex Trigger based on Contact Role

I am looking at a possible solution to help enforce that our sales reps add the correct contacts to an opportunity and assign roles to those contacts (as well as identifying who the primary contact is for the opportunity). The scenario would work like the following:

 

For our opportunities we know that we need at least two specific roles (defined in our opportunity contact roles) on every opportunity. I'd like to create two custom true/false fields on the opportunity (one for each role that we need). These fields would be updated by an apex trigger that would set the appropriate field to true when a contact was assigned the appropriate contact role for the opportunity.

 

I would also like to add a custom true/false field that would be updated to true when a contact associated with the opportunity was assigned as the "primary" contact (ensuring that all opportunities by a certain stage have a contact that has been assigned as the primary).

 

I could then have validation rules on those custom opportunity fields that wouldn't allow the opportunity stage to be advanced until the sales rep had identified and associated the required contacts/roles and primary.

 

So the opportunity contact roles that would be "required" would be:

 

  1. Sales leader
  2. Marketing leader

 

And the custom checkbox fields I would define for the Opportunity would be:

 

  1. Sales_Leader_Identified__c
  2. Marketing_Leader_Identified__c
  3. Primary_Assigned__c

I'm very new to the apex trigger world and would really love some help in creating this trigger if anyone is up for the challenge. Thanks for any help or guidance you can give here!

nj07nj07

Hi Ken,

 

Below is the code that you might be looking for:

 

//Trigger on OpportunityContactRole
trigger OppContactRole on OpportunityContactRole (after insert, after update)
{
OppContactRoleHandler objHandler = new OppContactRoleHandler();
if(trigger.isAfter && (trigger.isInsert || trigger.isUpdate))
objHandler.AfterInsert(trigger.new);

}
//End
//Handler Class
public with sharing class OppContactRoleHandler
{
private boolean m_isExecuting = false;
private integer BatchSize = 0;

public OppContactRoleHandler(boolean isExecuting, integer size)
{
m_isExecuting = isExecuting;
BatchSize = size;
}
//After insert Methods to update the Opportunities based on the Roles
public void AfterInsert(List<OpportunityContactRole> lstOppContactRole)
{
List<Opportunity> lstUpdateOpp = new List<Opportunity>();
for(OpportunityContactRole objConRole : lstOppContactRole)
{
if(objConRole.Role = 'Sales leader')
{
Opportunity objOpp = new Opportunity(Id = objConRole.OpportunityId);
objOpp.Sales_Leader_Identified__c = true;
if(objConRole.IsPrimary)
objOpp.Primary_Assigned__c = true;
lstUpdateOpp.add(objOpp);
}
else if(objConRole.Role = 'Marketing leader')
{
Opportunity objOpp = new Opportunity(Id = objConRole.OpportunityId);
objOpp.Marketing_Leader_Identified__c = true;
if(objConRole.IsPrimary)
objOpp.Primary_Assigned__c = true;
lstUpdateOpp.add(objOpp);
}
}
if(lstUpdateOpp != null && lstUpdateOpp.size() > 0)
update lstUpdateOpp;
}
//End
}
//End

 

Let me know incase of any issues.

 

If a reply to a post answers your question or resolves your problem, please mark it as the solution to the post so that others may benefit.

 

Thanks,

Nilesh Jain