You are here:
Apex Classes for Document Generation Process Cleaner Utility
Use the DocumentGenerationProcessCleanerUtility and BatchDeleteHelper Apex classes to create storage space for content document generation in Salesforce. Create these Apex classes using the sample code we've provided. If required, you can modify the statusList and batchSize fields.
The utility calls these Apex class methods:
cleanDocumentGenerationProcessAndContentDocument deletes all DocumentGenerationProcess records and their associated token data contentDocument within a specified time frame and status. Only records that use the TokenDataContentDocumentID field are considered.
cleanContentDocument deletes all token data contentDocument associated with the DocumentGenerationProcess records within a specified time frame and status. Only records that use the TokenDataContentDocumentID field are considered.
Note cleanContentDocument method deletes only the associated contentDocuments. After deletion, the corresponding TokenDataContentDocumentId field value is set to null in DocumentGenerationProcess records.
The Apex class methods require these parameters.
| Parameter | Description |
|---|---|
| Input | Class-level configurations:
Method-level configurations:
|
| Output |
|
DocumentGenerationProcessCleanerUtility Apex Code
The Document Generation Process Cleaner Utility permanently deletes records within a specified time frame to create space for content document generation in Salesforce.
Create the following class in your org, and modify it as needed.
public class DocumentGenerationProcessCleanerUtility {
List<String> statusList = new List<String>{'Success', 'Failure'}; // list of statuses considered for deletion
Integer batchSize = 2000; // batch size for deletion used in BatchDeleteHelper, 2000 is max batch size possible
String query = 'select Id, TokenDataContentDocumentId from DocumentGenerationProcess WHERE LastModifiedDate >= :dstart and LastModifiedDate <= :dend and TokenDataContentDocumentId != NULL and status IN :statusList';
// Given 'dstart','dend' datetime, delete all documentGenerationProcess records that use TokenDataContentDocument field and have LastModifiedDate = ['dstart','dend'], also delete associated contentDocuments
public void cleanDocumentGenerationProcessAndContentDocument(Datetime dstart,Datetime dend) {
System.Debug('Deleting DocumentGenerationProcess records along with used contentDocuments ...');
ID batchjobid = Database.executeBatch(new BatchDeleteHelper(false,query,dstart,dend, statusList), batchSize);
System.debug('Started batch job with ID: ' + batchjobid);
}
// Given 'dstart','dend' datetime, for all documentGenerationProcess records that use TokenDataContentDocument field and have LastModifiedDate = ['dstart','dend'] delete associated contentDocuments only
public void cleanContentDocument(Datetime dstart,Datetime dend) {
System.Debug('Deleting only contentDocuments associated with DocumentGenerationProcess records ...');
ID batchjobid = Database.executeBatch(new BatchDeleteHelper(true,query,dstart,dend,statusList), batchSize);
System.debug('Started batch job with ID: ' + batchjobid);
}
}
Batch Delete Helper Apex Class
The BatchDeleteHelper is used internally for a batch class.
Create the following class in your org, and modify it as needed.
public class BatchDeleteHelper implements Database.Batchable<sObject>, Database.Stateful {
String query;
Datetime dstart;
Datetime dend;
List<String> statusList;
Boolean isOnlyContentDocumentDelete;
Integer deletedRecordCount = 0;
public BatchDeleteHelper(Boolean isOnlyContentDocumentDeleteArgument,String queryArgument, Datetime dstartArgument, Datetime dendArgument,List<String> statusListArgument){
query = queryArgument;
dstart = dstartArgument;
dend = dendArgument;
statusList = statusListArgument;
isOnlyContentDocumentDelete = isOnlyContentDocumentDeleteArgument;
}
public Database.QueryLocator start(Database.BatchableContext BC){
System.Debug('Querying data ..');
return Database.getQueryLocator(query);
}
public void execute(Database.BatchableContext BC, List<DocumentGenerationProcess> scope){
Database.DeleteResult[] deleteResultList;
if(isOnlyContentDocumentDelete){
// need to create ContentDocument Object Set for ContentDocumentOnly deletion
Set<ContentDocument>contentDocumentSet = new Set<ContentDocument>();
for(DocumentGenerationProcess documentgenerationprocess:scope){
String contentDocumentId = documentgenerationprocess.TokenDataContentDocumentId;
if(contentDocumentId!=null)
contentDocumentSet.add(new ContentDocument(Id = contentDocumentId));
}
deleteResultList = Database.delete(new List<ContentDocument>(contentDocumentSet), false);
}
Else{
deleteResultList = Database.delete(scope, false);
}
for(Database.DeleteResult deleteResult : deleteResultList) {
if (deleteResult.isSuccess()) {
// System.debug('Successfully deleted record with ID: ' + deleteResult.getId());
deletedRecordCount++;
}
}
}
public void finish(Database.BatchableContext bc){
System.Debug('Total ' + deletedRecordCount + ' records deleted successfully');
}
}
To run these utility classes, see Run the Document Generation Process Cleaner Utility in Developer Console.

