• Apex Pupper
  • NEWBIE
  • 0 Points
  • Member since 2018

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 2
    Replies
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!
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!