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
Shri BEShri BE 

test class for contact roles on opportunity

Hi Friends,

I have done trigger on Opportunity to insert Contact role when Opportunity is inserted based on accountId. I have a issue in Test Class where it covers only 23%. Kindly review and assist to complete the test class.
 
TestClass:

@isTest
public class updateContactRole_Test {
    static testMethod void createAccount()
    {    
        Account a = new Account();
        a.Name = 'Test Co.';
        a.BillingStreet = '298 S. Ringo Street';
        a.BillingCity = 'Little Rock';
        a.BillingState = 'AR';
        a.BillingPostalCode = '72201';
        a.BillingCountry = 'USA';
        a.Phone = '501-555-5555';
        a.Website = 'www.testco.com';
        insert a;
        System.debug('created account');
            
        //Then create a primary contact
        Contact c = new Contact();
        c.FirstName = 'Paul';
        c.LastName  = 'Test';
        c.AccountId = a.id;
        c.MailingStreet = '298 S. Ringo Street';
        c.MailingCity = 'Little Rock';
        c.MailingState = 'AR';
        c.MailingPostalCode = '72201'; 
        insert c;
        System.debug('created primary contact');
            
        //Now create an opportunity
        Opportunity o = new Opportunity();
        o.Name = 'New Record';
        o.StageName = 'Posted';
        o.CloseDate = Date.today();
        o.Description = 'Test Record';
        insert o;
        System.debug('created opportunity');
                  
        //Now update the OCR for the primary contact
        OpportunityContactRole ocr = new OpportunityContactRole();
        ocr.ContactId = c.Id;
        ocr.OpportunityId = o.Id;
        ocr.IsPrimary = TRUE;
        ocr.Role = 'Decision Maker';
        insert ocr;
        System.debug('created opportunity contact role for primary');          
        
    }
}
 
Trigger:

trigger updateContactRole on Opportunity (after insert, after update) {
    
    System.debug('-- Inside Opportunity Contact Role Trigger---');
    
    List<OpportunityContactRole> newContactRoleList = new List<OpportunityContactRole>();
    Set<Id> oppId = new Set<Id>();
    
    for(Opportunity o : [SELECT Id, AccountId FROM Opportunity WHERE AccountId IN: oppId]) {
       
/* Code not covering inside for loop */
 List <Contact> contacts = [SELECT Id FROM Contact WHERE accountId = : o.accountId];
        
        List <OpportunityContactRole> contactRoles = [SELECT Id, ContactId FROM OpportunityContactRole WHERE OpportunityId = :o.Id];
        
        if (contacts.size() > contactRoles.size()) {
            Set<Id> myContacts = new Set<Id>();
            for (OpportunityContactRole contactRole : contactRoles) {
                myContacts.add(contactRole.ContactId);
            }
            for (Contact contact : contacts) {
                if (!myContacts.contains(contact.Id)) {
                    OpportunityContactRole myContactRole = new OpportunityContactRole();
                    myContactRole.ContactId = contact.Id;
                    myContactRole.OpportunityId = o.Id;
                    newContactRoleList.add(myContactRole);
                    
                    // To prevent duplicate.
                    myContacts.add(contact.Id); 
                }
            }
        }
    }
    
    try {
        if(newContactRoleList.size()>0) {
            insert newContactRoleList;
        }
    }

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

Thanks in advance.
ShirishaShirisha (Salesforce Developers) 
Hi Shri,

Greetings!

As per the trigger,I can see that you are trying to query the Opportunity and Contacts based on the AccountId and trying to insert the Contacts of related Accounts to OCR Object.

So,I would suggest you to insert the sample Account record ,Opportunity record and insert 2 contacts related to the created Account.Once you insert then use the method in the trigger to check the same.

Can you please check the sample code provided in the below document for example:

https://developer.salesforce.com/forums/?id=9060G000000XcAKQA0

Kindly let me know if it helps you and close your query by marking it as best answer so that it can help others in the future.

Warm Regards,
Shirisha Pathuri