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
Jack InsJack Ins 

Test Coverage Problems

Hello All,

I have been able to write a trigger with "bulkification" but I am having problems understanding how to write the test class to cover all lines.

 

Here is the code that I am trying to cover.

 

trigger ContactOpportunityAssoc on Contact (after insert) {
set<String> custnmbr = new set<String>();
		for (Contact c: Trigger.new){
            if (c.cnt_plcsc_CustNmbr_ExtID__c != null){custnmbr.add(c.cnt_plcsc_Customer_Number__c);}
        }
        
Map<String,Id> mapOpps = new Map<String,Id>();
/*List all Contacts that have the customer number the same as the loaded custnmbr set*/      
		for(Opportunity opp: [Select Id, Customer_Number__c 
							From Opportunity 
							Where Customer_Number__c IN: custnmbr]){mapOpps.put(opp.Customer_Number__c , opp.Id);}

set<String> oppids = new set<String>();
		for(Opportunity opp2: [Select Id, Customer_Number__c 
								From Opportunity 
								Where Customer_Number__c IN: custnmbr]){oppids.add(opp2.Id);}

Map<String,Id> mapCnts = new Map<String,Id>();
/*List all Contacts that have the customer number the same as the loaded custnmbr set*/      
		for(Contact cnt: Trigger.new){mapCnts.put(cnt.cnt_plcsc_Customer_Number__c , cnt.Id);}
		
/*Loop through the main list of Cnts*/
List<OpportunityContactRole> ocr = new List<OpportunityContactRole>();
List<Opportunity> oppsList = [Select Id, Customer_Number__c From Opportunity Where Customer_Number__c In: custnmbr];

			for(Opportunity o2: oppsList){
				if(mapOpps.containsKey(o2.Customer_Number__c)){
						OpportunityContactRole ocr2 = new OpportunityContactRole();
						ocr2.ContactId = mapCnts.get(o2.Customer_Number__c);
						ocr2.OpportunityId = o2.Id;
						ocr2.IsPrimary = true;
						ocr2.Role = 'Primary Insured';
						ocr.add(ocr2);
				}
			}
/*Insert the new list of OCR's*/
	if(!ocr.isEmpty()){
		insert ocr;}
}

 Here is my sad attempt at code coverage:

Contact cntExisting = new Contact(LastName = 'Tester');
			cntExisting.FirstName = 'Test';
			cntExisting.RecordTypeId = '01240000000DVfaAAG';
			cntExisting.Email='test@hanover.com';
			cntExisting.cnt_plcsc_CustNmbr_ExtID__c = '123456789INS01';
			cntExisting.cnt_plcsc_Customer_Number__c='123456789';
			cntExisting.MailingPostalCode = '01453';
			String cntId = cntExisting.Id;
		insert cntExisting;
		
		cntExisting = [Select Id, Name, cnt_plcsc_Customer_Number__c from Contact Where Id=:cntExisting.Id];
    	System.assertEquals('123456789', cntExisting.cnt_plcsc_Customer_Number__c);
    	
    	Map<String,Id> mapCntpRecs = new Map<String,Id>();
    		for(Contact mapcnts:[Select Id, cnt_plcsc_Customer_Number__c from Contact Where cnt_plcsc_Customer_Number__c=:cntExisting.cnt_plcsc_Customer_Number__c]){
    			mapCntpRecs.put(mapcnts.cnt_plcsc_Customer_Number__c, mapcnts.Id);}
    		
    	Contact cntExisting2 = new Contact(LastName = '2Tester');
			cntExisting2.FirstName = 'Test';
			cntExisting2.RecordTypeId = '01240000000DVfaAAG';
			cntExisting2.Email='test2@hanover.com';
			cntExisting2.cnt_plcsc_CustNmbr_ExtID__c = '223456789INS01';
			cntExisting2.cnt_plcsc_Customer_Number__c='223456789';
			cntExisting2.MailingPostalCode = '01453';
			String cntId2 = cntExisting2.Id;
    	insert cntExisting2;
    	
    	Opportunity otherPol1 = new Opportunity(Name = '1Other Pol Name');
				otherPol1.customer_number__c = '1234abcd';
				otherPol1.Policy_Id__c = '1XX111111';
				otherPol1.LOB__c = 'Auto';
				otherPol1.stageName = 'Qualified';
				otherPol1.CloseDate = Date.today();
				otherPol1.AccountId = otherAct.Id;
				otherPol1.X1st_Nmd_Insd__c = 'Joe Test1';
				otherPol1.Zip__c = '01453';
				otherPol1.Consumer_Email__c = '1test@hanover.com';
				otherPol1.opp_plcsc_Integration_ID__c = 'A1XX111111';
				insert otherPol1;
    	System.assertEquals('A1XX111111', otherPol1.opp_plcsc_Integration_ID__c);
    	
    	Opportunity otherPol = new Opportunity(Name = 'Other Pol Name');
				otherPol.customer_number__c = '1234abcd';
				otherPol.Policy_Id__c = 'XXX111111';
				otherPol.LOB__c = 'Other';
				otherPol.stageName = 'Qualified';
				otherPol.CloseDate = Date.today();
				otherPol.AccountId = otherAct.Id;
				otherPol.X1st_Nmd_Insd__c = 'Joe Test';
				otherPol.Zip__c = '01453';
				otherPol.Consumer_Email__c = 'test@hanover.com';
		insert otherPol;
						
		Map<String,Id> mapOpps = new Map<String,Id>();
    	for(Opportunity opp: [Select Id, Customer_Number__c From Opportunity Where Customer_Number__c =: otherPol1.Customer_Number__c]) 
    	{mapOpps.put(opp.Customer_Number__c,opp.Id);}
    	
    	set<String> oppids = new set<String>(); 
		for(Opportunity opp2: [Select Id, Customer_Number__c From Opportunity 
			Where Customer_Number__c =: otherPol.Id]){oppids.add(opp2.Id);} 
			
    	OpportunityContactRole ocrInsert = new OpportunityContactRole(OpportunityId = otherPol1.Id, ContactId = cntExisting2.Id);
    			ocrInsert.IsPrimary = true;
    			ocrInsert.Role = 'Primary Insured';
    			insert ocrInsert;
    	
    	Map<String,Id> mapOppRecs = new Map<String,Id>();
    		for(OpportunityContactRole mapoppscntr:[Select Id, ContactId, OpportunityId from OpportunityContactRole Where OpportunityId=:otherPol1.Id]){
    			mapOppRecs.put(mapoppscntr.OpportunityId, mapoppscntr.Id);} 
    						     
		List<OpportunityContactRole> ocrs=new List<OpportunityContactRole>();
		List<Opportunity> oppsList = [Select Id, Customer_Number__c From Opportunity Where Id In: oppids];
		for(Opportunity o2:oppsList){
			if(mapOppRecs.containsKey(o2.Customer_Number__c)){
				OpportunityContactRole ocr2 = new OpportunityContactRole(OpportunityId = otherPol.Id, ContactId = cntExisting2.Id);
		        		ocr2.Role = 'Primary Insured';
		        		ocr2.IsPrimary = false;
		        		ocrs.add(ocr2);
		}}
		insert ocrs;

 Any help would be greatly appreciated.  :)

 

