You need to sign in to do that
Don't have an account?
Sarah Robertson
Help with Test class for ContentDocument
HI we've developed a trigger which is fired in the event of deletion upon the ContentDocument object. Wondering if someone could tell me where i am going wrong ! The class is running with no apparent errors however we have 0% code coverage :( :
My Class :
@istest
public class count_attach_test {
static testmethod void attachtest() {
Account a = new Account();
a.RecordTypeId='0121r000000pvQm';
a.name = 'Test NL Account';
insert a;
Attachment at = new Attachment();
at.name = 'test attachment';
at.body = blob.valueof('attachment body');
at.parentid = a.id;
insert at;
Opportunity o = New Opportunity ();
o.RecordTypeId ='0121r000000pvdX';
o.AccountId =a.Id;
o.StageName ='Stage 1: Suspect';
o.Start_Date__c=System.today();
o.CloseDate=System.today() + 5;
o.Name='Test NL Opportunity';
insert o;
o.attachment_edited__c=TRUE;
update o;
Attachment at2 = new Attachment();
at2.name = 'test attachment';
at2.body = blob.valueof('attachment body');
at2.parentid = o.id;
insert at2;
delete at;
delete at2;
Blob beforeblob=Blob.valueOf('Unit Test Attachment Body');
ContentVersion cv = new ContentVersion();
cv.title = 'test content trigger';
cv.PathOnClient ='test';
cv.VersionData =beforeblob;
insert cv;
ContentVersion testContent = [SELECT id, ContentDocumentId FROM ContentVersion where Id = :cv.Id];
List<ContentDocument> documents = [SELECT Id, Title, LatestPublishedVersionId FROM ContentDocument];
ContentDocumentLink contentlink=new ContentDocumentLink();
contentlink.LinkedEntityId=a.id;
contentlink.ShareType= 'v';
contentlink.LinkedEntityId = a.Id;
contentlink.ContentDocumentId=documents[0].Id;
contentlink.Visibility = 'AllUsers';
insert contentlink;
delete contentlink;
//List<Attachment> attachments = [Select Id From Attachment Where parentId = :a.id];
// System.assert(attachments.size() == 1, 'Wrong number of attachments (( ' + attachments.size() );
} }
My Trigger :
trigger DeleteFileNL on ContentDocument (before delete) {
Set<id> contentDocumentIDs = new Set<Id>();
Set<id> sObjectIds = new Set<id>();
Map<id,Account> tempAccounts = new Map<id,Account>();
List<Account> accountsToUpdate = new List<Account>();
List<ContentDocument> cdocuments = ( Trigger.isDelete ? Trigger.old : Trigger.new );
String additonalWhereClause = ( Trigger.isDelete ? 'and ContentDocumentID not in:contentDocumentIDs' : '');
for(ContentDocument cd: cdocuments){
contentDocumentIDs.add(cd.id);
}
for ( ContentDocumentLink cdl : [Select LinkedEntityID, ContentDocumentID from ContentDocumentLink where ContentDocumentID IN: contentDocumentIDs]) {
sObjectIds.add( cdl.LinkedEntityId );
}
if(sObjectIds.size()>0){
String query = 'Select LinkedEntityId, count(ContentDocumentID) from ContentDocumentLink where LinkedEntityId in:sObjectIds '+additonalWhereClause+' group by LinkedEntityId';
AggregateResult[] aggrResult = Database.query(query);
SYstem.debug(query);
Map<id,Account> accMap = new Map<id,Account>([Select id, name, Attachment_Increment__c from Account where id in:sObjectIds]);
for(AggregateResult ar: aggrResult){
Object sObjectId = ar.get('LinkedEntityId');
if(accMap.containsKey((id)sObjectId)){
Account acc = accMap.get((id)sObjectId);
//acc.Attachment_Increment__c = (Integer)ar.get('expr0');
acc.Attachment_Increment__c=1*-1;
acc.Attachment_Edited__c=True;
System.debug('(Integer)ar.get(expr0)'+(Integer)ar.get('expr0'));
tempAccounts.put(acc.id,acc);
accountsToUpdate.add(acc);
}
}
for(Account acc: accMap.values()){
if(!tempAccounts.containsKey(acc.id)){
Account a = acc;
acc.Attachment_Increment__c=1*-1;
a.Attachment_Edited__c=True;
accountsToUpdate.add(acc);
}
}
try{
update accountsToUpdate;
}catch(Exception e){
System.debug('Error Message: '+e.getMessage()+' trace: '+e.getStackTraceString());
}
}
}
My Class :
@istest
public class count_attach_test {
static testmethod void attachtest() {
Account a = new Account();
a.RecordTypeId='0121r000000pvQm';
a.name = 'Test NL Account';
insert a;
Attachment at = new Attachment();
at.name = 'test attachment';
at.body = blob.valueof('attachment body');
at.parentid = a.id;
insert at;
Opportunity o = New Opportunity ();
o.RecordTypeId ='0121r000000pvdX';
o.AccountId =a.Id;
o.StageName ='Stage 1: Suspect';
o.Start_Date__c=System.today();
o.CloseDate=System.today() + 5;
o.Name='Test NL Opportunity';
insert o;
o.attachment_edited__c=TRUE;
update o;
Attachment at2 = new Attachment();
at2.name = 'test attachment';
at2.body = blob.valueof('attachment body');
at2.parentid = o.id;
insert at2;
delete at;
delete at2;
Blob beforeblob=Blob.valueOf('Unit Test Attachment Body');
ContentVersion cv = new ContentVersion();
cv.title = 'test content trigger';
cv.PathOnClient ='test';
cv.VersionData =beforeblob;
insert cv;
ContentVersion testContent = [SELECT id, ContentDocumentId FROM ContentVersion where Id = :cv.Id];
List<ContentDocument> documents = [SELECT Id, Title, LatestPublishedVersionId FROM ContentDocument];
ContentDocumentLink contentlink=new ContentDocumentLink();
contentlink.LinkedEntityId=a.id;
contentlink.ShareType= 'v';
contentlink.LinkedEntityId = a.Id;
contentlink.ContentDocumentId=documents[0].Id;
contentlink.Visibility = 'AllUsers';
insert contentlink;
delete contentlink;
//List<Attachment> attachments = [Select Id From Attachment Where parentId = :a.id];
// System.assert(attachments.size() == 1, 'Wrong number of attachments (( ' + attachments.size() );
} }
My Trigger :
trigger DeleteFileNL on ContentDocument (before delete) {
Set<id> contentDocumentIDs = new Set<Id>();
Set<id> sObjectIds = new Set<id>();
Map<id,Account> tempAccounts = new Map<id,Account>();
List<Account> accountsToUpdate = new List<Account>();
List<ContentDocument> cdocuments = ( Trigger.isDelete ? Trigger.old : Trigger.new );
String additonalWhereClause = ( Trigger.isDelete ? 'and ContentDocumentID not in:contentDocumentIDs' : '');
for(ContentDocument cd: cdocuments){
contentDocumentIDs.add(cd.id);
}
for ( ContentDocumentLink cdl : [Select LinkedEntityID, ContentDocumentID from ContentDocumentLink where ContentDocumentID IN: contentDocumentIDs]) {
sObjectIds.add( cdl.LinkedEntityId );
}
if(sObjectIds.size()>0){
String query = 'Select LinkedEntityId, count(ContentDocumentID) from ContentDocumentLink where LinkedEntityId in:sObjectIds '+additonalWhereClause+' group by LinkedEntityId';
AggregateResult[] aggrResult = Database.query(query);
SYstem.debug(query);
Map<id,Account> accMap = new Map<id,Account>([Select id, name, Attachment_Increment__c from Account where id in:sObjectIds]);
for(AggregateResult ar: aggrResult){
Object sObjectId = ar.get('LinkedEntityId');
if(accMap.containsKey((id)sObjectId)){
Account acc = accMap.get((id)sObjectId);
//acc.Attachment_Increment__c = (Integer)ar.get('expr0');
acc.Attachment_Increment__c=1*-1;
acc.Attachment_Edited__c=True;
System.debug('(Integer)ar.get(expr0)'+(Integer)ar.get('expr0'));
tempAccounts.put(acc.id,acc);
accountsToUpdate.add(acc);
}
}
for(Account acc: accMap.values()){
if(!tempAccounts.containsKey(acc.id)){
Account a = acc;
acc.Attachment_Increment__c=1*-1;
a.Attachment_Edited__c=True;
accountsToUpdate.add(acc);
}
}
try{
update accountsToUpdate;
}catch(Exception e){
System.debug('Error Message: '+e.getMessage()+' trace: '+e.getStackTraceString());
}
}
}