-2

Please find the below code:

trigger DeleteNote on Note (before delete) {

    for(Note N : trigger.old){

        if(Trigger.isDelete){

            N.adderror('Note Cannot be deleted');
        }

    }

}

Please suggest changes for this trigger to work.

2

Update :

If you are using Enhanced Notes, then you will have to write a trigger on ContentDocument object. When using Enhanced Notes, it does not create Notes rather it creates ContentNote records. You can find the structure of the ContentNote which is much more complex, so you will have to change your trigger to be on ContentDocument and not on Note

And this is how your trigger should look like

trigger DeleteNote on ContentDocument (before delete) {
    if(Trigger.isBefore && Trigger.isDelete){
        Map<Id, ContentDocument> mapContentDocument = new Map<Id, ContentDocument>([Select Id, FileExtension from ContentDocument where Id in: trigger.oldMap.keyset()]);
        for(ContentDocument cd : Trigger.Old){
            if(mapContentDocument.containsKey(cd.Id) && mapContentDocument.get(cd.Id).FileExtension == 'snote' ){
                cd.adderror('Note Cannot be deleted');
            }
        }
    }
}

Your trigger should be on before delete event and not on after delete. In after delete event, the note would already have be deleted. So just change the event and your trigger should work fine.

trigger DeleteNote on Note (before delete) {
    if(Trigger.isDelete && Trigger.isBefore){
        for(Note N : trigger.old){
            N.adderror('Note Cannot be deleted');
        }
    }
}
| improve this answer | |
  • .It is still not working. Case owner is able to delete the Note. – Sneha Oct 1 '19 at 7:43
  • @Sneha Did you change the event from after delete to before delete ? – Vijay Ganji Oct 1 '19 at 7:51
  • Yes...I've tried the same trigger you've mentioned in the comment – Sneha Oct 1 '19 at 8:00
  • Thank you!. trigger on ContentDocument worked – Sneha Oct 1 '19 at 11:15
0

If your trigger on Note is not firing for the note you are deleting then it is the enhanced note you are deleting not the old note.

I this case you should write trigger on ContentDocument object and check them with matching id in trigger.old.

trigger DeleteContentDocument on ContentDocument (before delete) {
    for(ContentDocument cd: [SELECT Id, FileExtension FROM ContentDocument WHERE FileExtension = 'snote' and Id =: trigger.oldMap.keyset() ]){
        trigger.oldMap.get(cd.Id).adderror('Note Cannot be deleted');                         
    }
}

Note:- You cannot write trigger on ContentNote. Also, I have not choosen the contentdocumentLink because one note can be shared with multiple records and hence the multiple contentDocumentLink will be generated. You need to write extra stuff to handle them.

| improve this answer | |
  • Errors can only be added on the context variables, you have queried the record and trying to add error to that record not to the one which is in transaction. This will give you System.FinalException: SObject row does not allow errors – Vijay Ganji Oct 1 '19 at 11:05
  • Ahh, Thanks for highlighting it. corrected it now. – sanket kumar Oct 1 '19 at 11:17

Not the answer you're looking for? Browse other questions tagged or ask your own question.