You are here:
Salesforce Document Generation Utilities
Salesforce Document Generation Utilities provide tools to help manage and optimize the document generation process. These utilities include the Document Generation Process Cleaner Utility, which permanently deletes old document generation process records and associated data to free up storage space. This utility uses specific Apex classes that can be created and run in the Developer Console to clean up records within a defined timeframe. Additionally, Salesforce offers a feature to run a scheduled job to terminate long-running document generation requests, ensuring system performance by automatically canceling requests that exceed a specified time limit. These utilities help administrators maintain an efficient and performant document generation environment.
Required Editions
| Available in: Lightning Experience |
| Available in: Professional, Enterprise, Unlimited, and Developer Editions |
- Document Generation Process Cleaner Utility
The Document Generation Process Cleaner Utility permanently deletes records within a specified time frame to create space for content document generation in Salesforce. You can use the utility to delete the document generation process records and their associated token data content documents or only the token data content documents. The utility can clean up 100,000 records and their respective token data files of 1MB size in about 2 hours. - Terminate Long-Running Document Generation Requests
Use the TerminateDocGenRequestCronJob scheduled job to maintain server-side document generation performance by preventing long-running requests from affecting system efficiency.
Document Generation Process Cleaner Utility
The Document Generation Process Cleaner Utility permanently deletes records within a specified time frame to create space for content document generation in Salesforce. You can use the utility to delete the document generation process records and their associated token data content documents or only the token data content documents. The utility can clean up 100,000 records and their respective token data files of 1MB size in about 2 hours.
During server-side document generation, the TokenDataContentDocumentId field in the Document Generation Process entity is used for passing token data that’s more than 131k characters. The TokenDataContentDocumentId field references ContentDocument records that hold the JSON token data as .txt or .json files.
The utility calls cleanDocumentGenerationProcessAndContentDocument and cleanContentDocument Apex class methods.
Deleting content documents that are part of an in-progress document generation request can cause the document generation request to fail.
It might not be possible to retrieve the deleted data from the recycle bin by the calling Apex class methods cleanDocumentGenerationProcessAndContentDocument and cleanContentDocument.
- 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. - Run the Document Generation Process Cleaner Utility in Developer Console
Use the Document Generation Process Cleaner Utility to either delete the document generation process records and their associated token data content documents or only the token data content documents.
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.
Run the Document Generation Process Cleaner Utility in Developer Console
Use the Document Generation Process Cleaner Utility to either delete the document generation process records and their associated token data content documents or only the token data content documents.
-
Click
to open the quick access menu, and then select Developer
Console.
- From the Debug menu, select Open Execute Anonymous Window.
-
Copy one of the statements in the Enter Apex Code dialog and specify the start
and end times.
-
To call cleanDocumentGenerationProcessAndContentDocument:
DocumentGenerationProcessCleanerUtility obj = new DocumentGenerationProcessCleanerUtility(); DateTime starttime= DateTime.newInstanceGmt(2022,11,08,18,49,48); DateTime endtime= DateTime.newInstanceGmt(2022,11,09,01,20,48); obj.cleanDocumentGenerationProcessAndContentDocument(starttime,endtime); -
To call cleanContentDocument use:
DocumentGenerationProcessCleanerUtility obj = new DocumentGenerationProcessCleanerUtility(); DateTime starttime= DateTime.newInstanceGmt(2022,11,08,18,49,48); DateTime endtime= DateTime.newInstanceGmt(2022,11,09,01,20,48); obj.cleanContentDocument(starttime,endtime);
-
To call cleanDocumentGenerationProcessAndContentDocument:
- Select the statement you pasted.
-
Click Execute Highlighted.
You can view the batch job ID and the count of successfully deleted records in the log files.
- Close the Developer Console.
Terminate Long-Running Document Generation Requests
Use the TerminateDocGenRequestCronJob scheduled job to maintain server-side document generation performance by preventing long-running requests from affecting system efficiency.
| REQUIRED EDITIONS |
|---|
| Available in: Lightning Experience |
| Available in: Professional, Enterprise, Unlimited, and Developer Editions with Salesforce Document Generation |
| USER PERMISSIONS NEEDED | |
|---|---|
| To run the cron job: | CLM Admin |
Prevent document generation requests from running indefinitely by turning on the time-out setting. Specify a time limit for document generation requests. Any individual or batch document generation requests that exceed the specified time limit, but are still in progress, are terminated and marked as failed. The default timeout is set to 6 hours.
- From Setup, in the Quick Find box, enter Document Generation, and then select Document Generation Settings.
- Click the dropdown next to the Docgen label, and then select Edit.
- Turn on the Enable InProgress DocGen Request Time Out setting.
- To specify the time interval (in hours) after which the long running document generation requests are terminated, enter a number in InProgress DocGen Request Time Out (hrs).
-
Click Continue.
The TerminateDocGenRequestCronJob is added to the Scheduled Jobs list.
Individual or batch document generation requests that exceed your specified time limit are terminated and marked as failed.

