0

I want to validate the file format of the file uploaded on file related list on record detail page and on the object record feed. Which content object to use to achieve this. Contentversion, contentdocument or contentdocumentlink.

-1

When we talk about contentVersion, ContentDocumentLink and ContentDocument, they are related to each other.

Whenever you upload a file from the related list on an object record or insert it through the apex and link with an object record, All the three contentVersion, ContentDocumentLink and ContentDocument records get created.

i.e. 1 Contentversion, 2 ContentDocumentLink and 1 ContentDocument

  • ContentVersion:- Represents a specific version of a document in Salesforce CRM Content or Salesforce Files. There may be multiple versions of the same file if it is created by the user.

  • ContentDocumentLink-Represents the link between a Salesforce CRM Content document or Salesforce file and where it's shared. A file can be shared with other users, groups, records, and Salesforce CRM Content libraries.

Note:- when you upload a file through the related list of an object record page, it has been shared with you and that particular record.

  • ContentDocument:- Represents a document that has been uploaded to a library in Salesforce CRM Content or Salesforce Files.

Now, When you are talking about the before trigger on an insert to prevent the file uploaded in a particular format, you have two option, But I will let you decide which one is better:-

update:-

  1. ContentVersion:- You are validating the file before insert, it will be easy for you to write a trigger on ContentVersion.
trigger ContentVersiontrigger on ContentVersion (before insert) {
    for(ContentVersion cv:Trigger.new){
        if(cv.PathOnClient==cv.Title+'.jpg'){
          cv.adderror('error');
        }
    }}

Note:-

  • The trigger will not get fire when you insert through chatter feed as I could check
  • When you will try to get the filetype directly before insert, it will give a null value.
  • FileType is defined by either ContentUrl for links or PathOnClient for documents, but not both.
  • PathOnClient:- The complete path of the document. One of the fields that determine the FileType.

Now,

  1. ContentDocumentLink:- It will be hard for you to write a trigger on it. Checking the Filetype before insert needs ContentDocument record gets inserted. So directly you cannot check the filetype through it. What you need is to query the ContentVersion again and check the FileType and throw the error.

I have a demo:-

    set<id> ids =new set<id>();
                Map<id,ContentDocumentLink> mapcvwithid =new map<id,ContentDocumentLink>();
                for(ContentDocumentLink c:Trigger.new){    
                    ids.add(c.ContentDocumentId);
                    mapcvwithid.put(c.ContentDocumentId,c);
                }for(ContentVersion cd: [select id, ContentDocumentId,FileType,Fileextension from ContentVersion where contentDocumentid=:ids]){
                    if(cd.FileExtension=='jpg' ){
                        mapcvwithid.get(cd.ContentDocumentId).adderror('error cdl');
                    }}

Note:-

  • First, ContentVersion gets inserted, then the other two get inserted. So when you write a trigger, many of field of ContentDocumentlink will be null for before insert in trigger.new.
  • The trigger will fire whenever you try to insert file either from Related list or Chatter feed.

If you want the trigger to get fire on both event(from related list and from chatter feed), go for ContentDocumentLink.

Hope it helps you.

  • Thanks for the detailed explanatiom Sanket, if i write a trigger on contentdocumentlink it doesnt fire on updating a file version. As i need file type validation on file version update and on uploading file from files related list or from chatterfeed. So would this require a trigger on contentversion and chatterfeed objects. Can you please help me to decide on this. Can 1 contentdocument be shared with many objects? – Sukruti Sep 1 '18 at 4:21
  • @Sukruti I have updated my answer with the details which I missed earlier. – sanket kumar Sep 1 '18 at 5:44
  • Yeah i did understand it better now. Thanks for detailed explanation Sanket! I have 1 more question i.e. contentdocumentlink trigger wont fire on updating version of file. I need a validation if filetype there too. So which would be better option to follow. Can you help me on it. – Sukruti Sep 1 '18 at 6:37
  • It is obvious behaviour, When you are updating version, ContentDocumentlink trigger will not get fire, But ContentVersion will fire. Similarly, When you are sharing a file in chatter feed, only contentDocumentlink triggger will fire, ContentVersion will not. The reason behind it is contentversion means insertion in short and contentdocumentlink means sharing the inserted file. – sanket kumar Sep 1 '18 at 6:45
  • to have a validation fired on updating version of a file needs the trigger on content version.i have a specific list of objects for which i have to restrict uploading of exe files from chatter as well as from files related list and also when the version is updated. If i write a trigger on content version i need the objectype of the current object to check whether its part of my list to fire a validation. so i need the object type.By querying on contentdocumentlink using the contentdocumentid which i get from content version i need object on which operation is done currently not all. – Sukruti Sep 1 '18 at 11:19
2

You want to use the ContentDocumentLink. Any record the uploaded file is related to will show up as the ContentDocumentLink via the LinkedEntityId.

Something like:

SELECT Id, ContentDocument.FileExtension FROM ContentDocumentLink WHERE LinkedEntityId =: <recordId>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.