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
HTANIRSHTANIRS 

trigger on Opportunity to insert opportunity contact role

Hi Friends,

I have a requirement when I need to insert Opportunity Contact Role when Opportunity is created.
My code is working fine when creating a Opportunity from Contact but a duplicate is getting created when Converting a Opportunity from Lead.
 
trigger updateContactRole on Opportunity (after insert, after update) {
    
    System.debug('-- Inside Opportunity Contact Role Trigger---');
    
    List<OpportunityContactRole> newContactRoleList = new List<OpportunityContactRole>();
    Map<Id, Id> opportunityAccountMap = new Map<Id, Id>();
	Map<Id, List<Id>> mapOpportunityContact = new Map<Id, List<Id>>();
	Map<Id, List<Id>> mapOpportunityContactRole = new Map<Id, List<Id>>();
	
	for(Opportunity opp : trigger.new) {
		if(opp.AccountId != null) {
			opportunityAccountMap.put(opp.AccountId, opp.Id);
		}
	}
	
	// get contacts from opportunity account 
	for(Contact cont: [SELECT Id, AccountId FROM Contact WHERE accountId IN: opportunityAccountMap.keySet()]) {
		List<Id> contactIds = new List<Id>();
		if(mapOpportunityContact.containsKey(opportunityAccountMap.get(cont.AccountId))) {
			contactIds = mapOpportunityContact.get(opportunityAccountMap.get(cont.AccountId));
		}
		contactIds.add(cont.Id);
		mapOpportunityContact.put(opportunityAccountMap.get(cont.AccountId), contactIds);
	}
	
	
	// get contacts from opportunity contact role. 
	for(OpportunityContactRole contRole: [SELECT Id, OpportunityId, ContactId FROM OpportunityContactRole 
											WHERE OpportunityId IN: opportunityAccountMap.values()]) {
		List<Id> oppContactIds = new List<Id>();
		if(mapOpportunityContactRole.containsKey(contRole.OpportunityId)) {
			oppContactIds = mapOpportunityContactRole.get(contRole.OpportunityId);
		}
		oppContactIds.add(contRole.ContactId);
		mapOpportunityContactRole.put(contRole.OpportunityId, oppContactIds);
	}
	
	
	for(Id oppId : mapOpportunityContact.keySet()) {		
		// opp account contacts
		List<Id> contactRoles = new List<Id>();
        if(mapOpportunityContactRole.containsKey(oppId)) {
            contactRoles = mapOpportunityContactRole.get(oppId);
        }
		
		for(Id contId :  mapOpportunityContact.get(oppId)){
			if(!contactRoles.contains(contId)){
				OpportunityContactRole myContactRole = new OpportunityContactRole();
				myContactRole.ContactId = contId;
				myContactRole.OpportunityId = oppId;
                                myContactRole.isPrimary = true;
				newContactRoleList.add(myContactRole);
			}
		}		
	}
    
    try {
        if(newContactRoleList.size()>0) {
            insert newContactRoleList;
        }
    }

    catch(Exception e) {
        System.debug(e);
    }
}

Kindly refer the code and let me know how can I solve this issue.

Thanks in advance..
Best Answer chosen by HTANIRS
David Zhu 🔥David Zhu 🔥
When converting a Lead with creating a new  Opportunity, Saleforce create a contact role with lead information automatically. You trigger does the samething. That is why you get the duplicates. 
You may add a custom checkbox field on both Lead and Opportunity object, let say "Created From Lead Conversion", then map two fields at lead conversion.
In you trigger, if Opportunity.Created_From_Lead_Conversion is try, then skip the trigger.

All Answers

David Zhu 🔥David Zhu 🔥
When converting a Lead with creating a new  Opportunity, Saleforce create a contact role with lead information automatically. You trigger does the samething. That is why you get the duplicates. 
You may add a custom checkbox field on both Lead and Opportunity object, let say "Created From Lead Conversion", then map two fields at lead conversion.
In you trigger, if Opportunity.Created_From_Lead_Conversion is try, then skip the trigger.
This was selected as the best answer
HTANIRSHTANIRS
Hi David,

Thank you for your solution. This worked fine. I can able to restrict creating duplicate contact role.