Document Automation with S-Docs Jobs – Lightning

Please note that automation is only available to S-Docs Unlimited Edition customers for an additional fee. Click here to contact our team for more information.


If you're looking for a way to automate your document generation process--perhaps you want a document to be automatically generated and emailed when a field value has changed or a date has passed--this document will help you configure S-Docs to meet your requirements using the S-Docs Jobs object.

For example, when a user changes an opportunity stage field to “send quote,” you can configure S-Docs to generate a PDF quote along with a customized cover letter and email it to the opportunity contact. Users would not need to click on any buttons or choose any templates. Whenever the field value is changed, even from a mobile device, the process is invoked and the documents are generated and optionally emailed.

This document will show you how to use S-Docs Jobs to automate document generation in three different ways: with Salesforce Process Builder, apex Triggers, and Salesforce Flows.

If you are looking for a way to generate multiple S-Docs (in batch) from an object list view, you can accomplish this with S-Docs Jobs as well. For example, a user could select multiple records at once from a list view and send each record a custom invitation email to an event. The possibilities to further automate and distribute your documents are unlimited. Please refer to this documentation for more information about batch document generation.

Automatic Document Generation with S-Docs Jobs

An S-Docs Job is a simple, intuitive Salesforce object designed for automatically generating (and optionally emailing) documents with your custom code, Salesforce Processes, Salesforce Flows, or other integrations.

For use with your custom code, Salesforce Processes, or Salesforce Flows, the API name for S-Docs Jobs is SDOC__SDJob__c, and the API names for some of its fields can be found in the following list. You'll use these fields later when configuring your document automation.

To automatically generate S-Docs with S-Docs Jobs, you'll be configuring your Salesforce Process, custom code, or Flow to do at least five things:

  1. Create an S-Docs Job record
  2. Specify the ID of the record you would like to generate these documents for
  3. Specify this record’s object API name (Opportunity, Account, etc.)
  4. Specify the name of the template you would like to use for document generation
  5. Optionally fill in the fields for emailing these documents. Finally, you will need to set the SDOC__Start__c field for this Job record to “true” in your apex code or Salesforce flow to run this Job (i.e. to generate and optionally email the specified documents)

Before we see how to use S-Docs Jobs for automatic document generation, we’ll see what the Job records themselves look like.

Behind the Scenes of S-Docs Jobs

When one of the three methods (Salesforce Processes, apex code, or Flow) creates an S-Docs Job, it is stored as an S-Docs Jobs record. To view these records, navigate to the App Launcher, scroll down to All Items, and click S-Docs Jobs.

All S-Docs Jobs Record appear here. In most (if not all) cases, we will not be creating Jobs records with the New button here; the records here should be created by your Salesforce Process, apex triggers, Salesforce flows, etc. However, you can delete logs here by clicking the arrow at the end of each log and then clicking Delete (though this may also be done programmatically).

You can open a job record by clicking on the blue SDJOB number. The record will show you things like the IDs of the templates used to generate it (i.e. the Doclist), the ID of the record it generated documents for (i.e. the Object ID), and other relevant Jobs fields.

The documents the Job generated will also be included in the record under the Job Execution Details section. You can click on their icons to view them.

You don't need to find the job record to view documents generated by a job, however. A related list for these documents will be automatically created on the page of the record you generated the documents for. For example, if we created an apex trigger that would create a Job to generate three documents whenever an account record was created, we would see those three documents on that account record’s related tab like so:

Note that these documents would still exist in the S-Docs related list on the account record page even if we deleted the Job that created these documents. If you wanted to delete these documents for good, you could delete them from the S-Docs related list.

Now that we’ve seen how S-Docs Jobs work behind the scenes, we can see how we might leverage their functionality with Salesforce Processes, apex triggers, and Salesforce flows.

Use Case: Automatically Generating & Emailing a Welcome Letter Whenever an Account is Created

Consider the following use case: whenever an account is created, we would like S-Docs to generate a welcome letter for that account, and then email it to a contact associated with that account. This process can be automated in no time using S-Docs Jobs! In the three sections following this one, we will explore how to accomplish this in Process Builder, apex triggers, and in Salesforce Flows. In all three of these scenarios, we will have to fill in the following fields in order to automate this particular case; you'll see how to use these fields once we get into each scenario.

