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.
To restrict specific file extensions from being uploaded to Salesforce Chatter, create an Apex Trigger on the FeedItem object using the following logic:
setExtNotAllowed that contains the file extensions you want to block (for example: exe, dll, jpg, jpeg).Trigger.new list of FeedItem records.ContentPost, retrieve the associated ContentVersion record to check the file extension.addError() on the FeedItem to prevent the upload and display an error message to the user.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.
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.
000383955

We use three kinds of cookies on our websites: required, functional, and advertising. You can choose whether functional and advertising cookies apply. Click on the different cookie categories to find out more about each category and to change the default settings.
Privacy Statement
Required cookies are necessary for basic website functionality. Some examples include: session cookies needed to transmit the website, authentication cookies, and security cookies.
Functional cookies enhance functions, performance, and services on the website. Some examples include: cookies used to analyze site traffic, cookies used for market research, and cookies used to display advertising that is not directed to a particular individual.
Advertising cookies track activity across websites in order to understand a viewer’s interests, and direct them specific marketing. Some examples include: cookies used for remarketing, or interest-based advertising.