You need to sign in to do that
Don't have an account?

Need a help on code coverage for before delete apex trigger on file
Hi All,
Please help me to write a test class for the below apex trigger.
Thanks in advance.
here is the code
trigger PreventFileConDoc on ContentDocument (before delete) {
// for(ContentDocumentLink conl : trigger.old){
// conl.adderror('ContentDocumentLink Cannot be deleted');
// }
Id profileId = userinfo.getProfileId();
String profileName = [Select Id,name from Profile where Id =: profileId].Name;
if(Trigger.isBefore && Trigger.isDelete){
Map<Id, ContentDocument> mapContentDocument = new Map<Id, ContentDocument>([Select Id from ContentDocument where Id in: trigger.oldMap.keyset()]);
for(ContentDocument cd : Trigger.Old){
if (profileName != 'System Administrator') {
if(mapContentDocument.containsKey(cd.Id)){
cd.adderror('ContentDocument Cannot be deleted');
system.debug('testing inside loop'+cd);
}
}
}
}
}
Thank you,
Bheem
Please help me to write a test class for the below apex trigger.
Thanks in advance.
here is the code
trigger PreventFileConDoc on ContentDocument (before delete) {
// for(ContentDocumentLink conl : trigger.old){
// conl.adderror('ContentDocumentLink Cannot be deleted');
// }
Id profileId = userinfo.getProfileId();
String profileName = [Select Id,name from Profile where Id =: profileId].Name;
if(Trigger.isBefore && Trigger.isDelete){
Map<Id, ContentDocument> mapContentDocument = new Map<Id, ContentDocument>([Select Id from ContentDocument where Id in: trigger.oldMap.keyset()]);
for(ContentDocument cd : Trigger.Old){
if (profileName != 'System Administrator') {
if(mapContentDocument.containsKey(cd.Id)){
cd.adderror('ContentDocument Cannot be deleted');
system.debug('testing inside loop'+cd);
}
}
}
}
}
Thank you,
Bheem
All Answers
Hi neeli,
please run the below test.
Please mark it as the best Answer if it helps you
Thank You
Thank you for your response,
With your code I got the error 'dml operation delete not allowed on contentversion'
I changed code something like below and then covers code coverage as 75%.
@isTest
public class Test_PreventFileDeletion {
static testMethod void testMethod1()
{
ContentVersion contentVersion = new ContentVersion(
Title = 'Penguins',
PathOnClient = 'Penguins.jpg',
VersionData = Blob.valueOf('Test Content'),
IsMajorVersion = true
);
insert contentVersion;
List<ContentDocument> documents = [SELECT Id, Title, LatestPublishedVersionId FROM ContentDocument];
try{
delete documents;
}
catch(exception e){}
}
}
Thank you,
Bheem