Fields for our Use Case
SDOC__Start__c (Start Job)This will be set to ‘true’ so that the specified documents will automatically generate (and in this case, automatically email, as we have specified them to do so using the Job’s email fields) upon the creation of this particular S-Doc Job record.

SDOC__SendEmail__c (Send Email)If we set this to ‘0,’ an email will not be sent. If we set it to ‘1,’ this S-Doc Job record will email the document it generated. Thus, we will be setting this to ‘1.’

For this to work properly, our Doclist needs to include an S-Doc HTML email template. In our example, this template will use a merge field in the Email To field (found under the Email Settings tab of the template editor) that will be dynamically populated with the appropriate contact's email address. Then, each Job record would generate a welcome letter PDF, generate an email using the HTML email template, attach the welcome letter PDF to that email, and then send that email.

SDOC__Oid__c (Object ID): This is the Object ID field, i.e. the place where we specify which object our Process/apex trigger/Flow should pull record IDs from. In our example, we will choose Account, since we want our process to use Account record IDs.

SDOC__ObjAPIName__c (Object API Name)This is the API name of our objects. In our example, we’re creating this trigger for new accounts, so we store Account in this field.

SDOC__Doclist__c (Doclist)This is the comma-delimited list of S-Docs template names that we will be using to generate our documents. Make sure there aren’t any spaces in your comma-delimited list. In our example use case, Welcome Letter is the name of our S-Docs PDF welcome letter template, and Email Template is the name of our S-Docs HTML email template. Thus, we will be using the following Doclist for our example use case Salesforce Process, apexcode, and Salesforce Flow:

Welcome Letter,Email Template

You can also use template IDs for the doclist parameter, however this is not recommended because these IDs will change between sandbox and production orgs, meaning that you will have to re-add the new template IDs into this parameter after transferring to production.

To find the template IDs for use in your Doclist field, go to the template record page for each template you're using.

The template ID can be found in the URL of the template record. You can then copy and paste this ID into your Doclist field for an S-Docs Job in your Process, apex code, Flow, etc.

Note: If your org contains multiple templates with the same name, all of them will generate if one of them is referenced in one of your processes. In this case, you should use the template ID instead.

In the following three sections, we will see how to use S-Docs Jobs with Salesforce Processes, apex triggers, and Salesforce Flows to automatically generate and email a welcome letter whenever an account is created.

Additional Fields

While the example use case in this article only uses 5 fields, S-Docs Jobs also support a number of other fields that you can use to further customize your process.

Email From

Email From allows you to set an org-wide email address as the From address when your S-Docs Job process includes emailing a document.

Email Params

Email Params allows you to add advanced email parameters to your process that are not available to access through standard fields on the S-Docs Job object, such as &useExistingNoContactRecord=true.

Incl. Attachments with Email

Incl. Attachments with Email allows you to specify a comma-delimited list of Salesforce Attachment IDs to be included in the email sent out by the S-Docs Job.

Incl. Documents with Email

Incl. Documents with Email allows you to specify a comma-delimited list of Salesforce Document IDs to be included in the email sent out by the S-Docs Job.

Incl. Files with Email

Incl. Files with Email allows you to specify a comma-delimited list of Salesforce File IDs to be included in the email sent out by the S-Docs Job.

Include All Related Files

Include All Related Files allows you to opt to attach every file related to the base record to the email sent out by the S-Docs Job.

Using S-Docs Jobs with Process Builder

The easiest way to generate and email a welcome letter whenever an account is created is to use an S-Docs Job in a Salesforce Process. From the Setup menu, type "Process Builder" into the Quick Find / Search bar and click Process Builder.

Click New to create a new process.

You can name the process whatever you want, then choose to have this process start when a record changes. Click Save.

Click Add Object, choose Account, and start the process only when a record is created.

Click Save. Then, click Add Criteria and set it to No Criteria, since we want our Job to execute every time a new account record is opened.

Then, click Add Action and choose Create a Record for our Action Type. For the Record Type, select SDoc Job. You may have to type it in to find it.

A Field Values menu will appear below. This is where you will add the five fields described above in the Fields for Our Use Case section. Click Add Row to add each new field.

Then, we click Save and click Activate at the top right of the screen.

