0

i need an option to create a note preventing the edit and delete on notes for a particular custom object. i wrote a below trigger .. but it is restricting to create a note also.

trigger NoteTrigger on ContentDocument(before delete, before update) {

    Schema.DescribeSObjectResult inv = Claim__c.sObjectType.getDescribe();
    String invKeyPrefix = inv.getKeyPrefix();

    List < Id > contentDocId = new List < Id > ();
    Map < Id, Id > contDocLinkedMap = new Map < Id, Id > ();

    if (trigger.IsDelete || trigger.IsUpdate) {
        for (ContentDocument con: Trigger.old) {
            contentDocId.add(con.Id);
        }
        for (ContentDocumentLink cdl: [SELECT ContentDocumentId, LinkedEntityId FROM ContentDocumentLink WHERE ContentDocumentId IN: contentDocId]) {
            contDocLinkedMap.put(cdl.ContentDocumentId, cdl.LinkedEntityId);
            System.debug('map ' + contDocLinkedMap);
        }

        for (ContentDocument cdoc: Trigger.new == null ? Trigger.old : Trigger.new) {
            //contDocLinkedMap.get(cdoc.Id) will give the LinkedEntityId
            if (invKeyPrefix == String.valueOf(contDocLinkedMap.get(cdoc.Id)).left(3) && cdoc.FileType == 'SNOTE') {
                cdoc.adderror('Notes could not be Edited or deleted .');
            }
        }
    }
}
1
  • 1
    Please make an effort to indent and format your code to help readers understand its logical structure.
    – David Reed
    Mar 21, 2019 at 20:57

1 Answer 1

2

ContentDocument acts, in the Content model, as the parent of one or more ContentVersion records, which represent the versions of the underlying body content (here, the note itself). As the parent, ContentDocument is inserted first, followed by ContentVersion, when a record is created.

Fields on the ContentDocument may be altered by the insertion of a ContentVersion, including the first one created upon the initial addition of a Note. (LatestPublishedVersionId and ContentSize, for example, one might expect to update - I don't know the exact underlying mechanics).

As a result an update trigger on ContentDocument will be fired during the initial insertion of a Note.

You'll probably have to do some experimenting with heuristics to determine whether the update event you're processing for any specific ContentDocument is actually the initial insert. Perhaps you could check if the old value of LatestPublishedVersionId is null? I'm not actually certain what logic will be needed.

As an additional note, your code currently assumes that each ContentDocument is associated with exactly one ContentDocumentLink, and that assumption is not accurate.

2
  • i wrote a trigger on contentversion object for before update , but need to know how restrict this only one specific custom object. any help please. trigger ContentVersion_PreventUpdate on ContentVersion (before update) { for ( ContentVersion oldCv : Trigger.old ) { ContentVersion newCv = Trigger.newMap.get(oldCv.Id); if ( (oldCv.Title != newCv.Title || oldCv.Description != newCv.Description) && oldCv.FileType='SNOTE') { newCv.addError('You do not have permission to edit this record.'); } } }
    – Madhu
    Mar 24, 2019 at 22:43
  • @Madhu If you have a new question, please open a new post. You'll need to provide more detail about what you're asking in that post; I am not sure how to answer based on what you said.
    – David Reed
    Mar 24, 2019 at 22:44

You must log in to answer this question.

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