0

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.');
            }
        }
    }
}
4
  • 3
    Are you sure you want to have this trigger on 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's ContentNote. Nov 23, 2022 at 14:49
  • Thanks @ThomasTaylor, yes I'm using Notes in Lightning. I don't see an option to create a trigger on ContentNote. I do see ContentDocument as an option.
    – Dhrivz
    Nov 23, 2022 at 18:21
  • 1
    I think you'd want to fire on ContentVersion and basically prevent creation of new records if the parent is of type SNOTE. But haven't tried it. Nov 23, 2022 at 18:45
  • 1
    The trigger on ContentVersion is not working as expected either. I came across this link (ideas.salesforce.com/s/idea/a0B8W00000Gda30UAB/…) which suggests that writing a trigger to prevent Note updates is not possible at this moment in time (i.e., see comments from the Customers). I think the autosave functionality on the Note itself is throwing the trigger off.
    – Dhrivz
    Nov 23, 2022 at 19:27

0

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.