All done! S-Docs will now automatically generate and email a welcome letter whenever an account is created.

Using S-Docs Jobs with Apex Triggers

S-Docs Jobs are also simple to use with apex triggers. To get started, click the Setup cog in the upper right corner, then click Developer Console.

Then, navigate to File > New > Apex Trigger.

You can name the trigger whatever you want, then select Account for the sObject.

You'll see the following screen.

You can delete the text here, then copy and paste the following apex trigger into the text field.

trigger SendWelcomeLetter on Account (after insert) {
  List<SDOC__SDJob__c> jobList = new List<SDOC__SDJob__c> {};
  for(Account acct : Trigger.New) {
    SDOC__SDJob__c job =
    new SDOC__SDJob__c(SDOC__Start__c=true,
                      SDOC__Doclist__c='Welcome Letter,Email Template');
  insert jobList;

This simply creates a Job Object in the apex trigger, fills in its fields as previously discussed in the Fields for our Use Case section above, and inserts each Job into our Salesforce database. Note that SDOC__SendEmail__c is a string, so its value must be specified in single quotes. SDOC__Doclist__c and SDOC__ObjApiName__c must be specified in single quotes as well.

SDOC__Oid__c, the Object ID field, is given the value acct.Id. This means that for each account record in our loop, the S-Doc Job created for this particular account record will have this account record’s ID stored in its SDOC__Oid__c field.

Click File > Save to complete your apex Trigger!

Using S-Docs Jobs with Salesforce Flows

This option is a special case; say we don’t want to generate an email letter whenever an account is created anywhere in our org. Rather, we would like to generate and email a welcome letter only when an account record is created in a Salesforce Flow. Using S-Docs Jobs with Salesforce Flows is as simple as using any other object with Salesforce Flows, so this is easy to accomplish. To get to Flow Builder, type "Flows" into the QuickFind / Search bar in the Setup menu, click Flows, then click New Flow.

Create an Autolaunched Flow.

First, we need to create a new variable called “acct.” Select Manager in the Toolbox area, then click New Resource.

For the Resource Type, select Variable. Enter "acct" for the API name, and make sure the Data Type field is set to Text.

Now we need to create two Record Creates: one for an Account and one for an SDoc Job. We'll create the Record Create for an Account first. Navigate back to the Elements tab in the Toolbar, then drag Create Records into the Flow.

Insert the following information:

Label: Create Account
API Name: Create_Account
How Many Records to Create: One
How to Set the Field Records: Use separate variables, resources, and literal values
Object: Account
Field: Name
Value: New Account Example
Variable: {!acct}

Next, we need to add a Record Create for the SDoc Job. Drag Create Records into the flow again. Insert the following information:

Name: Automatically Generate and Email Welcome Letter
Label: Automatically_Generate_and_Email_Welcome_Letter
How Many Records to Create: One
How to Set the Record Fields: Use separate variables, resources, and literal values
Object: SDoc Job

Fields & Values:

SDOC__Start__c: {!$GlobalConstant.True}
SDOC__SendEmail__c: 1 (This specifies that we wish to email the document generated by this Job record (we would type in “0“ (without quotes) if we did not wish to send an email)
SDOC__Oid__c: {!acct} This variable stores the ID of the Account record created when the “Record Create” that we made for an Account executes.
SDOC__ObjApiName__c: Account
SDOC__Doclist__c: [Insert your template names here]

Once you've saved both record creates, attach them together by clicking the circle on "Create Account" and dragging the line to the circle on "Automatically Generate and Email Welcome Letter," and attach "Start" to "Create Account."

At this point, running this flow would result in an account called “New Account Example” being created, and documents for this account would automatically be generated and emailed by the S-Doc Job record created for this account.

If we wanted to, we could now start building a much larger flow involving these two Record Create boxes.

Batch Processing with S-Docs Jobs (Recommended)

S-Docs Jobs are also used to facilitate batch document processing. However, they are used behind the scenes throughout the entire process; batch processing is performed in a simple click-only interface, so you won’t have to worry about creating the Jobs themselves. This feature, in addition to the mass merge feature, is covered in detail in the S-Docs Mass Merge & Batch Document Feature documentation.

Tags: , ,

Was this helpful?