You are here:
Sample Apex Code for Server-Side Document Generation
Here's a sample code that demonstrates how to generate documents on the server by inserting a DocumentGenerationProcess record. You can generate a document on the server either by directly passing token data, or by sending document template details and letting Salesforce fetch token data automatically.
Directly Pass Token Data
Use this approach to when you want to define and control the token values that Salesforce uses during the document generation request. See DocumentGenerationProcess
DocumentGenerationProcess documentGeneration = new DocumentGenerationProcess();
documentGeneration.Type = 'GenerateAndConvert';
documentGeneration.RequestText = JSON.serialize(new Map<String, Object>{
'keepIntermediate' => true,
'title' => 'Quote Details',
'templateContentVersionId' => '068Ws00000BXC2aIAH'
});
documentGeneration.ReferenceObject = '001Ws00003FrP4dIAF';
documentGeneration.TokenData = JSON.serialize(new Map<String, String>{
‘Name => ’John P’,
});
documentGeneration.DocGenApiVersionType = 'Advanced';
insert documentGeneration;Send Document Template Details to Fetch Token Data Automatically
Use this approach to automatically resolve token data based on the document template’s configuration. When you use DocumentInputType = DocumentTemplate, Salesforce automatically resolves token data based on the template’s configured data mappings, such as DataRaptor Extracts.
DocumentGenerationProcess documentGeneration = new DocumentGenerationProcess();
documentGeneration.Type = 'GenerateAndConvert';
documentGeneration.RequestText = JSON.serialize(new Map<String, Object>{
'keepIntermediate' => true,
'title' => 'Quote Details',
'templateContentVersionId' => '068Ws00000BXC2aIAH'
});
documentGeneration.ReferenceObject = '001Ws00003FrP4dIAF';
documentGeneration.DocumentInputType = 'DocumentTemplate';
documentGeneration.DataRaptorInput = '{"Id":"0017Y00001jt9cVQAQ"}'; // Pass ID Which should be the Input ID to the Extract DataMapper
documentGeneration.DocGenApiVersionType = 'Advanced';
documentGeneration.DocumentTemplateId = '2dtWs000000KELNIA4';
insert documentGeneration;
Send Token Data Using a JSON File
Use this approach when your token data is stored in a JSON file. Upload the JSON file to Salesforce Files, and then set the TokenDataContentDocumentId field on the DocumentGenerationProcess record to the uploaded JSON file’s ContentDocumentId. During server-side document generation, Salesforce reads the JSON from the file and uses it as token data.
DocumentGenerationProcess documentGeneration = new DocumentGenerationProcess();
documentGeneration.Type = 'GenerateAndConvert';
documentGeneration.RequestText = JSON.serialize(new Map<String, Object>{
'keepIntermediate' => true,
'title' => 'Quote Details',
'templateContentVersionId' => '068Ws00000BXC2aIAH'
});
documentGeneration.ReferenceObject = '001Ws00003FrP4dIAF';
// Uploaded JSON File ContentDocument ID
documentGeneration.TokenDataContentDocumentId = '069WU00000ImN3BYAV';
documentGeneration.DocGenApiVersionType = 'Advanced';
insert documentGeneration;Merge Generated PDFs
To combine generated PDFs into a single file, use Apex to generate each PDF, and then start a merge request.
Create a document template in Document Template Designer for each file to merge, and insert a DocumentGenerationProcess record for each template with the relevant object ID, template ID, request text, and token data, as shown in the earlier sections. Capture each PDF's ID from the response field on the record. When all the PDFs are ready, insert a DocumentGenerationProcess record with Type set to MergePDF and the source PDF IDs in PdfDocIdentifiersList, and set ReferenceObject to the record where Salesforce attaches the merged PDF. Alternatively, you can merge any PDFs within Salesforce using the same steps and running the sample apex code below.
String requestText = '{\"title\":\"NewDocDemoTestDevConsole\"}';
String type = 'MergePDF';
String pdfDocList = '068SG000000IhrKYAS,068SG000000Imm9YAC';
DocumentGenerationProcess request = new DocumentGenerationProcess();
request.Type = type;
request.RequestText = requestText;
request.DocGenApiVersionType = 'Advanced';
request.PdfDocIdentifiersList = pdfDocList;
insert request;

