You need to sign in to do that
Don't have an account?
Apex Trigger to create Opportunity Contact Role based on Contact Lookup field
Hey all,
I have created the following Apex trigger that will create, update and delete Opportunity Contact Roles based on a Contact Lookup field placed on the Opportunity. Everything works great currently. I am just trying to figure out one final scenario. If John Doe already has a Contact Role but is not the Primary Contact Role and I want him to be. I want to be able to change my look up field to him and then have that check him as the primary. Currently it just creates a new one and checks them both as primary. So what I need to do is check the current Contact Roles and if there is already one with the same name as in the Lookup field then just check that one as primary, but if there isn't then create a new one. Below is the code. Please let me know if you need further description.
Thanks,
I have created the following Apex trigger that will create, update and delete Opportunity Contact Roles based on a Contact Lookup field placed on the Opportunity. Everything works great currently. I am just trying to figure out one final scenario. If John Doe already has a Contact Role but is not the Primary Contact Role and I want him to be. I want to be able to change my look up field to him and then have that check him as the primary. Currently it just creates a new one and checks them both as primary. So what I need to do is check the current Contact Roles and if there is already one with the same name as in the Lookup field then just check that one as primary, but if there isn't then create a new one. Below is the code. Please let me know if you need further description.
Thanks,
trigger CreateContactRole on Opportunity (after insert, after update) { List<OpportunityContactRole> newContactRoleList = new List<OpportunityContactRole>(); List<OpportunityContactRole> oldContactRoleList = new List<OpportunityContactRole>(); Set<Id> OppId = new Set<Id>(); sET<Id> ContactId = new Set<Id>(); if(Trigger.isInsert) { for(Opportunity opp : Trigger.new) { if(opp.Primary_Contact__c != null) { //Creating new Contact Role newContactRoleList.add(new OpportunityContactRole(ContactId=opp.Primary_Contact__c,OpportunityId=opp.Id,Role='Decision Maker',IsPrimary=true)); } } } if(Trigger.isUpdate) { for(Opportunity opp : Trigger.new) { if(opp.Primary_Contact__c != null && Trigger.oldMap.get(opp.Id).Primary_Contact__c == null) { //Creating new Contact Role newContactRoleList.add(new OpportunityContactRole(ContactId=opp.Primary_Contact__c,OpportunityId=opp.Id,Role='Decision Maker',IsPrimary=true)); } else if(opp.Primary_Contact__c != null && Trigger.oldMap.get(opp.Id).Primary_Contact__c != null) { //Create New Contact Role make new CR Primary over the old CR Opportunity OldOpp = Trigger.oldMap.get(opp.Id); OppId.add(OldOpp.id); ContactId.add(OldOpp.Primary_Contact__c); newContactRoleList.add(new OpportunityContactRole(ContactId=opp.Primary_Contact__c,OpportunityId=opp.Id,Role='Decision Maker',IsPrimary=true)); } else if(opp.Primary_Contact__c == null && Trigger.oldMap.get(opp.Id).Primary_Contact__c != null) { Opportunity OldOpp = Trigger.oldMap.get(opp.Id); OppId.add(OldOpp.id); ContactId.add(OldOpp.Primary_Contact__c); try { //Deleting old Contact Roles if(oldContactRoleList.size()>0) delete oldContactRoleList; } catch(Exception e) { System.debug(e); trigger.new[0].addError('An error has occurred. Please contact your system administrator.'); } } } } try { //inserting new contact roles if(newContactRoleList.size()>0)insert newContactRoleList; //Selecting old Contact Roles if(OppId.size()>0) oldContactRoleList = [Select Id from OpportunityContactRole where ContactId in : ContactId and OpportunityId in : OppId]; } catch(Exception e) { System.debug(e); trigger.new[0].addError('An error has occurred. Please contact your system administrator.'); } }
Please use the below code.
Thanks
Anupama
Thanks for the response. I attempted to use your code and it is still not working properly.
Assuming not working properly means it's creating a duplicate contact role? If that's the case, you should consider switching to a map instead of using a list. This way you're not creating duplicates.
I would first build the map of contact roles to opportunities (so a map of a map). We use something like this to create contact roles for opportunities when the opp is created, moved to another account or closed. Something like this:
Then when you're iterating through your opportunities to determine if the Primary_Contact__c has been updated, then you'll use something like this to pull the list of existing contact roles:
Then something like this to update the existing one if it matches:
And then you'll upsert on the map like this
Hopefully I've given you enough examples to make something of it, but using the list is definitely the source of your woes because you're not updating the existing contact role.
sorry - wrong upsert statement listed above - use this for upsert:
I have altered my code quite a bit since I made this post. Below is my new code. The same issue is still relevant though.
You also do not need to be using a query in a for loop. That will eat up your governor limits very quickly. While we're on best practices, I highly suggest taking the 'functionality' of your code and moving it to a class and limiting your trigger to just identifying opps that meet a critiera.
We can’t add any custom fields into OpportunityContactRole Object.
We can’t add any validation rules on OpportunityContactRole object.
We can’t create any trigger on this OpportunityContactRole Object.
Please refer below link for your reference.
http://sfdcsrini.blogspot.com/2014/04/what-is-opportunity-contact-role.html
We can’t add any custom fields into OpportunityContactRole Object.
We can’t add any validation rules on OpportunityContactRole object.
We can’t create any trigger on this OpportunityContactRole Object.
Please refer below link for your reference.
http://sfdcsrini.blogspot.com/2014/04/what-is-opportunity-contact-role.html