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?