+ Start a Discussion
Apex PupperApex Pupper 

[Help!] Primary Contacts - only 21 lines of code, but 61% coverage.

Hello Community,

I've tried to not bother you awesome folks and solve it myself first (with Google's help). But even Google can't get me out of the issue where I'm stuck at. Could you shine some light and help me out? I hope this thread will also help others who work on a similar project. Thanks in advance, and below is the story.

I wrote a trigger for tagging Primary Contacts in each account. The desired final result should look like:
  1. It limits 2 primary contacts per account just for now. I also can adjust the limit later if the management changes their mind.
  2. I can run a report to list the accounts that have 0 primary contacts. So sales team can stay on top of the data maintenance based on that report. Currently, I wrote this feature by updating a customized field (in Account) after one or two contact(s) get tagged as the primary contact.
Here's the code:
trigger primaryContact on Contact (after insert, after update) {
      Set<Id> accountIds = new Set<Id>();
      List<Account> updatedAccounts = new List<Account>();
      for (Contact c : Trigger.new) {
        accountIds.add(c.AccountId);
      }
      Map<Id,Account> accountMap = new Map<Id,Account>([Select Id, Primary_Contact__c, (Select Id, Name from Contacts where IsPrimary__c = true) from Account where Id in :accountIds]);
      for (Contact c : Trigger.new) {
        if (c.IsPrimary__c && accountMap.get(c.AccountId) != null) {
            Account updAccount = accountMap.get(c.AccountId);
            updAccount.Primary_Contact__c = c.id;
            if (updAccount.Contacts != null && updAccount.Contacts.size()>2) {
              c.IsPrimary__c.addError(' ¯`_(ツ)_/¯  Sorry... maximum 2 Primary Contacts allowed per Account.');
            }
            
            updatedAccounts.add(updAccount);
        } 
      }
      
      update updatedAccounts;
}

The trigger works perfectly with the dummy data in my developer account. And all features work as intend. However, when I moved to the sandbox and get it ready for the production, the developer console threw some errors after the "Run All" test (screenshot below). And the Overall Code Coverage is only 61% - need at least 75% to push it to production.

color-coded error area in Developer Console

Another screenshot of those errors in details.
It seems that the new trigger doesn't play well with existing plugins.

Please help. Many thanks again!
Gururaj BGururaj B
Looking at the code coverage details the if condition is not getting satisfied. So check the test code that when a new contact is created are you updating the field IsPrimary__c= true and the contact is associated with an account. 
To have 100% coverage you should have contact created with IsPrimary__c=true and associate account one that is already having more than 2 contacts and another that is not having contacts more than 2 associated.
Hope this helps. If so please mark as best answer.

Thanks
 
Apex PupperApex Pupper
Hi Gururaj, thanks for the prompt reply!

Could you elaborate more on the solution? I'm pretty new to the Apex, and still learning it by doing. How would you change the code itself?

BTW, I know the red-colour errors are highlighted from line 10 to line 16. Still, I'm wondering whether line 7 has issues as well. Maybe it's fine, but I'm not so sure.
Apex PupperApex Pupper
Oh... now I see what you mean, Gururaj. I need to write a customised unit test to test the code.

I'm on a binge-reading mode and trying to learn to write a test, but still very new to it. Can you or anyone show me an example? I can read and understand most parts, but not ready to create from the scratch yet.

Many thanks!
Gururaj BGururaj B
Hi,

Red color on the codes doesnt mean there is any error on those line of codes. It means those lines were not exeucted as part of your test. If you could provide me the test class i can point out what is missing in your test class. It might need update on your test class to have the 100% code coverage.
Gururaj BGururaj B
So you know which test is actulay failing with 61% coverage? If you know the test class, then open it and send the code. i can modifiy to make it cover 100%.