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
AmulyaAmulya 

Code coverage for if

Hi All,
I am not able to write the test class code to cover highlighted lines in below trigger code. Please help.

trigger AddPrimaryContRole on Opportunity (after insert, after update) 
{
    for (Opportunity o : Trigger.new) 
    {   
        Integer i=0, j=0;  
        if(Trigger.isUpdate && (o.Primary_Contact_Name__c != trigger.oldMap.get(o.Id).Primary_Contact_Name__c || o.Primary_Contact_Role__c !=trigger.oldMap.get(o.Id).Primary_Contact_Role__c))
        {

            List<OpportunityContactRole> ocrl = [select id, ContactId, Opportunityid, role, isPrimary from OpportunityContactRole where opportunityid =: o.Id];
            system.debug('Queried OCR:'+ocrl);
            
            if(ocrl.size() == 0)
            {
               system.debug('Entered IF');
               OpportunityContactRole new_ocr = new OpportunityContactRole();
               new_ocr.opportunityid = o.Id;
               new_ocr.role = o.Primary_Contact_Role__c;
               new_ocr.ContactId = o.Primary_Contact_Name__c;
               new_ocr.isPrimary = true;
               Insert(new_ocr);
               system.debug('Inserted OCR:'+new_ocr);
            }
            else
            {
                for(OpportunityContactRole oc: ocrl)
                {
                    if(oc.contactId == o.Primary_Contact_Name__c)
                    {
                        i++;
                        oc.isPrimary = true;
                        oc.role = o.Primary_Contact_Role__c;
                        Update oc;
                    } 
                 }
                 if(i==0)
                     j++;

             }
         }   
         if(Trigger.isInsert || j>0)
         { 
            OpportunityContactRole new_ocr = new OpportunityContactRole();
            new_ocr.opportunityid = o.Id;
            new_ocr.role = o.Primary_Contact_Role__c;
            new_ocr.ContactId = o.Primary_Contact_Name__c;
            new_ocr.isPrimary = true;
            Insert(new_ocr);
         }                
    }
}
Best Answer chosen by Amulya
Raj VakatiRaj Vakati
try this
 
@isTest
public class TestCreateContactRole {
    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';
		a.Mission_Statement__c = 'We do lots of things';
		a.Facebook_URL__c = 'www.facebooktest.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'; 
		c.Primary_Membership_Contact__c = TRUE;
		insert c;
		System.debug('created primary contact');
			
		//Then create another non-primary contact
		Contact ci = new Contact();
		ci.FirstName = 'Bob';
		ci.LastName  = 'Test';
		ci.AccountId = a.id;
		ci.MailingStreet = '298 S. Ringo Street';
		ci.MailingCity = 'Little Rock';
		ci.MailingState = 'AR';
		ci.MailingPostalCode = '72201'; 
		ci.Primary_Membership_Contact__c = FALSE;
		insert ci;
		System.debug('created primary contact');
			
		//Now create an opportunity
		Opportunity o = new Opportunity();
		o.RecordType = [SELECT Id, Name, DeveloperName FROM RecordType WHERE Name = 'Membership' LIMIT 1];
		o.Name = 'New Record';
		o.StageName = 'Posted';
	o.CloseDate = Date.today();
		o.Primary_Contact_Name__c = c.Id;
		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');
			
		//Now update the OCR for the non-primary contact
		OpportunityContactRole ocr1 = new OpportunityContactRole();
			
		ocr1.ContactId = ci.Id;
		ocr1.OpportunityId = o.Id;
		ocr1.IsPrimary = FALSE;
		ocr1.Role = 'Decision Maker';
		insert ocr1;
		System.debug('created opportunity contact role for non-primary contact');
		
		
				o.Primary_Contact_Name__c = ci.Id;

		Update o;
					
	}
}

 

All Answers

Raj VakatiRaj Vakati
try this
 
@isTest
public class TestCreateContactRole {
    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';
		a.Mission_Statement__c = 'We do lots of things';
		a.Facebook_URL__c = 'www.facebooktest.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'; 
		c.Primary_Membership_Contact__c = TRUE;
		insert c;
		System.debug('created primary contact');
			
		//Then create another non-primary contact
		Contact ci = new Contact();
		ci.FirstName = 'Bob';
		ci.LastName  = 'Test';
		ci.AccountId = a.id;
		ci.MailingStreet = '298 S. Ringo Street';
		ci.MailingCity = 'Little Rock';
		ci.MailingState = 'AR';
		ci.MailingPostalCode = '72201'; 
		ci.Primary_Membership_Contact__c = FALSE;
		insert ci;
		System.debug('created primary contact');
			
