Created
September 5, 2014 15:54
-
-
Save james-gibson/fec3c348a1cf35f75a45 to your computer and use it in GitHub Desktop.
Contact summary trigger w/test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| trigger SummarizeAccountContacts on Contact (after delete, after insert) { | |
| set<ID> accountIds = new set<ID>(); | |
| set<ID> contactIds = new Set<ID>(); | |
| public void addValidAccountId(Id i) | |
| { | |
| if(i !=null) | |
| accountIds.add(i); | |
| } | |
| if(trigger.isInsert){ | |
| for(Contact contact : trigger.new){ | |
| addValidAccountId(contact.AccountId); | |
| } | |
| } | |
| if(trigger.isDelete){ | |
| for(Contact contact : trigger.old){ | |
| addValidAccountId(contact.AccountId); | |
| } | |
| } | |
| //Validate existence of accounts to update | |
| if(accountIds == Null) | |
| return; //This is the only untested line, I'm not sure how to trigger a trigger-failure | |
| List<AggregateResult> contactsByAccount = [ | |
| select AccountId, | |
| count(ID) numContacts | |
| from Contact | |
| where AccountId in : accountIds Group By AccountId | |
| ]; | |
| Map<Id,Integer> contactCountMap = new Map<Id,Integer>(); | |
| //Convert list results to a map | |
| for (AggregateResult contactCount : contactsByAccount) { | |
| contactCountMap.put( | |
| string.valueOf(contactCount.get('AccountId')) | |
| , Integer.valueOf(contactCount.get('numContacts')) | |
| ); | |
| } | |
| List<Account> updateAccounts = new List<Account>(); | |
| //Create temporary accounts to update Num_Contacts__c | |
| for(Id accountId : accountIds ){ | |
| Account tmpAccount = new Account(id=accountId); | |
| tmpAccount.Num_Contacts__c = contactCountMap.get(accountId); | |
| updateAccounts.add(tmpAccount); | |
| } | |
| update updateAccounts; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @isTest | |
| public class TestSummarizeAccountContacts { | |
| public static testMethod void validateContactCountOnAddition() { | |
| Account accountPlaceHolder = new Account(); | |
| Account tmpAccount = new Account( | |
| Name='Acme' | |
| ); | |
| insert tmpAccount; | |
| System.assertEquals(tmpAccount.Num_Contacts__c, null); | |
| Contact tmpContact = new Contact( | |
| LastName='Evans', | |
| FirstName='Bob', | |
| AccountID= tmpAccount.ID | |
| ); | |
| insert tmpContact; | |
| accountPlaceHolder = [ | |
| SELECT | |
| ID | |
| , Num_Contacts__c | |
| FROM Account | |
| WHERE ID = :tmpAccount.id | |
| ]; | |
| System.assertEquals(accountPlaceHolder.Num_Contacts__c,1); | |
| } | |
| public static testMethod void validateContactCountOnSubtraction() { | |
| Account accountPlaceHolder1 = new Account(); | |
| Account tmpAccount1 = new Account( | |
| Name='Wyane Industries' | |
| ); | |
| insert tmpAccount1; | |
| System.assertEquals(tmpAccount1.Num_Contacts__c, null); | |
| Contact tmpContact1 = new Contact( | |
| LastName='Evans', | |
| FirstName='Bob', | |
| AccountID= tmpAccount1.ID | |
| ); | |
| Contact tmpContact2 = new Contact( | |
| LastName='Wyane', | |
| FirstName='Bruce', | |
| AccountID= tmpAccount1.ID | |
| ); | |
| System.assertEquals(tmpAccount1.Num_Contacts__c, null); | |
| insert tmpContact1; | |
| insert tmpContact2; | |
| accountPlaceHolder1 = [ | |
| SELECT | |
| ID | |
| , Num_Contacts__c | |
| FROM Account | |
| WHERE ID = :tmpAccount1.id | |
| ]; | |
| System.assertEquals(accountPlaceHolder1.Num_Contacts__c,2); | |
| delete tmpContact2; | |
| accountPlaceHolder1 = [ | |
| SELECT | |
| ID | |
| , Num_Contacts__c | |
| FROM Account | |
| WHERE ID = :tmpAccount1.id | |
| ]; | |
| System.assertEquals(accountPlaceHolder1.Num_Contacts__c,1); | |
| } | |
| public static testMethod void validateContactCountNonRelatedAccount() { | |
| Account accountPlaceHolder = new Account(); | |
| Account tmpAccount = new Account( | |
| Name='Acme' | |
| ); | |
| Account tmpAccount2 = new Account( | |
| Name='Wyane Industries' | |
| ); | |
| insert tmpAccount; | |
| System.assertEquals(tmpAccount.Num_Contacts__c, null); | |
| System.assertEquals(tmpAccount2.Num_Contacts__c, null); | |
| Contact tmpContact = new Contact( | |
| LastName='Evans', | |
| FirstName='Bob', | |
| AccountID= tmpAccount.ID | |
| ); | |
| insert tmpContact; | |
| accountPlaceHolder = [ | |
| SELECT | |
| ID | |
| , Num_Contacts__c | |
| FROM Account | |
| WHERE ID = :tmpAccount.id | |
| ]; | |
| System.assertEquals(accountPlaceHolder.Num_Contacts__c,1); | |
| //Not exactly sure how to trigger a trigger-failure, this is my best guess | |
| System.assertEquals(tmpAccount2.Num_Contacts__c,null); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment