In Apex using a AfterInsert Trigger on ContentVersion (cv) object, the method cv.addError ('Custom message') is not working as expected.

Instead of showing the custom error message the error that is showing is just standard error and provide no information about the error.

trigger ContentVersionTrigger on ContentVersion (after insert) {
    for (ContentVersion cv : trigger.new) {
        System.debug(cv.Title+' : ' +cv.ContentSize);
        if(cv.ContentSize > 5242880){
            cv.addError('You can\'t upload files with size more than 5MB.');

There is a similar question in the Dev community (Trigger on ContentDocumentLink) but no answer.

  • 1
    Yeah!! If you building a Lightning component for content management, I think it should be handled from UI side rather than the trigger. – Sarvesh Sep 10 '19 at 10:43
  • @sarveshkumar I am using lightning:fileUpload, and I couldn't find any attribute regarding restricting files based on size. Any idea how to implement without trigger? – Saroj Bera Sep 10 '19 at 12:29
  • 1
    Use onuploadfinished event, Please see the document. developer.salesforce.com/docs/component-library/bundle/… – Sarvesh Sep 10 '19 at 12:57
  • @sarveshkumar onuploadfinished triggered when files have finished uploading.so I can't use this to restrict the upload. – Saroj Bera Sep 10 '19 at 18:30

Your Answer

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

Browse other questions tagged or ask your own question.