Best Answer chosen by Admin (Salesforce Developers) 
Damien_Damien_

O yea, insert the Opportunities before you insert the Contacts.

All Answers

Damien_Damien_

Your trigger code looked like it did a lot of unnecessary checks because many of your queries were built with the exact same WHERE statements.  I changed it to what i believe would give the exact same result.  (Always the high chance I am wrong but wouldn't hurt to try it.)

 

trigger ContactOpportunityAssoc on Contact (after insert) {
	set<String> custnmbr = new set<String>();
	Map<String,Id> mapCnts = new Map<String,Id>();
	for (Contact c: Trigger.new){
		if (c.cnt_plcsc_CustNmbr_ExtID__c != null)
		{
			custnmbr.add(c.cnt_plcsc_Customer_Number__c);
		}
		/*List all Contacts that have the customer number the same as the loaded custnmbr set*/ 
		mapCnts.put(cnt.cnt_plcsc_Customer_Number__c , cnt.Id);
    }
    
	List<OpportunityContactRole> ocr = new List<OpportunityContactRole>();
	/*List all Contacts that have the customer number the same as the loaded custnmbr set*/
	for(Opportunity opp: [Select Id, Customer_Number__c 
						From Opportunity 
						Where Customer_Number__c IN: custnmbr]){
		
		OpportunityContactRole ocr2 = new OpportunityContactRole();
		ocr2.ContactId = mapCnts.get(o2.Customer_Number__c);
		ocr2.OpportunityId = o2.Id;
		ocr2.IsPrimary = true;
		ocr2.Role = 'Primary Insured';
		ocr.add(ocr2);
		
	}
	
	/*Insert the new list of OCR's*/
	if(!ocr.isEmpty())
	{insert ocr;}
}

 

From here, it would be useful to know what specific lines weren't covered.  I have a feeling that some of what are you missing is from the code being a little more complicted than it needs to.

 

 

Jack InsJack Ins

Thank you very much on the code help.  It does work great and simplifies things. 

 

My code coverage is missing lines 19 through 24 and line 30.

Seems that I am not covering the for loop and the insert of the OCR.

Damien_Damien_

I can't tell for sure what those lines of code are, but it looks to me like you need to use the correct Customer number for your opportunities.  Change

 

customer_number__c = '1234abcd';

to

 

customer_number__c = '123456789';

In the trigger your opps are looking for a matching customer number to the Contact.  It isn't finding one because you are using different numbers.

Jack InsJack Ins

I changed the value in the customer number but the lines are still not being covered.

 

Thoughts?

Damien_Damien_

O yea, insert the Opportunities before you insert the Contacts.

This was selected as the best answer
Jack InsJack Ins

OMG  how simple was that.  Since I have 2 triggers one from Contact to Opportunity Association and one from Opportunity to Contact Association I needed to do both.  Insert contact then oops and reverse opps then contacts.

 

Thank you so much for your help.  This was a great learning experience for me.

Your the best.  :)