• hero zero
  • NEWBIE
  • 10 Points
  • Member since 2019

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 3
    Replies
Hi,

I am newbie in Salesforce, what i am trying to do is creating a trigger for image files that starts with 'ABC' to open them to community. So this is my trigger in below but it gives this error when i am trying to upload a file starts with 'ABC':

FATAL_ERROR System.FinalException: Record is read-only
FATAL_ERROR Trigger.ContentDocumentLinkTrigger: line 4, column 1

I have used after insert because i am trying to fetch DocumentContentID and even if i change my trigger to before trigger it works other way around. It doesn't work for other image files. It just works for files with 'ABC' and it changes its visibility to on before trigger.

Can you please help me out? I couldn't sort it out in some way. I am using files by the way not notes&attachments.

trigger ContentDocumentLinkTrigger on ContentDocumentLink (after insert) {
  for (ContentDocumentLink cdl : Trigger.new) {
            if (getFileExtensionAndTitle(cdl)) {
                cdl.Visibility = 'AllUsers';
            }
        }
    
     public static Boolean getFileExtensionAndTitle(ContentDocumentLink cdl) {
        String docId = cdl.ContentDocumentId;
        ContentDocument contentdocument = [SELECT FileExtension,Title FROM ContentDocument WHERE Id = :docId LIMIT 1];
        Boolean isExpected = contentdocument.Title.startsWith('ABC') && (contentdocument.FileExtension.equals('jpg') || contentdocument.FileExtension.equals('jpeg') || contentdocument.FileExtension.equals('png'));
        return isExpected;
    }
}
Hi,

I am newbie in Salesforce, what i am trying to do is creating a trigger for image files that starts with 'ABC' to open them to community. So this is my trigger in below but it gives this error when i am trying to upload a file starts with 'ABC':

FATAL_ERROR System.FinalException: Record is read-only
FATAL_ERROR Trigger.ContentDocumentLinkTrigger: line 4, column 1

I have used after insert because i am trying to fetch DocumentContentID and even if i change my trigger to before trigger it works other way around. It doesn't work for other image files. It just works for files with 'ABC' and it changes its visibility to on before trigger.

Can you please help me out? I couldn't sort it out in some way. I am using files by the way not notes&attachments.

trigger ContentDocumentLinkTrigger on ContentDocumentLink (after insert) {
  for (ContentDocumentLink cdl : Trigger.new) {
            if (getFileExtensionAndTitle(cdl)) {
                cdl.Visibility = 'AllUsers';
            }
        }
    
     public static Boolean getFileExtensionAndTitle(ContentDocumentLink cdl) {
        String docId = cdl.ContentDocumentId;
        ContentDocument contentdocument = [SELECT FileExtension,Title FROM ContentDocument WHERE Id = :docId LIMIT 1];
        Boolean isExpected = contentdocument.Title.startsWith('ABC') && (contentdocument.FileExtension.equals('jpg') || contentdocument.FileExtension.equals('jpeg') || contentdocument.FileExtension.equals('png'));
        return isExpected;
    }
}