function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
XIOXIO 

Assistance with FeedItem Apex Trigger

Hello! 

We have the trigger below that if an attachment is deleted from Chatter Feed, certain profiles will recieve the error message. I need assistance in updating the trigger to only fire on a certain object (Documents_Manager__c). Currently the trigger is firing on all objects. The trigger is also currently blocking the System Administrator profile for testing testing purposes but we would like to add serveral profiles to the block list as well. Thank you in advance!
trigger ChatterDeleteBlocker on FeedItem (After Delete) {
 User u = [SELECT id,ProfileId,Profile.Name  FROM User WHERE id = :UserInfo.getUserId()];
        for(FeedItem fi : trigger.old)
        if(fi.type == 'ContentPost' && u.Profile.Name == 'System Administrator')
            fi.addError('You do not have the permissions to delete files from chatter feed');
}
Best Answer chosen by XIO
Akhil AnilAkhil Anil
Update your code to this
 
trigger ChatterDeleteBlocker on FeedItem (After Delete) {
 User u = [SELECT id, Name, ProfileId,Profile.Name  FROM User WHERE id = :UserInfo.getUserId()];
 for(FeedItem fi : trigger.old) {
      
      Document_Manager__c dm = new Document_Manager__c();
      
      try {
         dm = [Select Id from Document_Manager__c where Id =:fi.ParentId];
      }
      catch(Exception e) {
         dm = null;
      }
     
      if (dm != null) {
        if(fi.type == 'ContentPost' && (u.Profile.Name != 'System Administrator' || u.Name != 'Bryan Girkins'
           || u.Name != 'Mary Loy' || u.Name != 'Judit Szilagyi' || u.Name != 'Sarah Swanson'|| u.Name != 'Jeremy DalleTezze')) {
            fi.addError('You do not have the permissions to delete files from chatter feed');
        }
      }
 }
}

 

All Answers

Akhil AnilAkhil Anil
Hi Harmens,

You need to modify your code like this. It will ensure that the validation runs only on the Documents Manager object. Also, include the other profile names as seen in the code below.
 
trigger ChatterDeleteBlocker on FeedItem (After Delete) {
 User u = [SELECT id,ProfileId,Profile.Name  FROM User WHERE id = :UserInfo.getUserId()];
 for(FeedItem fi : trigger.old) {
      
	  Documents_Manager__c dm = new Documents_Manager__c();
	  
	  try {
	     dm = [Select Id from Documents_Manager__c where Id =:fi.ParentId];
	  }
	  catch(Exception e) {
	     dm = null;
	  }
	 
	  if (dm != null) {
        if(fi.type == 'ContentPost' && (u.Profile.Name == 'System Administrator' || u.Profile.Name == 'Profile name 2 here'
		   || u.Profile.Name == 'Profile name 3 here')) {
            fi.addError('You do not have the permissions to delete files from chatter feed');
		}
	  }
 }
}

Kindly mark it as an answer if that resolves your query !
XIOXIO
Hi Akhil,

Thank you for the quick response! I tried your code but users not included in the IF statement are receiving the error below:

User-added image

I also updated the code a bit to not equal (!=) certain profiles and users instead of blocking certain profiles. See below:
trigger ChatterDeleteBlocker on FeedItem (After Delete) {
 User u = [SELECT id,ProfileId,Profile.Name  FROM User WHERE id = :UserInfo.getUserId()];
 for(FeedItem fi : trigger.old) {
      
      Document_Manager__c dm = new Document_Manager__c();
      
      try {
         dm = [Select Id from Document_Manager__c where Id =:fi.ParentId];
      }
      catch(Exception e) {
         dm = null;
      }
     
      if (dm != null) {
        if(fi.type == 'ContentPost' && (u.Profile.Name != 'System Administrator' || u.Name != 'Bryan Girkins'
           || u.Name != 'Mary Loy' || u.Name != 'Judit Szilagyi' || u.Name != 'Sarah Swanson'|| u.Name != 'Jeremy DalleTezze')) {
            fi.addError('You do not have the permissions to delete files from chatter feed');
        }
      }
 }
}


 
Akhil AnilAkhil Anil
Update your code to this
 
trigger ChatterDeleteBlocker on FeedItem (After Delete) {
 User u = [SELECT id, Name, ProfileId,Profile.Name  FROM User WHERE id = :UserInfo.getUserId()];
 for(FeedItem fi : trigger.old) {
      
      Document_Manager__c dm = new Document_Manager__c();
      
      try {
         dm = [Select Id from Document_Manager__c where Id =:fi.ParentId];
      }
      catch(Exception e) {
         dm = null;
      }
     
      if (dm != null) {
        if(fi.type == 'ContentPost' && (u.Profile.Name != 'System Administrator' || u.Name != 'Bryan Girkins'
           || u.Name != 'Mary Loy' || u.Name != 'Judit Szilagyi' || u.Name != 'Sarah Swanson'|| u.Name != 'Jeremy DalleTezze')) {
            fi.addError('You do not have the permissions to delete files from chatter feed');
        }
      }
 }
}

 
This was selected as the best answer
XIOXIO
Thanks Akhil, I update the code and it appears that it's blocking all users now.
XIOXIO
Hi Akhil. I updated with no errors! Thank you for all your help. 

Basically, the code is blocking all users (except the profiles and users in the IF statement) from deleting attachments only (no posts) from a specific custom object. 
 
trigger ChatterDeleteBlocker on FeedItem (After Delete) {
 User u = [SELECT id, Name, ProfileId,Profile.Name  FROM User WHERE id = :UserInfo.getUserId()];
 for(FeedItem fi : trigger.old) {
      
      Custom_Object__c co = new Custom_Object__c();
      
      try {
         co = [Select Id from Custom_Object__c where Id =:fi.ParentId];
      }
      catch(Exception e) {
         co = null;
      }
     
      if (co != null) {
        if(fi.type == 'ContentPost' && (u.Profile.Name != 'PLACE PROFILE NAME HERE') && (u.Name != 'PLACE USER NAME HERE'
           || u.Name != 'PLACE USER NAME HERE' || u.Name != 'PLACE USER NAME HERE' || u.Name != 'PLACE USER NAME HERE'|| u.Name != 'PLACE USER NAME HERE')) {
            fi.addError('You do not have the permissions to delete files from chatter feed');
        }
      }
 }
}