Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- trigger NoteOnContentDocument on ContentDocument (before delete) {
- for (ContentDocument c : Trigger.old){
- List<ContentDocumentLink> links = [SELECT LinkedEntityId FROM ContentDocumentLink
- WHERE ContentDocumentId= :c.Id];
- if (!links.isEmpty() && Approval.isLocked(links.get(0).LinkedEntityId)){
- c.addError('Approval pending. You do not have the permission to edit/delete this note/attachment, please contact your administrator.');
- }
- }
- }
- @istest
- public class TestNoteOnContentDocuments {
- static testmethod void NoteOnContentDoc(){
- ContentVersion contentVersion = new ContentVersion(
- Title = 'Penguins',
- PathOnClient = 'Penguins.jpg',
- VersionData = Blob.valueOf('Test Content'),
- IsMajorVersion = true
- );
- insert contentVersion;
- List<ContentDocument> documents = [SELECT Id, Title, LatestPublishedVersionId FROM ContentDocument];
- delete documents;
- DmlException expectedException;
- Test.startTest();
- try { delete documents; }
- catch (DmlException dmx) {
- Boolean expectedExceptionThrown = dmx.getMessage().contains('Approval pending. You do not have the permission to edit/delete this note/attachment, please contact your administrator.') ? true : false;
- System.assertEquals(expectedExceptionThrown, true);
- }
- Test.stopTest();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement