0

I need to prevent ALL Users from creating a new Enhanced Note (we ended up creating our own object because we needed more customization). I'm trying to write a trigger but am getting stuck on an error. Any help would be apprediated!

trigger NoteBlockCreate on ContentVersion (before insert) {
for(ContentVersion cv:trigger.new){
    if(cv.FileType = 'SNOTE')
    {
        cv.adderror('Please use new notes');
    }
}
}

The error I get is: Field is not writeable: ContentVersion.FileType

3
  • 1
    For comparison , you need to use == operator. Like - cv.FileType == 'SNOTE' in your if statement.
    – Rohit
    Commented Mar 6, 2023 at 14:23
  • Of course! I'm so out of practice. Thank you! Unfortunately the trigger is not working when I test do you happen to have any suggestions?
    – Heather
    Commented Mar 6, 2023 at 14:27
  • I would suggest you to use String comparison operators listed here - developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/… Try adding System.debug() for checking the comparison.
    – Rohit
    Commented Mar 6, 2023 at 14:30

1 Answer 1

4

If you would like to compare one value to another, the proper syntax is:

if (version.FileType == 'SNOTE')

When you use only one =, it is an assignment rather than a comparison. Note that it is highly recommended to use whole words in your variable names.

3
  • 1
    Anecdotally, every time I read cv in code, I can't help but think of a resume (I watch way too much British programming). The designers of Apex gave us a symbol length of 40 characters, so we may as well be as descriptive as necessary.
    – sfdcfox
    Commented Mar 6, 2023 at 19:55
  • I'm the outlier here; I'm perfectly happy with cv for ContentVersion and cdl for ContentDocumentLink; especially in short vertical space for loops
    – cropredy
    Commented Mar 7, 2023 at 0:56
  • It's so little benefit and you force the reader to interpret what could be explicit and straightforward.
    – Adrian Larson
    Commented Mar 7, 2023 at 1:30

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .