Skip to content

Instantly share code, notes, and snippets.

/**
* Created by Nick Likane on 30/12/2016.
*/
public without sharing class SecurityUtil {
// Default up down map
public static Boolean GroupCalculationActive = false;
//////////
//Managing of specific share records
//////////
public static Set<Id> getTopAccountIds(Set<Id> accountsTargeted) {
Set<Id> topAccountIds = new Set<Id>();
Set<Id> recursivelyTargetedAccountIds = new Set<Id>();
for (Account targetedAccount : [
SELECT Id,
Name,
ChildrenUserGroupId__c,
ChildrenIncludedUserGroupId__c,
UserGroupId__c,
ParentUserGroupId__c,
@Walletau
Walletau / mps1.cls
Last active March 29, 2017 16:28
MagicParentsSnippet1
public static Set<Id> getTopAccountIds(Set<Id> accountsTargeted) {
Set<Id> topAccountIds = new Set<Id>();
Set<Id> recursivelyTargetedAccountIds = new Set<Id>();
for (Account targetedAccount : [
SELECT Id,
Name,
ChildrenUserGroupId__c,
ChildrenIncludedUserGroupId__c,
UserGroupId__c,
ParentUserGroupId__c,
@Walletau
Walletau / mps2.cls
Created March 29, 2017 16:45
MagicParentsSnippet2
Account a = [SELECT Id, Name,
ParentID,
Parent.ParentId,
Parent.Parent.ParentId
FROM Account
WHERE Id = '0013600000NHSlW' LIMIT 1];
System.debug('1: ' + a.ParentId);
System.debug('2: ' + a.Parent.ParentId);
System.debug('3: ' + a.Parent.Parent.ParentId);
@Walletau
Walletau / mps3.cls
Created March 29, 2017 16:48
MagicParentsSnippet3
Account a = [SELECT Id, Name,
ParentID,
Parent.ParentId
FROM Account
WHERE Id = '001p000000K12dV' LIMIT 1];
@Walletau
Walletau / mps4.cls
Created March 29, 2017 16:49
MagicParentsSnippet4
Account a = [SELECT Id, Name,
Parent.Parent.Parent.ParentId
FROM Account
WHERE Id = '001p000000K12dV' LIMIT 1];
@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);
@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);
@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 / 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: [