Why Batch Apex Class?

In our series of posts on Salesforce-Jira Integration we saw how we can pull issues from Jira and save into Salesforce in a Custom Object using Jira's REST API. However, the integration makes sense only if the push-pull of issues are done in the background automatically in a defined period of time. This is where Batch Apex Classes comes into picture.

Salesforce Developer Guide defines Batch Apex as below which is pretty much self explanatory.

Batch Apex operates over small batches of records, covering your entire record set and breaking the processing down to manageable chunks.

For more information on Batch Apex and its usage refer the below links,

If you are using the Batch Apex to operate over the records within your Salesforce org, then there isn't any additional code required apart from the one explained in the developer guide. But since the batch is supposed to operate on records that are external to the Salesforce (i.e. Jira), we need some extra classes which are explained hereafter.

Batchable Class

Again from the developer guide; to make a class batchable it has to implement Database.Batchable interface which in turn requires the three interface methods to be implemented. Starting with the start method, the return type could either be Database.QueryLocator or Iterable<sObject>. Obviously and unfortunately we cannot have a QueryLocator for the response that we get from the JIRA REST URL. So the start method has to be with the below signature:

global Iterable<sObject> start(Database.BatchableContext bc) {}

Note that the sObject type in Iterable<sObject> will be nothing but the custom object we created for storing the issues in Salesforece, i.e. JiraIssue__c

Iterable to Iterator

Now to make the start method return an Iterable<sObject> type we have to create a class say IssueIterableImplementation that implements the iterable interface. This interface has a method named iterator whose return type is Iterator<sObject>.

So similarly to have a return type of Iterator<sObject> we need to create a class that implements the iterator interface which in turn has two methods to be implemented.

  1. hasNext() and
  2. next()

Let's call this class as IssueIterator() then the two classes would be as below,

IssueIterableImplementation Class

    global class IssueIterableImplementation implements iterable<Issues__c>{
     global Iterator<Issues__c> Iterator(){
      return new IssueIterator();

IssueIterator Class

global class IssueIterator implements Iterator<Issues__c>{

   // Issus class and other variables from previous post goes here
   Integer i {get; set;} //Counter for current index of supportIssues List

   public IssueIterator(){ 
    //JiraIntegrationController from previous post goes here

   global boolean hasNext(){ 
       if(i >= supportIssues.size()) {
           return false; 
       } else {
           return true; 

   global Issues__c next(){ 
       if(i == supportIssues.size()){return null;} 
       return supportIssues[i-1]; 

Finally the start method will be like:

global Iterable<Support_Issues__c> start(Database.BatchableContext bc){
    return new IssueIterableImplementation();

And the execute method be like:

global void execute(Database.BatchableContext bc,
 List<Support_Issues__c> supportIssuesList){
  // Code to iterate over the issues list and do the desired operations.

And the final Batch apex class will look like:

global class SalesforceJiraSynchronizer implements 

That is it !!! Now this batch class can be scheduled to run every one hour or more than that using Cron expression based upon how frequently the issues are updated in your Jira. Below given is the sample apex code that can be executed in the developer console to schedule our batch apex run every hour. Once you run the code you can confirm the schedule by checking the Monitor - Jobs - Scheduled Jobs section.

SalesforceJiraSynchronizer sjs= new SalesforceJiraSynchronizer ();
String cronString = '0 0 * * * ?';
System.schedule('Salesforce Jira Synchronizer', cronString, sjs);

If you face any issues with the Batch Apex Class for Salesforce-Jira Sync then please feel free to write me or comment below. Thank You.

You’ve successfully subscribed to inteygrate
Welcome back! You’ve successfully signed in.
Great! You’ve successfully signed up.
Your link has expired
Success! Check your email for magic link to sign-in.