		//Now create an opportunity
		Opportunity o = new Opportunity();
		o.RecordType = [SELECT Id, Name, DeveloperName FROM RecordType WHERE Name = 'Membership' LIMIT 1];
		o.Name = 'New Record';
		o.StageName = 'Posted';
	o.CloseDate = Date.today();
		o.Primary_Contact_Name__c = c.Id;
		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');
			
		//Now update the OCR for the non-primary contact
		OpportunityContactRole ocr1 = new OpportunityContactRole();
			
		ocr1.ContactId = ci.Id;
		ocr1.OpportunityId = o.Id;
		ocr1.IsPrimary = FALSE;
		ocr1.Role = 'Decision Maker';
		insert ocr1;
		System.debug('created opportunity contact role for non-primary contact');
		
		
				o.Primary_Contact_Name__c = ci.Id;

		Update o;
					
	}
}

 
This was selected as the best answer
AmulyaAmulya
@Raj vakati - I had done the same thing except the last 2 lines, didn't update another Contact so it was giving 39% code coverage. Now we moved to 78%. Thank you very much. It's still not covering below code part. We have already added OCR insertion part but still it's not covered. Any idea why? I am trying to understand Test class logics as I am beginner and Org code coverage is at 74% so I need to work on other Test classes.
 OpportunityContactRole new_ocr = new OpportunityContactRole();
               new_ocr.opportunityid = o.Id;
               new_ocr.role = o.Primary_Contact_Role__c;
               new_ocr.ContactId = o.Primary_Contact_Name__c;
               new_ocr.isPrimary = true;
               Insert(new_ocr);
Raj VakatiRaj Vakati
Try this .. you will get 100 %
@isTest
public class TestCreateContactRole {
    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';
		a.Mission_Statement__c = 'We do lots of things';
		a.Facebook_URL__c = 'www.facebooktest.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'; 
		c.Primary_Membership_Contact__c = TRUE;
		insert c;
		System.debug('created primary contact');
			
		//Then create another non-primary contact
		Contact ci = new Contact();
		ci.FirstName = 'Bob';
		ci.LastName  = 'Test';
		ci.AccountId = a.id;
		ci.MailingStreet = '298 S. Ringo Street';
		ci.MailingCity = 'Little Rock';
		ci.MailingState = 'AR';
		ci.MailingPostalCode = '72201'; 
		ci.Primary_Membership_Contact__c = FALSE;
		insert ci;
		System.debug('created primary contact');
			
		//Now create an opportunity
		Opportunity o = new Opportunity();
		o.RecordType = [SELECT Id, Name, DeveloperName FROM RecordType WHERE Name = 'Membership' LIMIT 1];
		o.Name = 'New Record';
		o.StageName = 'Posted';
	o.CloseDate = Date.today();
		o.Primary_Contact_Name__c = c.Id;
		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');
			
		//Now update the OCR for the non-primary contact
		OpportunityContactRole ocr1 = new OpportunityContactRole();
			
		ocr1.ContactId = ci.Id;
		ocr1.OpportunityId = o.Id;
		ocr1.IsPrimary = FALSE;
		ocr1.Role = 'Decision Maker';
		insert ocr1;
		System.debug('created opportunity contact role for non-primary contact');
		
		
				o.Primary_Contact_Name__c = ci.Id;

		Update o;
					
	}
	 static testMethod void createAccount_2()
	{

		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';
		a.Mission_Statement__c = 'We do lots of things';
		a.Facebook_URL__c = 'www.facebooktest.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'; 
		c.Primary_Membership_Contact__c = TRUE;
		insert c;
		System.debug('created primary contact');
			
		//Then create another non-primary contact
		Contact ci = new Contact();
		ci.FirstName = 'Bob';
		ci.LastName  = 'Test';
		ci.AccountId = a.id;
		ci.MailingStreet = '298 S. Ringo Street';
		ci.MailingCity = 'Little Rock';
		ci.MailingState = 'AR';
		ci.MailingPostalCode = '72201'; 
		ci.Primary_Membership_Contact__c = FALSE;
		insert ci;
		System.debug('created primary contact');
			
		//Now create an opportunity
		Opportunity o = new Opportunity();
		o.RecordType = [SELECT Id, Name, DeveloperName FROM RecordType WHERE Name = 'Membership' LIMIT 1];
		o.Name = 'New Record';
		o.StageName = 'Posted';
	o.CloseDate = Date.today();
		o.Primary_Contact_Name__c = c.Id;
		o.Description = 'Test Record';
		insert o;
 			
		 
				o.Primary_Contact_Name__c = ci.Id;

		Update o;
					
	}
}