Skip to main content
added 304 characters in body
Source Link
Santanu Boral
  • 36.2k
  • 8
  • 45
  • 72

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);

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

}

@IsTest(seeAllData=false) private class TestNoteOnContentversion {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();
    }   
}

}

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

}

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

}

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

insert and updat unittest not covering. adderror why?

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

}

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

}