0

I just finished writing the trigger below and I am so excited that it works. This is the first time I have ever done a trigger on the ContentDocumentLink object. I am now trying to create a test class for it and can't get more than 19% coverage. I know I am missing something obvious. Any help would be great! Thanks.

TRIGGER:

    trigger Update_Image_Field_BPR2 on ContentDocumentLink (after insert, after update, before delete, after undelete){
try{
    List<Subject__c> subList = new List<Subject__c>();
    List<Business__c> busList = new List<Business__c>();
    List<Schema.SobjectField> imageFldBus = new List<Schema.SobjectField> {
        Business__c.Image1__c,
        Business__c.Image2__c,
        Business__c.Image3__c,
        Business__c.Image4__c,
        Business__c.Image5__c};
    List<Schema.SobjectField> imageFldSubs = new List<Schema.SobjectField> {
        Subject__c.Image1__c,
        Subject__c.Image2__c,
        Subject__c.Image3__c,
        Subject__c.Image4__c,
        Subject__c.Image5__c};
    //Set of Subject__c Ids that we will iterate through
    Set<id> fileIds = new Set<id>();
    for(ContentDocumentLink content : Trigger.IsDelete ? Trigger.old : Trigger.new){
        Id parentObjId = content.LinkedEntityId;
        if(parentObjId.getSObjectType() == Subject__c.SObjectType || parentObjId.getSObjectType() == Business__c.SObjectType){
            fileIds.add(content.LinkedEntityId);
        }        
    }
    // Get the Subject__c id and Image1__c  for each Subject__c
    Map<id, Subject__c> subMap = new Map<id, Subject__c>([Select id, Image1__c, Image2__c, Image3__c, Image4__c, Image5__c 
                                                          from Subject__c Where Id in :fileIds]);  
    Map<id, Business__c> busMap = new Map<id, Business__c>([Select id, Image1__c, Image2__c, Image3__c, Image4__c, Image5__c 
                                                            from Business__c Where Id in :fileIds]);  
    Map<id, ContentDocumentLink> docMap = new Map<id, ContentDocumentLink>([SELECT ContentDocument.LatestPublishedVersionId 
                                                                            FROM ContentDocumentLink WHERE LinkedEntityId IN :fileIds
                                                                           AND (ContentDocument.FileExtension = 'jpg' OR ContentDocument.FileExtension = 'jpeg' 
                                                                                OR ContentDocument.FileExtension = 'png' OR ContentDocument.FileExtension = 'gif')]); 
    for(ContentDocumentLink content : Trigger.IsDelete ? Trigger.old : Trigger.new){
        Id parentObjId = content.LinkedEntityId;
        if(parentObjId.getSObjectType() == Business__c.SObjectType){
            Business__c bus = busMap.get(content.LinkedEntityId);
            ContentDocumentLink querymember = docMap.get(content.id);
            String fullFileURL = querymember.ContentDocument.LatestPublishedVersionId;
            system.debug(fullFileURL);
            if(!Trigger.IsDelete){
                for (Schema.SobjectField f : imageFldbus)
                if (bus.get(f) == null) { // is curr val null?
                    bus.put(f,fullFileUrl);  // set null val to url and leave loop
                    break;
                }}
            if(Trigger.IsDelete){
                for (Schema.SobjectField f : imageFldbus)
                if (bus.get(f) == fullFileUrl) { // is curr val URL?
                    bus.put(f,null);  // set val to null and leave loop
                    break;
                }}
            busList.add(bus);
        }
        if(parentObjId.getSObjectType() == Subject__c.SObjectType){
            Subject__c sub = subMap.get(content.LinkedEntityId);
            ContentDocumentLink querymember = docMap.get(content.id);
            String fullFileURL = querymember.ContentDocument.LatestPublishedVersionId;
            system.debug(fullFileURL);
            if(!Trigger.IsDelete){
                for (Schema.SobjectField f : imageFldsubs)
                if (sub.get(f) == null) { // is curr val null?
                    sub.put(f,fullFileUrl);  // set null val to url and leave loop
                    break;
                }}
            if(Trigger.IsDelete){
                for (Schema.SobjectField f : imageFldsubs)
                if (sub.get(f) == fullFileUrl) { // is curr val URL?
                    sub.put(f,null);  // set val to null and leave loop
                    break;
                }}            
            subList.add(sub);
        }
    }
    if(subList != null && subList.size() > 0){update subList;}if(busList != null && busList.size() > 0){update busList;}} catch(Exception e){ System.debug('ERROR: '+ e);}
}

