Introduction
The DocumentSDK, also referred to as the S-Docs SDK is an essential tool for developers that aims to streamline document generation and distribution methods originating from within organizations. This SDK provides methods to simplify the process of creating documents and emails. It empowers developers to effortlessly integrate document generation and email creation features into their applications. The methods generateDoc and generateEmail facilitate the creation of documents and email content, providing developers with essential tools to enhance user experiences.
Explore the capabilities of the S-Docs SDK (DocumentSDK) and leverage its potential to streamline document-related tasks and elevate document management with ease within any org. Refer to the documentation below for more detailed information on implementation and usage.
The following is an overview of its capabilities:
DocumentSDK Methods
PDF Document Generation & S-Docs Email SDK
Supported Contexts
The following contexts are supported:
- Apex Invocable Class (that can be called from a Flow)
- Apex Queueable
- Apex Batch
- @future Apex method
- Visualforce Apex Controller (Custom and Controller extensions)
- AuraEnabled Apex method
- Apex class that supports an OmniScript
Use cases
With this SDK method you can generate documents from your own business processes as well as custom UI components. Some of the use cases that we anticipate to leverage this capability is as follows:
- Generate a document in batch from an existing Apex Batch or Apex Queueable class
- Generate a document from an existing UI component: Aura or Lightning Web Component
- Generating a document from a custom Visualforce page
- Generating a document from an OmniScript
Usage
In your apex class (Lightning Web component controller, Apex invocable, Queueable or Apex batch classes) you would invoke the above SDK method like below:
String jsonResp = SDOC.DocumentSDK.generateDocument('<templateId>','recordId'); Map<String,String> respAsObject = JSON.deserialize(jsonResp, Map<String,String>.class)
Method Signature
generateDocument(templateId, recordId, generationOptions)
Parameters
templateId | (Type: Id): The ID of the template used for generating the document.
recordId | (Type: Id): The ID of the base record object.
Returns
Type: String
Document information associated with the generated PDF in JSON format.
Method Signature
generateDocument(Id templateId, Id recordId, GenerationOptions generationOptions)
Parameters
templateId | (Type: Id): The ID of the template used for generating the document.
recordId | (Type: Id): The ID of the base record object.
generationOptions | (Type: SDOC.GenerationOptions)
-
documentOptions | (Type: SDOC.DocumentOptions)
-
userInputs | (Type: SDOC.UserInput)
-
-
emailOptions | (Type: SDOC.EmailOptions)
-
template | (Type: String)
-
sObjectId | (Type: Id)
-
toAddresses | (Type: List<String>)
-
ccAddresses | (Type: List<String>)
-
bccAddresses | (Type: List<String>)
-
replyToEmail | (Type: String)
-
orgWideEmailAddressId | (Type: Id)
-
emailSubject | (Type: String)
-
sdocIdsToAttach | (Type: List<Id>)
-
contentVersionsIdsToAttach | (Type: List<Id>)
-
-
actions | (Type:SDOC.Action)
-
name | (Type: String)
-
parameters | (Type: Map<String, Object>)
-
-
overrideActions | (Type: Boolean)
-
skipFileSave | (Type: Boolean)
The return JSON will have the following structure:
{ "id": "<ID of SDOC__SDoc__c record created>", "contentDocumentId": "Id of the corresponding ContentDocument record that was created", "templateId": "<ID of Template passed in as the parameter>", "recordId": "<ID of base record passed in as the parameter>", "createdDate": "<Date of document created>", "attachmentName":"<Name of file created>", "allowEdit":"<Boolean>", "ssignEnabled": "<Boolean>", }
Limitations:
- Currently only PDF and PDF-UPLOAD formats are supported.
- You can only have 1 invocation of generateDoc per Apex transaction. I.e. if you invoke it multiple times in the same transaction you will get a LimitsException
- If there is an error in generating the document, there is an Exception thrown that needs to be handled to introspect into the error details.
- Runtime prompts based templates aren’t supported in the SDK.
HTML Email Generation
Supported Contexts
The following contexts are supported:
- Apex Invocable Class (that can be called from a Flow)
- Apex Queueable
- Apex Batch
- @future Apex method
- Visualforce Apex Controller (Custom and Controller extensions)
- AuraEnabled Apex method
- Apex class that supports an OmniScript
Use cases
Since S-Docs email templates have the same ability to merge data fields from base objects, you can leverage this SDK to have richer email content that what the standard email templates support.
Usage
in your apex class (Lightning Web component controller, Apex invocable, Queueable or Apex batch classes) you would invoke the above SDK method like below:
String emailBody = SDOC.DocumentSDK.generateEmail('<templateId>','objectId'); Messaging.SingleEmailMessage theEmail = new Messaging.SingleEmailMessage(); theEmail.setHtmlBody(emailBody); ... .. Messaging.sendEmail(new Messaging.SingleEmailMessage[] {semail});
Method Signature
generateEmail(Id templateId, Id objectId)
Parameters
templateId | (Type: Id): The ID of the template used for generating the email.
objectId | (Type: Id): The ID of the base record object.
Returns
Type:
String
String containing the email body in HTML format.
Generate Documents in Batch & Combine Documents Handler
Supported Contexts
The following contexts are supported:
- Apex Invocable Class (that can be called from a Flow)
- Apex Queueable
- Apex Batch
- @future Apex method
- Visualforce Apex Controller (Custom and Controller extensions)
- AuraEnabled Apex method
- Apex class that supports an OmniScript
Use cases
Automate document generation across multiple records from multiple templates to streamline workflows that require batch processing.
Usage
In your apex class (Lightning Web component controller, Apex invocable, Queueable or Apex batch classes) you would invoke the above SDK method like below:
Id batchJobId = SDOC.DocumentSDK.generateDocsInBatch( {List<Id> recordIds}, {Set<Id> templateIds}, {Integer batchSize} };
Method Signature
generateDocsInBatch(baseRecords, templateIds, n)
Parameters
baseRecordIds | (Type: Id): The list of the records used for generating documents.
templateIds | (Type: Id): The set of IDs of templates to generate.
batchSize | (Type: Integer): The size of each batch in a job. (Default value is 5)
batchActions | (Type: List): List of batch actions to perform on the generated documents.
CombinedDocumentHandler
The CombinedDocumentHandler is built-in functionality that combines all generated documents into one PDF.
Returns
Type: ID
The ID of the AsyncApexJob record returned by the system.
Example Code
String generatedDocumentAsString = SDOC.DocumentSDK.generateDocsInBatch( new List<Id> { accounts[0].Id, accounts[1].Id }, new Set<Id> { accountReport.Id }, 5, new List<SDOC.BatchAction> { new SDOC.CombinedDocumentHandler() } );
Explanation
- recordIds: The method takes a list of record IDs (accounts[0].Id, accounts[1].Id) for which the documents need to be generated.
- templateIds: It uses a set of template IDs (accountReport.Id) specifying the templates for document generation.
- batchSize: The batch size is set to 5, meaning it will process 5 documents at a time.
- batchActions: The CombinedDocumentHandler is added to the list of batch actions, which will combine all generated documents into a single PDF.
The DocumentSDK empowers developers to effortlessly integrate document generation and distribution capabilities into their end user workflows. The methods outlined in this article aim to facilitate the a wide range of processes suitable for any org, providing admins and developers with an essential and flexible toolkit that helps enhance user experience and boost operation efficiency.