Skip to content

Instantly share code, notes, and snippets.

@jeffdonthemic
Created April 10, 2012 13:41
Show Gist options
  • Save jeffdonthemic/2351453 to your computer and use it in GitHub Desktop.
Save jeffdonthemic/2351453 to your computer and use it in GitHub Desktop.
AccountRegionTrigger, Handler and Test class
public with sharing class AccountRegionTriggerHandler {
@future
public static void ProcessRegionsAsync(Set<ID> accountIds){
// holds a map of the account id and comma separated regions to build
Map<Id, String> accountRegionMap = new Map<Id, String>();
// get ALL of the regions for all affected accounts so we can build
List<Account_Region__c> accountRegions = [select id, Account__c,
Region__r.Name from Account_Region__c
where Account__c IN :accountIds order by Region__r.Name];
for (Account_Region__c ar : accountRegions) {
if (!accountRegionMap.containsKey(ar.Account__c)) {
// if the key (account) doesn't exist, add it with region name
accountRegionMap.put(ar.Account__c,ar.Region__r.Name);
} else {
// if the key (account) already exist, add ", region-name"
accountRegionMap.put(ar.Account__c,accountRegionMap.get(ar.Account__c) +
', ' + ar.Region__r.Name);
}
}
// get the account that were affected
List<Account> accounts = [select id from Account where Id IN :accountIds];
// add the comma separated list of regions
for (Account a : accounts)
a.Regions__c = accountRegionMap.get(a.id);
// update the accounts
update accounts;
}
}
trigger AccountRegionTrigger on Account_Region__c (after delete, after insert, after update) {
// fires after both insert and update
if((Trigger.isInsert || Trigger.isUpdate) && Trigger.isAfter){
// find the ids of all accounts that were affected
Set<Id> accountIds = new Set<Id>();
for (Account_Region__c ar : [select Id, Account__c from Account_Region__c
where Id IN :Trigger.newMap.keySet()])
accountIds.add(ar.Account__c);
// process the accounts
AccountRegionTriggerHandler.ProcessRegionsAsync(accountIds);
// fires when records are deleted. may want to do undelete also?
} else if(Trigger.isDelete && Trigger.isAfter){
// find the ids of all accounts that were affected
Set<Id> accountIds = new Set<Id>();
for (ID id : Trigger.oldMap.keySet())
accountIds.add(Trigger.oldMap.get(id).Account__c);
// process the accounts
AccountRegionTriggerHandler.ProcessRegionsAsync(accountIds);
}
}
@isTest
private class Test_AccountRegionTriggerHandler {
static List<Region__c> regions = new List<Region__c>();
static {
// insert some regions
Region__c r1 = new Region__c(name='Region 1');
Region__c r2 = new Region__c(name='Region 2');
Region__c r3 = new Region__c(name='Region 3');
Region__c r4 = new Region__c(name='Region 4');
regions.add(r1);
regions.add(r2);
regions.add(r3);
regions.add(r4);
insert regions;
}
private static void testInsertRecords() {
List<Account> accounts = new List<Account>();
List<Account_Region__c> accountRegions = new List<Account_Region__c>();
// insert some accounts
Account a1 = new Account(name='Account 1');
Account a2 = new Account(name='Account 2');
accounts.add(a1);
accounts.add(a2);
insert accounts;
Test.startTest();
accountRegions.add(new Account_Region__c(Account__c=a1.Id, Region__c=regions.get(0).Id));
accountRegions.add(new Account_Region__c(Account__c=a1.Id, Region__c=regions.get(1).Id));
accountRegions.add(new Account_Region__c(Account__c=a2.Id, Region__c=regions.get(2).Id));
accountRegions.add(new Account_Region__c(Account__c=a2.Id, Region__c=regions.get(3).Id));
insert accountRegions;
Test.stopTest();
// since async, check for the accounts AFTER tests stop
List<Account> updatedAccounts = [select id, name, regions__c from account where id IN :accounts];
System.assertEquals('Region 1, Region 3',updatedAccounts.get(0).Regions__c);
System.assertEquals('Region 2, Region 4',updatedAccounts.get(1).Regions__c);
}
private static void testDeleteRecords() {
List<Account> accounts = new List<Account>();
List<Account_Region__c> accountRegions = new List<Account_Region__c>();
// insert an account
Account a1 = new Account(name='Account 1');
accounts.add(a1);
insert accounts;
Test.startTest();
accountRegions.add(new Account_Region__c(Account__c=a1.Id, Region__c=regions.get(0).Id));
accountRegions.add(new Account_Region__c(Account__c=a1.Id, Region__c=regions.get(1).Id));
accountRegions.add(new Account_Region__c(Account__c=a1.Id, Region__c=regions.get(2).Id));
accountRegions.add(new Account_Region__c(Account__c=a1.Id, Region__c=regions.get(3).Id));
insert accountRegions;
// now delete a record
delete accountRegions.get(3);
Test.stopTest();
List<Account> updatedAccounts = [select id, name, regions__c from account where id IN :accounts];
System.assertEquals('Region 1, Region 2, Region 3',updatedAccounts.get(0).Regions__c);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment