Skip to content

Instantly share code, notes, and snippets.

@Walletau
Walletau / TriggerContext
Created May 24, 2017 16:11
Trigger Context
/**
* This class holds useful information about a trigger event that is taking
* place so that handler classes can make use of this data and don't have to
* pass any individual values.
*/
public class TriggerContext {
public final String objectName;
public final Boolean isBefore;
public class Build {
//------------------------------------------------------------------------------
//Account
public class AccountBuilder {
public AccountBuilder withName(String name) {
this.name = name;
return this;
/*
Copyright 2011 Mavens Consulting, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
@Walletau
Walletau / rhsnip5.cls
Last active April 1, 2017 17:55
RH Snip5
@isTest
public static void testObjectAOwnership() {
Account owningAccount = Build.anAccount()
.withRecordType(Build.AccountRecordType.Account)
.withName('testOwningAccount')
.build();
insert owningAccount;
Account secondOwningAccount = Build.anAccount()
.withRecordType(Build.AccountRecordType.Account)
.withName('testOwningAccount2')
@Walletau
Walletau / rhsnip4.cls
Created April 1, 2017 17:38
RH Snip4
public static void manageSharingObjectA(List<sObject> objectList) {
//Obtain the security configuration map
Map<String, SecurityDictionary__c> securityDictionaryMap = SecurityDictionary__c.getAll();
Set<Id> objectAIdsTargetedToReview = new Set<Id>();
List<ObjectA__Share> objectAShareToInsert = new List<ObjectA__Share>();
List<ObjectA__Share> objectAShareToDelete = new List<ObjectA__Share>();
for (sObject objectAObject : objectList) {
ObjectA__c objectAToReview = (ObjectA__c) objectAObject;
objectAIdsTargetedToReview.add(objectAToReview.Id);
}
@Walletau
Walletau / rhsnip3.cls
Last active April 1, 2017 17:23
RH Snip3
///////////////
//Hierarchy tree sorter
///////////////
//Initialising process to recalculate hierarchy for given accounts
//Any member of the account hierarchy can be identified in order to intiate a complete recalculation
public static void processAccountHierarchyMembership(Set<Id> targetedAccountsIds) {
//Given that the hierarchy code requires the top parent record, we first obtain the master parent records
Set<Id> topIds = getTopAccountIds(targetedAccountsIds);
List<Account> topAccounts = new List<Account>();
@Walletau
Walletau / RHSnip2.cls
Created April 1, 2017 16:59
RH Snip2
//Method consumes two sets of Ids and returns a map of sets
//Primarily used for comparison of existing and new memberships to see what existing records have to be removed
public static Map<String, Set<Id>> compareSets(Set<Id> setOne, Set<Id> setTwo) {
Map<String, Set<Id>> comparisonMap = new Map<String, Set<Id>>{
'In_First_Set' => new Set<Id>(),
'Present_In_Both' => new Set<Id>(),
'In_Second_Set' => new Set<Id>()
};
for (String setOneString : setOne) {
if (setTwo.contains(setOneString))
@Walletau
Walletau / RHsnip1
Last active April 1, 2017 16:56
RH Snip1
public static Boolean GroupCalculationActive = false;
public static List<PublicGroupAccountWrapper> createPublicGroupSet(Set<Id> accountIdsTargeted) {
if (!GroupCalculationActive) {
GroupCalculationActive = true;
List<PublicGroupAccountWrapper> accountsToPopulate = new List<PublicGroupAccountWrapper>();
Id organisationRecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByName().get(ConfigSupport.defaultOrganisationAccountRecordType).getRecordTypeId();
//Given that the hierarchy code requires the top parent record, we first obtain the master parent records
Set<Id> topIds = getTopAccountIds(accountIdsTargeted);
List<Account> topAccounts = new List<Account>();
for (Account acc: [
@Walletau
Walletau / tfe.cls
Last active April 1, 2017 06:22
Test Factory Example
@isTest
private class TestUserUtil {
@isTest static void updateRelatedContactAndAccountRecordsTest() {
// Create a User, Contact and Account
Account newAccount = Build.anAccount()
.withRecordType(Build.AccountRecordType.Organisation)
.build();
insert newAccount;
Contact contactForUser = Build.aContact()
.withName('Test1', 'Test2')
@Walletau
Walletau / mps5.cls
Created March 29, 2017 16:50
MagicParentsSnippet5
Account a = [SELECT Id, Name,
Owner.Manager.Manager.ManagerId
FROM Account
WHERE Id = '001p000000K12dV' LIMIT 1];
System.debug('1: ' + a.OwnerId);
System.debug('2: ' + a.Owner.ManagerId);
System.debug('3: ' + a.Owner.Manager.Manager.Manager.Manager.FirstName);
System.debug('3: ' + a.Owner.Manager.LastName);