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
Marmot74Marmot74 

Test Class for Trigger

hey Guys,

 

I've got a simple Trigger and a Test Class but for some reason I am only getting 77% test coverage. I am trying to understand why. It complains that lines 15 & 16 are not covered. Can someone explain why? I marked them below.

 

Heres the trigger.

trigger OnEFO_CreateContact on Contact (before insert, before update) { Set<String> conOrg_IDs = new Set<String>(); for(Contact Con : trigger.new){ if(Con.Org_ID__c != null && Con.AccountID == null){ conOrg_IDs.add(Con.Org_ID__c); } } List<Account> Accounts = [SELECT Org_ID__c, ID FROM Account WHERE Org_ID__c IN :conOrg_IDs]; for(Contact Con : trigger.new){ for(Account a : Accounts){ if(Con.Org_ID__c == a.Org_ID__c){ //Line 15 Con.AccountID = a.ID; //Line 16 } } } }

 

 

Heres the Test Class.

 

@isTest private class ContactTestClass { static testMethod void TestContact() { String checkid; Contact contact = new Contact(); contact.FirstName = 'Jane'; contact.LastName = 'Smith'; contact.Org_ID__c = '12345'; try{ insert contact; checkid=contact.id; }catch(DMLException e){ system.assert(false,'ERROR INSERTING CONTACTS:' + e.getDMLMessage(0)); } Contact upcontact = [SELECT AccountID FROM Contact WHERE Id = :checkid]; upcontact.FirstName = 'John'; upcontact.Org_ID__c = '12345'; try{ update upcontact; }catch(DMLException e){ system.assert(false,'ERROR UPDATING CONTACTS:' + e.getDMLMessage(0)); } } }