APEX TEST CLASS:

@isTest(seeAllData=false)
public class Update_Image_Field_BPR2_TEST {
    static testMethod void testSubAddFiles() 
    { 
        Subject__c sub=new Subject__c(); 
            sub.Image1__c=null;
            sub.Image2__c=null;
            sub.Image3__c=null;
            sub.Image4__c=null;
            sub.Image5__c=null;
        insert sub; 

        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=sub.id;
            contentlink.contentdocumentid=content.contentdocumentid;
        insert contentlink;

        List<ContentDocumentLink> files=[SELECT Id, ContentDocumentId, ContentDocument.LatestPublishedVersionId, LinkedEntityId, ContentDocument.Title 
                                        from ContentDocumentLink where ContentDocumentId= :content.ContentDocumentId]; 
        System.assertEquals(1, files.size()); 
    }
    static testMethod void testSubRemoveFiles() 
    { 
        Subject__c sub=new Subject__c(); 
            sub.Image1__c=null;
            sub.Image2__c=null;
            sub.Image3__c=null;
            sub.Image4__c=null;
            sub.Image5__c=null;
        insert sub; 

        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=sub.id;
            contentlink.contentdocumentid=content.contentdocumentid;
        insert contentlink;        
        List<ContentDocumentLink> files=[SELECT Id, ContentDocumentId, ContentDocument.LatestPublishedVersionId, LinkedEntityId, ContentDocument.Title 
                                        from ContentDocumentLink where ContentDocumentId= :content.ContentDocumentId]; 
        sub.Image1__c = contentlink.ContentDocument.LatestPublishedVersionId;
        delete contentlink;
        System.assertEquals(0, files.size()); 
    }
    static testMethod void testSubUpdateFiles() 
    { 
        Subject__c sub=new Subject__c(); 
            sub.Image1__c=null;
            sub.Image2__c=null;
            sub.Image3__c=null;
            sub.Image4__c=null;
            sub.Image5__c=null;
        insert sub; 

        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=sub.id;
            contentlink.contentdocumentid=content.contentdocumentid;
        insert contentlink;
        update contentlink;

        List<ContentDocumentLink> files=[SELECT Id, ContentDocumentId, ContentDocument.LatestPublishedVersionId, LinkedEntityId, ContentDocument.Title 
                                        from ContentDocumentLink where ContentDocumentId= :content.ContentDocumentId]; 
        System.assertEquals(1, files.size()); 
    }
    static testMethod void testBusAddFiles() 
    { 
        Business__c bus=new Business__c(); 
            bus.Image1__c=null;
            bus.Image2__c=null;
            bus.Image3__c=null;
            bus.Image4__c=null;
            bus.Image5__c=null;
        insert bus; 

        ContentVersion content=new ContentVersion(); 
            content.Title='Header_Picture2'; 
            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=bus.id;
            contentlink.contentdocumentid=content.contentdocumentid;
        insert contentlink;

        List<ContentDocumentLink> files=[SELECT Id, ContentDocumentId, ContentDocument.LatestPublishedVersionId, LinkedEntityId, ContentDocument.Title 
                                        from ContentDocumentLink where ContentDocumentId= :content.ContentDocumentId]; 
        System.assertEquals(1, files.size()); 
    }
    static testMethod void testBusRemoveFiles() 
    { 
        Business__c bus=new Business__c(); 
            bus.Image1__c=null;
            bus.Image2__c=null;
            bus.Image3__c=null;
            bus.Image4__c=null;
            bus.Image5__c=null;
        insert bus; 

        ContentVersion content=new ContentVersion(); 
            content.Title='Header_Picture2'; 
            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=bus.id;
            contentlink.contentdocumentid=content.contentdocumentid;
        insert contentlink;

        List<ContentDocumentLink> files=[SELECT Id, ContentDocumentId, ContentDocument.LatestPublishedVersionId, LinkedEntityId, ContentDocument.Title 
                                        from ContentDocumentLink where ContentDocumentId= :content.ContentDocumentId]; 
        bus.Image1__c = contentlink.ContentDocument.LatestPublishedVersionId;
        delete contentlink;
        System.assertEquals(0, files.size()); 
    }
    static testMethod void testBusUpdateFiles() 
    { 
        Business__c bus=new Business__c(); 
            bus.Image1__c=null;
            bus.Image2__c=null;
            bus.Image3__c=null;
            bus.Image4__c=null;
            bus.Image5__c=null;
        insert bus; 

        ContentVersion content=new ContentVersion(); 
            content.Title='Header_Picture2'; 
            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=bus.id;
            contentlink.contentdocumentid=content.contentdocumentid;
        insert contentlink;
        update contentLink;

        List<ContentDocumentLink> files=[SELECT Id, ContentDocumentId, ContentDocument.LatestPublishedVersionId, LinkedEntityId, ContentDocument.Title 
                                        from ContentDocumentLink where ContentDocumentId= :content.ContentDocumentId]; 
        System.assertEquals(1, files.size()); 
    }
}
3
  • 1
    If/else chains like that are a bad chain. Consider iterating through a List<SObjectField> instead.
    – Adrian Larson
    Mar 1 '16 at 18:48
  • Adrian, Can you show an example of how I can do that? Thanks!
    – Nathan
    Mar 1 '16 at 19:25
  • Just a note, you don't need to do Schema.Subject__c.Image1__c, you can simplify it to just Subject__c.Image1__c.
    – Adrian Larson
    Mar 1 '16 at 22:38
1

You have a number of if/else statements in there and it looks like your test methods do not cover each of those. As such the test coverage will only calculate the percentage of code you're actually running through. To fix this simply add more test cases that will hit the different if/else statements.

Also your trigger is after insert, after update, before delete, after undelete but you only test insert. Try adding test cases for update, delete and undelete.

And finally, you might want to look at re-writing your if/else's to be if/else if. And since they all do the same behavior you might want to pull out into a method.

3
  • Chris, Can you show me some examples of how to do that? I appreciate your help. Thanks!
    – Nathan
    Mar 1 '16 at 19:25
  • 1
    That's a little too much specific code to write in here. But in short, to pass your 75% minimum, try replicating your testBusFiles method, and adding an update contentLink call after your insert contentLink. Then replicate again and add in delete contentLink. Then replicate again, add in delete contentLink and then undelete contentLink. That should get you over the hump.. though there is still plenty to do to make your code clean, but that would be appropriate on a different thread or do some research on your own. Mar 1 '16 at 19:40
  • Thank you for the suggestion. I have updated the code above. Even with the suggestions it is still not giving more than 32% for code coverage. It is still having issues with everything after this line of code. Any ideas? Thanks. for(ContentDocumentLink content : Trigger.IsDelete ? Trigger.old : Trigger.new){ Id parentObjId = content.LinkedEntityId; if(parentObjId.getSObjectType() == Business__c.SObjectType){
    – Nathan
    Mar 1 '16 at 22:39
1

Here is how you rewrite those if statements ...

if (!Trigger.isDelete)
 for (String f : new List<String> {'Image1__c','Image2__c','Image3__c','Image4__c',Image5__c'})
   if (bus.get(f) == null) { // is curr val null?
      bus.put(f,fullUrl);  // set null val to url and leave loop
      break;
   }

and similar for the Trigger.isDelete use case

@AdrianLarson is correct, a better solution would be

List<Schema.SobjectField> imageFlds = new List<Schema.SobjectField> {
  Schema.Business__c.Image1__c,
  Schema.Business__c.Image2__c
  Schema.Business__c.Image3__c
  Schema.Business__c.Image4__c
  Schema.Business__c.Image5__c}; 




if (!Trigger.isDelete)
 for (Schema.SobjectField f : imageFlds)
   if (bus.get(f) == null) { // is curr val null?
      bus.put(f,fullUrl);  // set null val to url and leave loop
      break;
   }
3
  • Could use Schema fields to make it a little cleaner. That's definitely what I was thinking though.
    – Adrian Larson
    Mar 1 '16 at 21:36
  • 1
    yep, I agree -- I was going for the one line solution as the nested if-else bothered me as much as you
    – cropredy
    Mar 1 '16 at 21:38
  • Great! Appreciate the examples. Based on your suggestion, I have updated the code above. Thanks again!
    – Nathan
    Mar 1 '16 at 22:36

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.