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();
    }   
}
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.

| improve this answer | |
  • 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? – Eager 2 Learn apex Jan 22 '17 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 – Santanu Boral Jan 22 '17 at 23:47
  • your right so in this case i need to update content right? Or is it contentlink? – Eager 2 Learn apex Jan 22 '17 at 23:55
  • update content.. – Santanu Boral Jan 22 '17 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? – Eager 2 Learn apex Jan 23 '17 at 0:11

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.