0

Is there anyway to prevent a user from deleting a New Note, based on the status of the Opportunity that the contentnote is linked to?

The requirement is that I have a datetime field that is set, once the opportunity status is "Closed Won". Once this datetime field is not null, a note linked to this opportunity record should not be deleted.

I started writing a trigger on the contentdocument object (since ContentNote wont allow me to write a trigger), but it looks like the code is not working since its never hitting the ContentDocument Trigger.

Is this achievable?

  • Have you tried to trigger on ContentVersion instead of ContentDocument since every ContentDocument is inside of a ContentVersion as it can be updated - thus creating a new version of the same document - you can set share access to the file or update the Share access to the file to make it V or View Access in your trigger by editing the ContentDocumentLink for the user. – Ronnie Oct 6 '17 at 2:33
  • developer.salesforce.com/docs/atlas.en-us.api.meta/api/… LinkedEntityId is the connection to "What" this is shared with - you can change their access to the file to prevent them from deleting the file in your trigger – Ronnie Oct 6 '17 at 2:39
0

You can write trigger before delete on contentDocument.

you will get the ContentDocumentid from contentDocument in trigger.new.after that you need to fetch the opportunity id by querying the ContentDocumentLink as:- =[SELECT Id, LinkedEntityId, ContentDocumentId, Visibility, IsDeleted, ShareType,ContentDocument.Title, ContentDocument.createdDate, ContentDocument.FileType FROM ContentDocumentLink WHERE ContentDocumentId=:'your contentdocument id' and ContentDocument.FileType= 'SNOTE'];

this query will give you the List of Contentdocument which you have tried to delete. Now iterate it and store the LinkedEntityId in a set. The LinkedentityId is nothing but your opportunity id on which note is related with.

Now you can add logic to check for that opportunity and show error using add error method.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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