function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
J BengelJ Bengel 

Purpose of <sObject> when implementing Database.Batchable<sObject>

Pretty new to Apex in general, and completely new to Batch Apex, but that's what I'll need to use in order to do the job before me so I'm tyring to get my head around the differences. The Tralihead module on Async Apex has a good example, but doesn't answer my question. The developer guide (https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_batch_interface.htm) shows a couple of different examples, but still doesn't address the quesiton.

So in its shortest form, my quesiotn is what's the difference between these two class declarations? Besides the obvious I mean.
public class SearchAndReplace implements Database.Batchable<sObject>{}
and 
public class batchClass implements Database.batchable{}
One specifiec <sObject> in the protitype, the other doesn't. That suggests it's optional, but I've yet to find any docuemntation that outlines when to use it and when not to.

The difference in the classes themselves is mostly in the start method -- the first returns a QueryLocator and the second an Iterator (the other difference being that I mostly understand what the first one is doing). But it doesn't explain why the <sObject> exists in one prototype and not the other. There may be a use case where you'd use Batch Apex for something besides processing large numebrs of database records, but neiter of these examples does that -- they just differ in how (and presumably how many) they process the records in question.

That's not the only question these examples raise, but trying to tsneak in another one woudl be cheating. So I'll start with this one and see if it helps with the others. 

 
Best Answer chosen by J Bengel
Pravin K YadavPravin K Yadav
Hi J Bengel,

It depends on your need, if you want to run a batch on records that can be filtered by SOQL then QueryLocator is preferable, but if records that you want to be processed by batch can not be filtered by SOQL then you have to use iteratable. But in most of the cases, it will be achieved by query locator, so query locator is preferable so just try with it if your scope is complex and can not be achieved by SOQL then go with iterable.

Database.QueryLocator:
When we are using QueryLocator then we have to use <sObject>.
Use the Database.QueryLocator object when you are using a simple query (SELECT) to generate the scope of objects used in the batch job. 50 million records can be returned.

Iterable:
Governor limits will be enforced. So, we can use up to 50,000 records only. Else, an exception will be thrown.
You can also use the iterable to create your own custom process for iterating through the list.

For more info and examples about it, you can follow the below link
https://www.folkstalk.com/2020/03/salesforce-batch-apex-job-with-examples.html

I hope you find the above solution helpful. If it does, please mark it as Best Answer to help others too.

Thanks and Regards

All Answers

Pravin K YadavPravin K Yadav
Hi J Bengel,

It depends on your need, if you want to run a batch on records that can be filtered by SOQL then QueryLocator is preferable, but if records that you want to be processed by batch can not be filtered by SOQL then you have to use iteratable. But in most of the cases, it will be achieved by query locator, so query locator is preferable so just try with it if your scope is complex and can not be achieved by SOQL then go with iterable.

Database.QueryLocator:
When we are using QueryLocator then we have to use <sObject>.
Use the Database.QueryLocator object when you are using a simple query (SELECT) to generate the scope of objects used in the batch job. 50 million records can be returned.

Iterable:
Governor limits will be enforced. So, we can use up to 50,000 records only. Else, an exception will be thrown.
You can also use the iterable to create your own custom process for iterating through the list.

For more info and examples about it, you can follow the below link
https://www.folkstalk.com/2020/03/salesforce-batch-apex-job-with-examples.html

I hope you find the above solution helpful. If it does, please mark it as Best Answer to help others too.

Thanks and Regards
This was selected as the best answer
J BengelJ Bengel
Thanks. It took a second read, because I somehow missed the one line that made it make sense:
"When we are using QueryLocator then we have to use <sObject>."
I thought I'd gotten around the problem by oevrhauling my approach ot use a queueable job that woudl only need a couple of dozen DML operations, but I hit the 50,000 record query limit -- even though no single query returned more than 19,000 records.

But that's another question and this answers the question I had about the mysterious <sObject> in the signautre.