0

Why is my catch not working on the adderror on contentversion see:

trigger NoteOnContentversion on ContentVersion (before insert, before update) {
     for (ContentVersion c : Trigger.new)  {
        if (c.ContentDocumentId != null) {
            for (ContentDocumentLink link : [
                SELECT LinkedEntityId
                FROM ContentDocumentLink
                WHERE ContentDocumentId = :c.ContentDocumentId
            ]){
                Id parentId = link.LinkedEntityId;
                System.debug('link.LinkedEntityId '+ link);

                Boolean isOrderRegel = parentId.getSObjectType() == Orderregel__c.SObjectType;

                if (isOrderRegel && Approval.isLocked(parentId)){
                  c.addError('Approval pending. You do not have the permission to edit this note, please contact your administrator.');
                }
            }
        }
    }
}

Test class

@IsTest(seeAllData=false)
private class TestNoteOnContentversion {

    @IsTest 
    static void refuse_shouldAddError_whenOrderregelIsLocked() {
        // arrange
        Account acc = new Account(
            Name = 'TEST_ACCT', 
            Account_Status_DS__c = 'Status'
        );
        insert acc;

        Orderregel__c orderregel = new Orderregel__c(
            Account__c = acc.Id,
            Orderbegindatum__c = Date.today()
        );

        insert orderregel;

        ContentVersion content=new ContentVersion(); 
        content.Title='Header_Picture1'; 
        content.PathOnClient='/' + content.Title + '.jpg'; 
        Blob bodyBlob=Blob.valueOf('Unit Test ContentVersion Body'); 
        content.VersionData=bodyBlob; 
        //content.LinkedEntityId=sub.id;
        content.origin = 'H';
        insert content;

        ContentDocumentLink contentlink=new ContentDocumentLink();
        contentlink.LinkedEntityId=acc.id;
        contentlink.contentdocumentid=[select contentdocumentid from contentversion where id =: content.id].contentdocumentid;
        contentlink.ShareType = 'V';

        //insert contentlink;

        List<Account> accts = [SELECT Id from Account WHERE Name = 'TEST_ACCT'];

        //lock the account record.
        Approval.LockResult[] lrList = Approval.lock(accts, false);

        Test.startTest();
        try { 
              insert contentlink;
            }
        catch (Exception dmx) 
        { 
           Boolean expectedExceptionThrown = dmx.getMessage().contains('Approval pending. You do not have the permission to edit this note, please contact your administrator.') ? true : false; 
           System.assertEquals(expectedExceptionThrown, true);  
        }
        Test.stopTest();
    }   
}

1 Answer 1

0

I have already provided the guidance yesterday at your last question

Test Class coverage for trigger

Here, it can be seen that, you have not created Link record before locking Account record.

So, take this steps:

  1. Create ContentDocumentlink record.
  2. Lock Orderregel__c record where content document is under that Orderregel__c.
  3. Update the ContentDocument.

You will definitely get error to trap.

5
  • thanks for your help. On the ContentDocument it did work indeed, but im a little confused on the contentversion. Do you mean with 1. insert contentlink; 2. Approval.LockResult[] lrList = Approval.lock(accts, false); 3. update ContentDocument;? this last one isnt on the Contentversion could you please clarify whats wrong? Commented Jan 22, 2017 at 23:37
  • In your trigger you are locking Orderregel__c, but in test class you are locking Account... you are blindly copy & pasting the code without using the logic Commented Jan 22, 2017 at 23:47
  • your right so in this case i need to update content right? Or is it contentlink? Commented Jan 22, 2017 at 23:55
  • update content.. Commented Jan 22, 2017 at 23:56
  • ok if i use: Approval.LockResult[] lrList = Approval.lock(Orderregel__c, false); i get variable does not exist im a little in the dark here? Commented Jan 23, 2017 at 0:11

You must log in to answer this question.

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