You need to sign in to do that
Don't have an account?
Sajjan T S
Can someone help me writing a test class for the below?
Global class OldCaseDeleter implements Database.Batchable<SObject> {
Public List<SObject> allRecords;
Global Database.QueryLocator start(Database.BatchableContext BC) {
String query = 'SELECT Id FROM Case WHERE CreatedDate = today and Exempt_From_Deletion__c = false';
System.debug('query -> ' + query);
return Database.getQueryLocator(query);
}
Global void execute(Database.BatchableContext BC, List<SObject> records) {
List<Case> caseList = (List<Case>)records;
Set<Id> caseID = new Set<Id>();
for(Case c: caseList) {
caseId.add(c.Id);
}
List<ContentDocumentLink> files = [SELECT ContentDocumentId, LinkedEntityId
From ContentDocumentLink
WHERE LinkedEntityId IN :caseId];
Set<Id> conId = new Set<Id>();
for(ContentDocumentLink cd : files) {
conId.add(cd.ContentDocumentId);
}
List<ContentDocument> files1 = [SELECT Id, Title
FROM ContentDocument
WHERE Id IN : conId];
allRecords = new List<SObject>();
allrecords.addAll((List<SObject>)files1);
allrecords.addAll((List<Case>)caseList);
Delete allrecords;
}
Global void finish(Database.BatchableContext BC) {
}
}
Public List<SObject> allRecords;
Global Database.QueryLocator start(Database.BatchableContext BC) {
String query = 'SELECT Id FROM Case WHERE CreatedDate = today and Exempt_From_Deletion__c = false';
System.debug('query -> ' + query);
return Database.getQueryLocator(query);
}
Global void execute(Database.BatchableContext BC, List<SObject> records) {
List<Case> caseList = (List<Case>)records;
Set<Id> caseID = new Set<Id>();
for(Case c: caseList) {
caseId.add(c.Id);
}
List<ContentDocumentLink> files = [SELECT ContentDocumentId, LinkedEntityId
From ContentDocumentLink
WHERE LinkedEntityId IN :caseId];
Set<Id> conId = new Set<Id>();
for(ContentDocumentLink cd : files) {
conId.add(cd.ContentDocumentId);
}
List<ContentDocument> files1 = [SELECT Id, Title
FROM ContentDocument
WHERE Id IN : conId];
allRecords = new List<SObject>();
allrecords.addAll((List<SObject>)files1);
allrecords.addAll((List<Case>)caseList);
Delete allrecords;
}
Global void finish(Database.BatchableContext BC) {
}
}
Try the following code, I hope it may be helpful for you:
@isTest
public class OldCaseDeleter_Test {
@isTest
public static void OldCaseDeleter_Test_Method(){
Case con = new Case();
con.Status='New';
con.Origin='Phone';
con.Exempt_From_Deletion__c=false;
insert con;
ContentVersion contentVersionInsert = new ContentVersion(
Title = 'Test',
PathOnClient = 'Test.jpg',
VersionData = Blob.valueOf('Test Content Data'),
IsMajorVersion = true
);
insert contentVersionInsert;
ContentVersion contentVersionSelect = [SELECT Id, Title, ContentDocumentId FROM ContentVersion WHERE Id = :contentVersionInsert.Id LIMIT 1];
List<ContentDocument> documents = [SELECT Id, Title, LatestPublishedVersionId FROM ContentDocument];
ContentDocumentLink contentlink=new ContentDocumentLink();
contentlink.LinkedEntityId=con.id;
contentlink.ShareType= 'V';
contentlink.ContentDocumentId=documents[0].Id;
contentlink.Visibility = 'AllUsers';
insert contentlink;
Test.startTest();
OldCaseDeleter x = new OldCaseDeleter();
Database.executeBatch(x);
Test.stopTest();
}
}
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi
All Answers
Try the following code, I hope it may be helpful for you:
@isTest
public class OldCaseDeleter_Test {
@isTest
public static void OldCaseDeleter_Test_Method(){
Case con = new Case();
con.Status='New';
con.Origin='Phone';
con.Exempt_From_Deletion__c=false;
insert con;
ContentVersion contentVersionInsert = new ContentVersion(
Title = 'Test',
PathOnClient = 'Test.jpg',
VersionData = Blob.valueOf('Test Content Data'),
IsMajorVersion = true
);
insert contentVersionInsert;
ContentVersion contentVersionSelect = [SELECT Id, Title, ContentDocumentId FROM ContentVersion WHERE Id = :contentVersionInsert.Id LIMIT 1];
List<ContentDocument> documents = [SELECT Id, Title, LatestPublishedVersionId FROM ContentDocument];
ContentDocumentLink contentlink=new ContentDocumentLink();
contentlink.LinkedEntityId=con.id;
contentlink.ShareType= 'V';
contentlink.ContentDocumentId=documents[0].Id;
contentlink.Visibility = 'AllUsers';
insert contentlink;
Test.startTest();
OldCaseDeleter x = new OldCaseDeleter();
Database.executeBatch(x);
Test.stopTest();
}
}
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi
Thanks for your response.
This works perfectly.
Warm Regards,
Sajjan