Once a Note record is created in Salesforce, I would like to prevent that note from being edited by any user.
I tried this code but it is not working as I am still able to edit a note and the error message is not being thrown. Any ideas how else I can go about this?
trigger NoteTrigger on Note (before update, before delete) {
if (trigger.isbefore && trigger.isupdate){
NoteTriggerHandler.PreventNoteEdit(trigger.new, trigger.oldMap);
}
}
public without sharing class NoteTriggerHandler {
public static void PreventNoteEdit(List<Note> nList, Map<Id, Note> oldMap) {
for (Note n : nList){
if(oldMap.get(n.id).Body != n.Body) {
n.addError('Once a note is created it cannot be edited or deleted.');
}
}
}
}
Note
? That's the old note object from the Notes & Attachments related list. If you're using notes in Lightning with the rich text editor, that'sContentNote
.ContentVersion
and basically prevent creation of new records if the parent is of typeSNOTE
. But haven't tried it.