Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Protect2 {
- //method to process regular attachments
- //+will create separate constructors for chatter FeedItem attachments
- //+will split this method into multiple methods to avoid duplicating code
- public static void ProcessAttachments (List<Attachment> incomingList) {
- //create list of IDs to store all parentIDs for the incoming attachments
- List<id> protectedIds = new List <Id>();
- FOR (Attachment ilatt : incomingList){
- protectedIds.add(ilatt.ParentId);
- }
- //bind query to list of parentIDs
- //this will ensure that we will never hit the 50,000 limit on SOQL query results
- List<ProcessInstance> approvedProcesses = new List<ProcessInstance> ([SELECT TargetObjectId
- FROM ProcessInstance where Status = 'Approved' AND TargetObject.Type = 'Vendor_Invoice__c' AND TargetObjectId IN : protectedIds]);
- //create a set of parentIDs to evaluate against using set contains method
- Set<Id> protectedRecords = new Set<Id>();
- FOR(ProcessInstance pi : approvedProcesses){
- protectedRecords.add(pi.TargetObjectId);
- }
- //iterate over the list of incoming attachments to see if the attachments ParentID is in the list of Protected Records
- //still evaluating against the full query
- List<Attachment> protectedAttachments = new List<Attachment>();
- FOR (Attachment attach : incominglist){
- IF(protectedRecords.contains(attach.ParentId)){
- protectedAttachments.add(attach);
- }
- }
- //call killSwitch on protected attachments if size > 0
- IF(protectedAttachments.size()>0){
- attachmentKillSwitch(protectedAttachments);
- }
- }
- public static void processFeedItems(List<FeedItem> incomingList){
- //add code to process feed items
- List<id> protectedIds = new List <Id>();
- FOR (FeedItem ilatt : incomingList){
- protectedIds.add(ilatt.ParentId);
- }
- //bind query to list of parentIDs
- //this will ensure that we will never hit the 50,000 limit on SOQL query results
- List<ProcessInstance> approvedProcesses = new List<ProcessInstance> ([SELECT TargetObjectId
- FROM ProcessInstance where Status = 'Approved' AND TargetObject.Type = 'Vendor_Invoice__c' AND TargetObjectId IN : protectedIds]);
- //create a set of parentIDs to evaluate against using set contains method
- Set<Id> protectedRecords = new Set<Id>();
- FOR(ProcessInstance pi : approvedProcesses){
- protectedRecords.add(pi.TargetObjectId);
- }
- //iterate over the list of incoming attachments to see if the attachments ParentID is in the list of Protected Records
- //still evaluating against the full query
- List<FeedItem> protectedAttachments = new List<FeedItem>();
- FOR (FeedItem attach : incominglist){
- IF(protectedRecords.contains(attach.ParentId)){
- protectedAttachments.add(attach);
- }
- }
- //call killSwitch on protected attachments if size > 0
- IF(protectedAttachments.size()>0){
- attachmentKillSwitch(protectedAttachments);
- }
- }
- public static void processContentDocument (List<ContentDocument> incomingList)
- {
- /*
- //add code to process feed items
- List<id> protectedIds = new List <Id>();
- FOR (ContentDocument ilatt : incomingList){
- protectedIds.add(ilatt.ParentId);
- }
- //bind query to list of parentIDs
- //this will ensure that we will never hit the 50,000 limit on SOQL query results
- List<ProcessInstance> approvedProcesses = new List<ProcessInstance> ([SELECT TargetObjectId
- FROM ProcessInstance where Status = 'Approved' AND TargetObject.Type = 'Vendor_Invoice__c' AND TargetObjectId IN : protectedIds]);
- //create a set of parentIDs to evaluate against using set contains method
- Set<Id> protectedRecords = new Set<Id>();
- FOR(ProcessInstance pi : approvedProcesses){
- protectedRecords.add(pi.TargetObjectId);
- }
- //iterate over the list of incoming contentDocuments to see if the ParentID is in the list of Protected Records
- List<FeedItem> protectedAttachments = new List<FeedItem>();
- FOR (FeedItem attach : incominglist){
- IF(protectedRecords.contains(attach.ParentId)){
- protectedAttachments.add(attach);
- }
- }
- //call killSwitch on protected attachments if size > 0
- IF(protectedAttachments.size()>0){
- attachmentKillSwitch(protectedAttachments);
- }
- }
- */
- }
- //Kill switch method adds error for all attachments that come to it
- //dont know if having this as a static method makes sense?
- public static void attachmentKillSwitch (List<Attachment> violators) {
- User_Controls__c CS = User_Controls__c.getInstance();
- FOR (attachment vAtt : violators){
- system.debug('attachment on approved object - Id: '+vatt.ParentId);
- IF(!CS.Attachments_Allow_Delete__c){
- vAtt.addError('bogus - deleting attachments on approved records is not allowed');
- }
- }
- }
- public static void attachmentKillSwitch (List<FeedItem> violators) {
- User_Controls__c CS = User_Controls__c.getInstance();
- FOR (FeedItem vAtt : violators){
- system.debug('FeedItem attachment on approved object - Id: '+vatt.ParentId);
- IF(!CS.Attachments_Allow_Delete__c){
- vAtt.addError('bogus - deleting attachments on approved records is not allowed');
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement