You need to sign in to do that
Don't have an account?
Apex 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:
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.
Another screenshot of those errors in details.
Please help. Many thanks again!
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:
- It limits 2 primary contacts per account just for now. I also can adjust the limit later if the management changes their mind.
- 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.
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.
Another screenshot of those errors in details.
Please help. Many thanks again!
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
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.
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!
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.