Loading

Restrict Specific File Extensions from Being Uploaded to Salesforce Chatter Using an Apex Trigger

게시 일자: Jun 19, 2026
상세 설명

Overview

There are scenarios in which administrators want to restrict the upload of specific file extensions in Salesforce Chatter — for example, executable files (.exe), DLL files (.dll), or image formats (.jpg, .jpeg) — either globally or for specific user profiles.
The recommended approach is to create an Apex Trigger on the FeedItem object that checks the file extension of uploaded content and throws a validation error using the addError() method when a restricted extension is detected.

솔루션

Solution Approach

To restrict specific file extensions from being uploaded to Salesforce Chatter, create an Apex Trigger on the FeedItem object using the following logic:

  1. Create a set called setExtNotAllowed that contains the file extensions you want to block (for example: exe, dll, jpg, jpeg).
  2. Run a loop through the Trigger.new list of FeedItem records.
  3. For each FeedItem of type ContentPost, retrieve the associated ContentVersion record to check the file extension.
  4. If the file extension matches a blocked extension in your set, call addError() on the FeedItem to prevent the upload and display an error message to the user.

Disclaimer

The sample code below is provided as a reference only. Test all code customizations in a sandbox environment before deploying to production. Modify the logic to match your specific use case and requirements.

Sample Apex Trigger

The following sample trigger prevents upload of specific file extensions (exe, dll, jpg, jpeg) to Chatter. Modify the cv.FileType condition to match the extensions relevant to your org:

Trigger RestrictFile on FeedItem (after insert) {
Map<Id, FeedItem> contentversionIdToFeedItemMap = new Map<Id, FeedItem>();
for(FeedItem fi : trigger.new){
if(fi.type == 'ContentPost')
  contentversionIdToFeedItemMap.put(fi.RelatedRecordId, fi);
}
 for(ContentVersion cv : [SELECT Id, FileExtension,FileType FROM ContentVersion where Id IN :contentversionIdToFeedItemMap.keySet()])
{
  if(cv.FileType =='png')
  {
    contentversionIdToFeedItemMap.get(cv.Id).addError('File having this extension could not be attached,Please try some other extension.');
  }
 }  
}

Note: You can modify the conditions in the template trigger based on your use case.  




 

Knowledge 기사 번호

000383955

 
로드 중
Salesforce Help | Article