Content Version Trigger to restrict specific filetype from upload

Latest Posts Forums Project Scenarios Lightning Aura Component Content Version Trigger to restrict specific filetype from upload

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #1327 Reply
    Dimple Gupta
    Guest

    I need to write a trigger on File(ContentDocument) object in salesforce to restrict certain file formats from getting uploaded but triggers dont fir on that object it seems. Please help.

    #1329 Reply
    Admin
    Keymaster

    I would like to mention that ContentDocument object only supports delete(), describeSObjects(), query(), retrieve(), undelete(), update() calls. And ContentVersion Object supports create(), describeLayout(), describeSObjects(), query(), retrieve(), search(), update(), upsert() calls.

    Documentation can be found here:
    http://www.salesforce.com/us/developer/docs/api/Content/sforce_api_objects_contentdocument.htm

    http://www.salesforce.com/us/developer/docs/api/Content/sforce_api_objects_contentversion.htm

    Now to restrict the files I tried working with before insert and update triggers which did not work well as before the insertion the fileType fields came as null.

    So Using After insert I have achieved the required. Use the below code at your own discretion.

    public without sharing class FileService {
        public static void RestrictFiles(List<ContentVersion> ContentList){
            for(ContentVersion doc : ContentList){
                if(doc.FileType != null && doc.FileType =='EXCEL_X' ){
                    doc.addError('File having this extension could not be attached,Please try some other extension.');
                }
             }
        }
    }
    #1640 Reply
    Michael
    Guest

    I’m looking to restrict pdf files from being uploaded to a custom object if its a new version can you help?

    #1328 Reply
    Admin
    Guest

    I would like to mention that ContentDocument object only supports delete(), describeSObjects(), query(), retrieve(), undelete(), update() calls. And ContentVersion Object supports create(), describeLayout(), describeSObjects(), query(), retrieve(), search(), update(), upsert() calls.

    Documentation can be found here:
    http://www.salesforce.com/us/developer/docs/api/Content/sforce_api_objects_contentdocument.htm

    http://www.salesforce.com/us/developer/docs/api/Content/sforce_api_objects_contentversion.htm

    Now to restrict the files I tried working with before insert and update triggers which did not work well as before the insertion the fileType fields came as null.

    So Using After insert I have achieved the required. Use the below code at your own discretion.

    public without sharing class FileService {
        public static void RestrictFiles(List<ContentVersion> ContentList){
            for(ContentVersion doc : ContentList){
                if(doc.FileType != null && doc.FileType =='EXCEL_X' ){
                    doc.addError('File having this extension could not be attached,Please try some other extension.');
                }
             }
        }
    }
Viewing 4 posts - 1 through 4 (of 4 total)
Reply To: Content Version Trigger to restrict specific filetype from upload
Your information:




error: Content is protected !!