Skip to content

Instantly share code, notes, and snippets.

@florianhoehn
Created May 16, 2017 09:25
Show Gist options
  • Save florianhoehn/4738624c2e606697ea659d8934f6a103 to your computer and use it in GitHub Desktop.
Save florianhoehn/4738624c2e606697ea659d8934f6a103 to your computer and use it in GitHub Desktop.
AttachmentTriggerHandler
/**
* AttachmentInsertUpdateTriggerMVN
* Created By: Florian Hoehn
* Created On: 15/05/2017
* Description: This is a trigger class that creates ...
*
**/
public class AttachmentInsertUpdateTriggerMVN implements TriggerHandlerMVN.HandlerInterface {
private Map<String, Program_Member_Attachment_Setting_MVN__mdt> programMemberAttachmentSettings {
get {
Map<String, Program_Member_Attachment_Setting_MVN__mdt> programMemberAttachmentSettings = new Map<String, Program_Member_Attachment_Setting_MVN__mdt>();
for(Program_Member_Attachment_Setting_MVN__mdt setting : [SELECT SObject_API_Name_MVN__c, Program_Member_Field_API_Name_MVN__c, Type_MVN__c
FROM Program_Member_Attachment_Setting_MVN__mdt]) {
programMemberAttachmentSettings.put(setting.SObject_API_Name_MVN__c, setting);
}
return programMemberAttachmentSettings;
}
}
public void handle() {
if(Trigger.isInsert || Trigger.isUpdate) {
if(Trigger.isAfter) {
// arranging attachments by parentIds and parentIds by their type
Map<String, Set<Id>> parentIdsByType = new Map<String, Set<Id>>();
Map<Id, List<Attachment>> attachmentsByParentId = new Map<Id, List<Attachment>>();
for (Attachment attachment : (List<Attachment>) Trigger.new) {
List<Attachment> attachmentList = new List<Attachment>();
if(attachmentsByParentId.get(attachment.ParentId) != null) {
attachmentList = attachmentsByParentId.get(attachment.ParentId);
}
attachmentList.add(attachment);
attachmentsByParentId.put(attachment.ParentId, attachmentList);
String parentIdType = attachment.ParentId.getSObjectType().getDescribe().getName();
Set<Id> parentIds = new Set<Id>();
if(parentIdsByType.get(attachment.ParentId) != null) {
parentIds = parentIdsByType.get(parentIdType);
}
parentIds.add(attachment.ParentId);
parentIdsByType.put(parentIdType, parentIds);
}
// create documents by pulling the program member lookup fields from the setting and using a dynamic query to get the content
// ATTENTION:
// SOQL in for loop for each parent type: means each attachment with a different so long as we do not have 100 different parent types in one go it's fine...
List<Program_Member_Document_MVN__c> newProgramMemberDocuments = new List<Program_Member_Document_MVN__c>();
for(String parentIdType : parentIdsByType.keySet()) {
Set<Id> parentIdsToSelect = parentIdsByType.get(parentIdType);
if(programMemberAttachmentSettings.get(parentIdType) != null) {
for(SObject obj : Database.query('SELECT ' + programMemberAttachmentSettings.get(parentIdType).Program_Member_Field_API_Name_MVN__c + ' FROM ' + parentIdType + ' WHERE Id IN: parentIdsToSelect')) {
for(Id parentId : parentIdsToSelect) {
for(Attachment attachment : attachmentsByParentId.get(parentId)) {
newProgramMemberDocuments.add(
new Program_Member_Document_MVN__c(
Attachment_Id__c = attachment.Id,
Title_MVN__c = attachment.Name,
Program_Member_MVN__c = (Id)obj.get(programMemberAttachmentSettings.get(parentIdType).Program_Member_Field_API_Name_MVN__c),
Type_MVN__c = programMemberAttachmentSettings.get(parentIdType).Type_MVN__c
)
);
}
}
}
}
}
upsert newProgramMemberDocuments Attachment_Id__c;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment