Skip to content

Instantly share code, notes, and snippets.

@Walletau
Created April 1, 2017 17:38
Show Gist options
  • Save Walletau/5511e05aa7e59983857b346fce6fe039 to your computer and use it in GitHub Desktop.
Save Walletau/5511e05aa7e59983857b346fce6fe039 to your computer and use it in GitHub Desktop.
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);
}
//query objectA with account details
Map<Id, ObjectA__c> objectAsToReview = new Map<Id, ObjectA__c>([
SELECT
Id,
Account__c,
Account__r.ChildrenPublicGroupId__c,
Account__r.PublicGroupId__c,
Account__r.ParentPublicGroupId__c
FROM ObjectA__c
WHERE Id IN :objectAIdsTargetedToReview
]);
Map<Id, List<ObjectA__Share>> objectAIdToObjectAShare = new Map<Id, List<ObjectA__Share>>();
List<ObjectA__Share> objectASharesToReview = [
SELECT Id,
UserOrGroupId,
ParentId,
AccessLevel
FROM
ObjectA__Share
WHERE RowCause = :Schema.ObjectA__Share.rowCause.ExternalShareReason__c
AND ParentId IN :objectAIdsTargetedToReview
];
for (ObjectA__Share objectAShare : objectASharesToReview) {
if (!objectAIdToObjectAShare.containsKey(objectAShare.ParentId)) {
objectAIdToObjectAShare.put(objectAShare.ParentId, new List<ObjectA__Share>{
objectAShare
});
} else {
objectAIdToObjectAShare.get(objectAShare.ParentId).add(objectAShare);
}
}
for (Id objectAId : objectAsToReview.keySet()) {
ObjectA__c objectAToReview = objectAsToReview.get(objectAId);
List<ObjectA__Share> relatedObjectAShareRecords = new List<ObjectA__Share>();
if (objectAIdToObjectAShare.containsKey(objectAId))
relatedObjectAShareRecords = objectAIdToObjectAShare.get(objectAId);
Map<String, ObjectA__Share> groupsPresent = new Map<String, ObjectA__Share>();
for (ObjectA__Share objectASharePresent: relatedObjectAShareRecords) {
groupsPresent.put(objectASharePresent.UserOrGroupId + objectASharePresent.AccessLevel, objectASharePresent);
}
Set<Id> objectAShareSuitable = new Set<Id>();
if (objectAToReview.Account__c != null && securityDictionaryMap.containsKey('ObjectA')) {
if (objectAToReview.Account__r.ParentPublicGroupId__c != null &&
objectAToReview.Account__r.ChildrenPublicGroupId__c != null &&
objectAToReview.Account__r.PublicGroupId__c != null) {
String accessLevelParentPublicGroup = securityDictionaryMap.get('ObjectA').ParentAccess__c;
if (groupsPresent.containsKey(objectAToReview.Account__r.ParentPublicGroupId__c + accessLevelParentPublicGroup)) {
objectAShareSuitable.add(groupsPresent.get(objectAToReview.Account__r.ParentPublicGroupId__c + accessLevelParentPublicGroup).Id);
} else if (accessLevelParentPublicGroup != 'None') {
objectAShareToInsert.add(new ObjectA__Share(ParentId = objectAId, UserOrGroupId = objectAToReview.Account__r.ParentPublicGroupId__c, RowCause = Schema.ObjectA__Share.rowCause.ExternalShareReason__c, AccessLevel = accessLevelParentPublicGroup));
}
String accessLevelChildPublicGroup = securityDictionaryMap.get('ObjectA').ChildAccess__c;
if (groupsPresent.containsKey(objectAToReview.Account__r.ChildrenPublicGroupId__c + accessLevelChildPublicGroup)) {
objectAShareSuitable.add(groupsPresent.get(objectAToReview.Account__r.ChildrenPublicGroupId__c + accessLevelChildPublicGroup).Id);
} else if (accessLevelChildPublicGroup != 'None') {
objectAShareToInsert.add(new ObjectA__Share(ParentId = objectAId, UserOrGroupId = objectAToReview.Account__r.ChildrenPublicGroupId__c, RowCause = Schema.ObjectA__Share.rowCause.ExternalShareReason__c, AccessLevel = accessLevelChildPublicGroup));
}
String accessLevelPrimaryPublicGroup = securityDictionaryMap.get('ObjectA').PrimaryAccess__c;
if (groupsPresent.containsKey(objectAToReview.Account__r.PublicGroupId__c + accessLevelPrimaryPublicGroup)) {
objectAShareSuitable.add(groupsPresent.get(objectAToReview.Account__r.PublicGroupId__c + accessLevelPrimaryPublicGroup).Id);
} else if (accessLevelPrimaryPublicGroup != 'None') {
objectAShareToInsert.add(new ObjectA__Share(ParentId = objectAId, UserOrGroupId = objectAToReview.Account__r.PublicGroupId__c, RowCause = Schema.ObjectA__Share.rowCause.ExternalShareReason__c, AccessLevel = accessLevelPrimaryPublicGroup));
}
}
}
for (ObjectA__Share objectASharePresent: relatedObjectAShareRecords) {
if (!objectAShareSuitable.contains(objectASharePresent.Id)) {
objectAShareToDelete.add(objectASharePresent);
}
}
}
delete objectAShareToDelete;
insert objectAShareToInsert;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment