0

I have written trigger and a test class for my content version

Handler

public class handlerClassContentVersion{
    public set<string>prodNames = new set<string>();
    public set<string>contentid = new set<string>();
    public list<Product2>proList = new list<Product2>();
    public list<File_Entries__c>entList = new list<File_Entries__c>();
    public map<id,id>fileEntryMap = new map<id,id>();

    public void onAfterInsert(list<ContentVersion>conList){
        for(ContentVersion cov:conList){//capturing trigger.new 
            prodNames.add(cov.Product__c); 
            contentid.add(cov.ContentDocumentId);
        }

        proList = [select id,name from Product2 where name IN :prodNames];

        List<File_Entries__c> lstFileEntries = [select id,Product__c,File_ID__c from File_Entries__c where File_ID__c IN: contentid];//querying existing file entries records
        for(File_Entries__c lst:lstFileEntries){
            fileEntryMap.put(lst.File_ID__c,lst.id);//mapping contentdocument id and file id
        }
        for(ContentVersion cov:conList){
            for(Product2 pro : proList){
                if(lstFileEntries.size()>0){//updatinf gor existing records
                    if(fileEntryMap!=null && fileEntryMap.get(cov.ContentDocumentId)!=null){
                        File_Entries__c fe = new File_Entries__c(id=fileEntryMap.get(cov.ContentDocumentId));
                        fe.Product__c = pro.id;
                        fe.File_ID__c = cov.ContentDocumentId;
                        entList.add(fe);
                        system.debug('===entList==='+entList);

                    try{
                        update entList;     
                    }
                    catch(Exception e){
                    }
                }   
            }       
                    else{
                      system.debug('==else==');
                      File_Entries__c fe = new File_Entries__c();//creating new records
                      fe.Product__c = pro.id;
                      fe.File_ID__c = cov.ContentDocumentId;
                      entList.add(fe);
                    try{
                        insert entList;
                    }
                    catch(Exception e){
                    }
               } 
           }     
        }
    }
}

Test

@isTest
public class TesthandlerClassContentVersion{
    static testMethod void contentDoc() {
    list<ContentVersion>conlist = new list<ContentVersion>();

     //ContentDocument cd = [SELECT Id FROM ContentDocument ];


     ContentVersion cv = new ContentVersion();
     //cv.ContentDocumentId = '069M0000000Tpig';
     cv.Brand__c ='Heil';
     cv.Product_Category__c='Front Loader';
     cv.Product__c ='Half-Pack';
     cv.Document_Type__c = 'Generic Forms';
     cv.ContentURL='http://www.google.com/';
     cv.Title ='con';
     conlist.add(cv);
     insert cv;

     //ContentVersion cv = new ContentVersion();
     //cv.ContentDocumentId = '069M0000000Tpig';
     cv.Brand__c ='Heil';
     cv.Product_Category__c='Front Loader';
     cv.Product__c ='Half-Pack Freedom';
     cv.Document_Type__c = 'Generic Forms';
     cv.ContentURL='http://www.google.com/';
     cv.Title ='con';
     conlist.add(cv);
     update cv;

     handlerClassContentVersion handler = new handlerClassContentVersion();
     handler.onAfterInsert(conlist);
    }
}

I am not able to insert the content document and hence am not able to create a relationship between the content version and the content document in my test class. I am getting a NULL Pointer exception at the line handler.onAfterInsert(conlist);.Where am I going wrong?

0

1 Answer 1

1

First of all, ContentDocument is automatically inserted by SFDC when you insert a ContentVersion. This is covered in the SFDC Objects doc

You can discover the ContentDocument by doing:

ContentVersion cv = new ContentVersion(title='foo', contentUrl='http:///www.foor.bar');
insert cv;
ContentVersion cvRes = [select id, ContentDocument.id, ContentDocument.title 
                         from ContentVersion where id= :cv.id];
ContentDocument cd = cv.ContentDocument;

Debug output:

ContentDocument=ContentDocument:{Id=06940000001sEpHAAU, Title=foo}
ContentVersion=ContentVersion:{Id=06840000002udVdAAI, ContentDocumentId=06940000001sEpHAAU}

Secondly, your testmethod is updating the inserted ContentVersion and then adding it again to the list conList. Updating a ContentVersion does not create a new version, it simply edits the existing version.

Thirdly, your handler line:

contentid.add(cov.ContentDocumentId);

will add null to contentId as you need to requery for ContentDocumentId before invoking your handler as conList (and by inference any cv element) will not have ContentDocumentId set. Hence null pointer exception.

You can think of ContentDocumentId as like a fomula field or auto-number field on an inserted sobject. The value is not known to the apex code until you requery theContentVersion.

0

You must log in to answer this question.

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