You need to sign in to do that
Don't have an account?
Sajjan T S
Query Cases created before 18 months with related files (not attachments)
I am trying to write a batch apex to schedule and delete cases that were created before 18 months along with the related files. Can someone help me with formulating the query, please?
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) {
}
}
All Answers
Greetings to you!
You can use 'SELECT Id,CreatedDate FROM Case WHERE CreatedDate < Last_N_Months:18' query which will Returns Id's for all Case records created BEFORE the last 18 months
Please try the below code, I have tested in my org and it is working fine. Kindly modify the code as per your requirement.
Batch Apex:
Now, We have the batch class ready and it has to be in a schedulable context in-order to schedule the batch
Scheduled Apex:
Finally, schedule the batch class by executing anonymous code from either developer console or apex, the minimum is 1 hour:
// Cron EXP for hourly schedule:
I hope it helps you.
Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.
Thanks and Regards,
Khan Anas
Thanks for your response.
The issue with this query (SELECT Id,CreatedDate FROM Case WHERE CreatedDate < Last_N_Months:18') is that I would be able to get only Cases and not related Files. In the case of Attachments, it will be deleted if the related Case is deleted. However, with Files, it would still remain even when the corresponding Case is deleted. So, I need a query to SELECT all the cases along with the related files and delete them altogether.
Hope I'm making sense.
Cheers,
Sajjan
You can fetch the files related to Cases using ContentDocumentLink by filtering through LinkedEntityId, where LinkedEntityId is Case Id. Then delete Cases and Files together.
Try like this:
Regards,
Khan Anas
This helps.
Thank you so much :-)
Warm Regards,
Sajjan
I am getting this error - 15:31:20:273 USER_DEBUG [16]|DEBUG|Delete failed. First exception on row 1 with id 06A6F00000HpdHMUAZ; first error: ENTITY_IS_DELETED, entity is deleted: []
The problem is when I run this query - [SELECT ContentDocumentId, LinkedEntityId FROM ContentDocumentLink WHERE LinkedEntityId IN :caseId], this is returned -{"ContentDocumentId":"0696F00000EBQkyQAH","LinkedEntityId":"5006F00002HJAMhQAP","Id":"06A6F00000HpdHMUAZ"}
But the case and the related files aren't getting deleted.
Can you please help me with this?
Warm Regards,
Sajjan
allOrNone-> If you specify false for this parameter and a record fails, the remainder of the DML operation can still succeed. This method returns a result object that can be used to verify which records succeeded, which failed, and why.
Also, use FOR UPDATE clause in SOQL query: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_locking_statements.htm
However, I suggest you to use Iterable in a batch to delete the records. Please refer to the below links which might help you.
https://salesforce.stackexchange.com/questions/81284/delete-records-from-multiple-objects-via-apex-batch-class
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) {
}
}