# SDBatch (Legacy)

*SDBatch is intended to use if clients intends batching SD Jobs together to be created. The goal is to create the SD Job records individually with Start\_\_c=false then use SDBatch to gather all SD Jobs created and run them in sync. As long as the client is able to place the base record IDs in a list to reference, they can use the following apex class as a reference point.*

{% hint style="info" %}
The following instructions are intended for creating a test SDBatch class and provide more insight how the application operates.
{% endhint %}

\
Instructions

To test SDbatch quickly, we designed a method similar to Mass Merge. The intention is to have the user choose records from a list view and pass them to the class to create the jobs and run SDBatch.

{% stepper %}
{% step %}

#### Create your APEX class

{% hint style="warning" %}
Note: The template name or ID will need to be decided and placed into the class (line 25 & 31).
{% endhint %}

```apex
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 is 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('/');
    }
   
}
```

{% hint style="warning" %}
Note: The SendEmail parameter is currently not supported with Batch apex, and should be set to 0 as indicated in the example above.
{% endhint %}
{% endstep %}

{% step %}

#### Create your Visualforce page

{% hint style="warning" %}
Note: The object API name is added in line 1.
{% endhint %}

```xml
<apex:page standardController="[<OBJECTAPINAME>]" showHeader="false"
extensions="SDBatchTest"

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

{% endstep %}

{% step %}

#### Add the button to the object's List View

{% hint style="warning" %}
The behavior MUST BE SET to: Display in existing window without sidebar or header
{% endhint %}

Button URL:

```
/apex/SDBatch_Test?objectName=[<OBJECTAPINAME>]
```

{% endstep %}
{% endstepper %}

\
Testing

1. The user will select a set of records from the list view and click the SDBatch button.
2. The SDJobs will be created with 0%, but will be completed as the Apex Job completes.

{% hint style="warning" %}
Note: There may be some at 100% and some at 0%. This is likely linked to the batchsize in the apex class.
{% endhint %}

{% hint style="info" %}
Additional Notes:

* The batch can be tracked from Setup > Apex Jobs
* The Apex Jobs page has a respective Batch Jobs page. This will provide more detail of each batch created such as Submitted/Completed Date and Elapsed Time.
*

{% endhint %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://help.sdocs.com/sdocs/maintenance-and-troubleshooting/legacy-workflows/sdbatch-legacy.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
