Generate Documents with Batch Apex

Introduction

S-Docs Batchable Document Generation allows you to generate a large number of documents at once by using batchable apex to create and run S-Doc Job records together.

This feature is similar to the Mass Merge feature, with exception that Mass Merge utilizes queueable apex. While queueable apex can process multiple documents concurrently, batchable document generation can generally handle much larger jobs without running into daily Salesforce platform limits.

This article will show you how to create a test SDBatch class and provide more insight into how the application operates. It goes over designing a process similar to that of Mass Merge (selecting records from a list view and clicking a button to generate documents).

Step 1: Create An Apex Class

From the Setup menu, type "Apex Classes" into the Quick Find bar, click Apex Classes in the dropdown menu, then click New.

Insert the following Apex class.

Note: Be sure to replace [<TEMPLATE NAME or ID>] in lines 25 & 31 with an S-Docs template name or ID.
public class SDBatchTest {
    private ApexPages.StandardSetController stdSetController;

    public SDBatchTest(ApexPages.StandardSetController sscOther) {
        stdSetController = sscOther;
    }
    
    public PageReference createBatchTest() {
        String objectName = ApexPages.currentPage().getParameters().get('objectName');
        Boolean showHeader = ApexPages.currentPage().getParameters().get('showHeader') != 'false';

        List<SObject> selectedRecords = stdSetController.getSelected();
        if (Test.isRunningTest()) {
            selectedRecords = new List<SObject> { new Contact(LastName='Test') };
        }
       
        //This is responsible for the SDJob creation. Start__c must be set to False. 
        List<SDOC__SDJob__c> jobList = new List<SDOC__SDJob__c> {};
          for(SObject selectedRecord : selectedRecords) {
            SDOC__SDJob__c job =
            new SDOC__SDJob__c(SDOC__Start__c=false,
                              SDOC__Oid__c=String.valueOf(selectedRecord.get('Id')),
                              SDOC__ObjApiName__c=objectName,
                              SDOC__SendEmail__c='0',
                              SDOC__Doclist__c='[<TEMPLATE NAME or ID>]');
            jobList.add(job);
          }
          insert jobList;
        // This part calls SDBatch and searches for jobs based on isSDJobQuery.        
        SDOC.SDBatch sdb = new SDOC.SDBatch();
        sdb.templatenameorid = '[<TEMPLATE NAME or ID>]';
        sdb.isSDJobQuery = true;
        //The query can be updated for to specify the batch requirements. 
        sdb.query = 'WHERE SDOC__Start__c=false AND SDOC__Status__c != \'Error\''; 
        sdb.batchSize = 10;
        sdb.executeBatch();

        return new PageReference('/');
    }
   
}

Click Save.

Note: The SendEmail parameter is currently not supported with Batch apex, and should be set to 0 as indicated in the example above.

Step 2: Create A Visualforce Page

From the Setup menu, type "Visualforce" into the Quick Find bar, click Visualforce Pages in the dropdown menu, then click New.

Enter the following values:

Label: SDBatch_Test
Name: SDBatch_Test
Available for Lightning Experience, Experience Builder sites, and the mobile app: Checked

Markup:

Note: Be sure to replace [<OBJECTAPINAME>] with your  object's API name in line 1.
<apex:page standardController="[<OBJECTAPINAME>]" showHeader="false"
extensions="SDBatchTest"

recordSetVar="selected"
action="{!createBatchTest}">
</apex:page>

Click Save.

Step 3: Create A List Button

Creating a list button will allow users to select records from a list view and click the button to invoke batchable document generation.

From the Setup Menu, navigate to the Object Manager and choose your object.

Navigate to the Buttons, Links, and Actions tab and click New Button or Link.

Enter the following values:

Label: SDBatch
Name: SDBatch
Display Type: List Button
Display Checkboxes (for Multi-Record Selection): Checked
Behavior: Display in existing window without sidebar or header
Content Source: URL
URL:

Note: Be sure to replace [<OBJECTAPINAME>] with your object's API name.
/apex/SDBatch_Test?objectName=[<OBJECTAPINAME>]

Click Save.

Step 3A: Add List Button To Search Layout

After saving your button, navigate to the Search Layouts for Salesforce Classic tab. Find the List View search layout, click the dropdown arrow on the right, and click Edit.

Scroll down to the Custom Buttons section, click your SDBatch button, and click Add to add it to the list view search layout for your object.

Click Save.

Use The Batchable Document Generation Feature

After completing the configuration steps, test the feature by selecting records from your object's list view, and then click SDBatch.

A batch apex job will create S-Doc Job records for each record selected from the list view. Each S-Doc Job will begin at 0%, but will be completed as the apex job completes.

Once each S-Doc Job record reaches 100%, the Status Details field of each job will update to Document generated successfully via Batch Apex.

The documents that the S-Doc Jobs created will be linked to their corresponding object records.

Note: You can track your batch apex job in Setup > Apex Jobs > Click here to go to the new batch jobs page

Tags:

Was this helpful?