Winter '20 Release Note: If you are using S-Docs below version 4.53, you may experience an "Attempt to de-reference a null object" error when interacting with various forms of automation in S-Docs. In order to fix this bug, you can create a new SDocs Settings custom settings set. To do this, type "Custom Settings" into the Quick Find / Search bar in the Setup menu, and click Custom Settings. Click SDocsSettings, then click Manage at the top of the page. From there, click New. Fill out the following information:

Name: SDocsSettings
ConnectedAppLoginURL:
Production: login.salesforce.com
Sandbox: test.salesforce.com
SD Jobs Batch Size: 30
SD Jobs Move to Top of Flex Queue:

Additionally, ensure that you have a Remote Site Setting for either login.salesforce.com (production), or test.salesforce.com (sandbox).

Introduction

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 Visual Workflows.

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.

Automation without S-Docs Jobs

There are alternative legacy methods of using S-Docs with automation, workflow, and batch processing that don't use S-Docs Jobs. These methods involve the REST API, which is leveraged to invoke document generation programmatically. This powerful feature means that documents can be created in the background (asynchronously) without any user involvement whenever defined criteria are met.

However, S-Docs Jobs can accomplish anything that the legacy method with the REST API can. Thus, for users who haven’t used the legacy method yet, we recommend that you use S-Docs Jobs as your primary solution. The REST API methods are intended as a reference only for users who have already implemented these methods. You can read about the S-Docs REST API here.

Automatic Document Generation with S-Docs Jobs (Recommended)

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, and other integrations. We recommend you use S-Docs Jobs with Process Builder, which can do just about anything workflows can.

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 relevant fields can be found in the following list. You'll need 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 Visual Workflow 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 ID of the S-Docs templates (or template names) 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 Visual Workflows) creates an S-Docs Job, it is stored as an S-Docs Jobs record. To view these records, navigate to All Tabs ("+" icon), then scroll down and click S-Docs Jobs.

All S-Docs Jobs records 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 by opening a job record and clicking Delete (though this may also be done programmatically).


You can open a job record by clicking on the SDJob number on the left. 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. You can click on their icons under the Job Execution Details section 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 in that account record’s related list 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 (Visual Workflows). 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 SentTo" 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'll store Account in this field.

SDOC__Doclist__c (Doclist (Template IDs))This is the comma-delimited list of the IDs of the S-Docs templates 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, a033A00000FWmzh is the ID of our S-Doc template for a welcome letter PDF, and a0236000005b2dM is the ID of our S-Doc template for an HTML email. Thus, we will be using the following Doclist for our example use case Salesforce Process, APEX code, and Salesforce Flow:

a033A00000FWmzh,a0236000005b2dM

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. 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: You can also use the plain text template name in place of the template ID in the doclist field. If you are testing these features in a sandbox org, using the template name will make it easier to transition over to production, since you won't have to change anything in your processes; you would, however, need to update the template IDs, since these are different in sandbox and production orgs.

Additionally, note that if there are multiple templates with the same name, all of them will generate if one of them is referenced in one of your processes.

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.

Run the S-Doc Job under a specified user. One last note before we get started: If we wanted this S-Doc Job to be run as a specified user in any of the following implementations, we would also add the "Run as User" field (SDOC_RunAsUser__c). This field takes a string, specifically the username of the user we want this Job to be run as. For example, say we wanted our new account's welcome letter to be sent from joe@example.com, even if joe@example.com was not the user who created the account. To do this, we would add the "Run as User" field and enter joe@example.com.

Using the "Run as User" field requires setting a few things up in order to work without any preauthorization (as required by Salesforce). Click here to read about this setup.

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 in the upper right corner 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.

Additionally, if we wanted this S-Doc Job to be run as a different user, we would click Add Row again and add the "Run as User" field. For "Value," we would enter the username of the user we want this Job to be run under.

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 your username 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.

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 (Visual Workflows)

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 IDs 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.