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
The new LearnerThe new Learner 

Regarding associating contacts to opportunity

Hi Experts,

Can anyone help me out, how to achieve the below functionality using a trigger.

Need to link all contacts from the related account that are flagged as key contacts to an opportunity on that account when it is created.
Thanks in advance.
Best Answer chosen by The new Learner
Suraj Tripathi 47Suraj Tripathi 47
Hi,
Kindly find solution.
//trigger
trigger ContactTrigger on Contact (after insert) {
   
    if(Trigger.isAfter && Trigger.isInsert){        
        InsertContactToOppContactRole.insertContact( Trigger.new);  
    }
    
}
// class
public class InsertContactToOppContactRole {
    public static void InsertContact(List<Contact> conList){
        Set<Id> accountIdSet =new Set<Id>();
        for(Contact con:conList){
            accountIdSet.add(con.AccountId);
            
        }
        Map<Id, Account> accountMap = new Map<Id, Account>([Select id from Account where Id in :accountIdSet]); 
        List<Opportunity> opportunityList = new List<Opportunity>([select id ,AccountId from Opportunity where Id in:accountMap.keySet()]);
        Map<Id, List<Opportunity>> accountId_Vs_ListOfOpportunity = new Map<Id, List<Opportunity>>();
        
        for(Opportunity opp : opportunityList){
            if( !accountId_Vs_ListOfOpportunity.containsKey(opp.AccountId)){
                List<Opportunity> oppList = new List<Opportunity>();
                oppList.add(opp);
                accountId_Vs_ListOfOpportunity.put(opp.AccountId, oppList);
                
            }
            else{
                List<Opportunity> oppList = new List<Opportunity>();
                oppList = accountId_Vs_ListOfOpportunity.get(opp.AccountId);
                oppList.add(opp);
                accountId_Vs_ListOfOpportunity.put(opp.AccountId, oppList);
            }
            
        }
        List<OpportunityContactRole> ocrList = new List<OpportunityContactRole>();
        for(Contact con : conList){
            if(accountId_Vs_ListOfOpportunity.containsKey(con.AccountId)){
                List<Opportunity> oppList = accountId_Vs_ListOfOpportunity.get(con.AccountId);
                for(Opportunity opp: oppList){
                    OpportunityContactRole oppConRole = new OpportunityContactRole();
                    oppConRole.ContactId = con.Id;
                    oppConRole.OpportunityId = opp.Id;
                    ocrList.add(oppConRole);
                }  
            }
            
        }
        if(!ocrList.isEmpty()){
            insert ocrList;
            
        }
    }
}
If you find your Solution than mark as this as a best answer. 

Thanks and Regards
Suraj Tripathi.

All Answers

Suraj Tripathi 47Suraj Tripathi 47
Hi,
Kindly find solution.
//trigger
trigger ContactTrigger on Contact (after insert) {
   
    if(Trigger.isAfter && Trigger.isInsert){        
        InsertContactToOppContactRole.insertContact( Trigger.new);  
    }
    
}
// class
public class InsertContactToOppContactRole {
    public static void InsertContact(List<Contact> conList){
        Set<Id> accountIdSet =new Set<Id>();
        for(Contact con:conList){
            accountIdSet.add(con.AccountId);
            
        }
        Map<Id, Account> accountMap = new Map<Id, Account>([Select id from Account where Id in :accountIdSet]); 
        List<Opportunity> opportunityList = new List<Opportunity>([select id ,AccountId from Opportunity where Id in:accountMap.keySet()]);
        Map<Id, List<Opportunity>> accountId_Vs_ListOfOpportunity = new Map<Id, List<Opportunity>>();
        
        for(Opportunity opp : opportunityList){
            if( !accountId_Vs_ListOfOpportunity.containsKey(opp.AccountId)){
                List<Opportunity> oppList = new List<Opportunity>();
                oppList.add(opp);
                accountId_Vs_ListOfOpportunity.put(opp.AccountId, oppList);
                
            }
            else{
                List<Opportunity> oppList = new List<Opportunity>();
                oppList = accountId_Vs_ListOfOpportunity.get(opp.AccountId);
                oppList.add(opp);
                accountId_Vs_ListOfOpportunity.put(opp.AccountId, oppList);
            }
            
        }
        List<OpportunityContactRole> ocrList = new List<OpportunityContactRole>();
        for(Contact con : conList){
            if(accountId_Vs_ListOfOpportunity.containsKey(con.AccountId)){
                List<Opportunity> oppList = accountId_Vs_ListOfOpportunity.get(con.AccountId);
                for(Opportunity opp: oppList){
                    OpportunityContactRole oppConRole = new OpportunityContactRole();
                    oppConRole.ContactId = con.Id;
                    oppConRole.OpportunityId = opp.Id;
                    ocrList.add(oppConRole);
                }  
            }
            
        }
        if(!ocrList.isEmpty()){
            insert ocrList;
            
        }
    }
}
If you find your Solution than mark as this as a best answer. 

Thanks and Regards
Suraj Tripathi.
This was selected as the best answer
The new LearnerThe new Learner
Hi Suraj,

Can i ask you one more question , here we need to add all the contacts to the opportunity , when the account id is same for both records ? Kindly help me out. please
The new LearnerThe new Learner
Hi Suraj,

This is how  I performed testing.
 1. Created a contact and associated that with the account(which have already opportunity) but contactrole is not creating for that opportunity. can you help me out please.
 
Suraj Tripathi 47Suraj Tripathi 47
Hi,
Kindly find solution.
// Test Class
@isTest
public class InsertContactToOppContactRoleTest {
    
    @isTest
    public static void test(){
        Account acc = new Account( Name = 'Test Account');
        insert acc;
        Opportunity opp = new Opportunity();
        opp.Name = 'Test Opp';
        opp.StageName = 'Prospecting';
        opp.CloseDate = Date.today();
        opp.AccountId = acc.id;
        insert opp;
        
        Test.startTest();
        
        Contact con = new Contact();
        con.LastName ='Test Contact';
        con.AccountId = acc.Id;
        Insert con;
        
        Test.stopTest();
        
    }
}
If you find your Solution than mark as this as a best answer. 

Thanks and Regards
Suraj Tripathi.
The new LearnerThe new Learner
Hi Suraj,

I dont know why contacrole is not creating using this trigger. can you help me out, whether I am doing anything wrong?