Batch Apex Implementation Using Iterables
We use Iterables when you want to iterate over sObject rows rather than using Database.queryLocator in the Batch Apex.
You may require this if you need some more processing and need custom record batches for your batch execute method.
Below we discuss how to implement the Iterator and Iterable interfaces to use in Batch Apex.
Implement the Iterator Interface:
Using the Iterator interface you can create a custom set of instructions for traversing a List through a loop.
This is useful for data that exists in sources outside of Salesforce that you would normally define the scope of using a SELECT statement.
Iterators can also be used if you have multiple SELECT statements.
global class CustomIterable implements Iterator<Account>{
List<Account> accs {get; set;}
Integer i {get; set;}
public CustomIterable(){
accs =
[SELECT Id, Name,
NumberOfEmployees
FROM Account
WHERE Name = 'false'];
i = 0;
}
global boolean hasNext(){
if(i >= accs.size()) {
return false;
} else {
return true;
}
}
global Account next(){
// 8 is an arbitrary
// constant in this example
// that represents the
// maximum size of the list.
if(i == 8){return null;}
i++;
return accs[i-1];
}
}
To use custom iterators, you must create an Apex class that implements the Iterator interface.
hasNext() method returns true if there is another item in the collection being traversed, false otherwise.
next() method returns the next item in the collection.
All methods in the Iterator interface must be declared as global or public.
Implementing the Iterable Interface:
global class foo implements iterable<Account>{
global Iterator<Account> Iterator(){
return new CustomIterable();
}
}
The iterator method must be declared as global or public. It creates a reference to the iterator that you can then use to traverse the data structure.
In the above class, we are calling the CustomIterable class which was created before.
Using Iterable In Batch Apex:
global class batchClass implements Database.batchable<Account>{
global Iterable<Account> start(Database.batchableContext info){
return new foo();
}
global void execute(Database.batchableContext info, List<Account> scope){
List<Account> accsToUpdate = new List<Account>();
for(Account a : scope){
a.Name = 'UPDATED';
a.NumberOfEmployees = 79;
accsToUpdate.add(a);
}
update accsToUpdate;
}
global void finish(Database.batchableContext info){
}
}
The above class is a batch class that uses a custom iterator.
Please note that If you use an iterable, the governor limit for the total number of records retrieved by SOQL queries is still enforced.
Thank you for visiting SalesforceBlue.com
If you have any queries feel free to write down a comment below ð
Hi There,
Thank you for this explanation. I have one question on execute method here –
How many records will be going into List scope parameter in case of Iterable return type in start method ?
Are there any other limitations with Iterable as compared to query locator in normal batch scenarios?
Thanks
test
Pingback: Homepage
Pingback: https://snaptube.blog.br/
Pingback: https://stealthex.io
Pingback: dark168
Pingback: Sofwave āļĢāļĩāļ§āļīāļ§
Pingback: āļĢāļąāļāļāļģāđāļ§āđāļāđāļāļāđ
Pingback: White Label Payment Services
Pingback: Saxenda kaufen
Pingback: Browning shotguns
Pingback: Wood Pellets For Sale
Pingback: āļŠāļĨāđoāļ PG
Pingback: āļāļąāļāļŠāļ·āļāđāļāļāļāļ
Pingback: āđāļāđāļāļŠāļāļāđāļāļāļĢāļĩāđ
Pingback: āđāļāđāļāļāļķāļāđāļāļīāļ
Pingback: zircuit dex crypto
thailandiqos
Pingback: catzilla meme
Pingback: web design for therapists
You absolutely know how to keep your readers interest with your witty thoughts on that topic. I was looking for additional resources, and I am glad I came across your site. Feel free to check my website YK3 about Cosmetic Treatment.
Pingback: todos santos real estate
Pingback: Volnewmer
Pingback: wpsäļč――
Pingback: āļāļĩāđāļāļĢāļķāļāļĐāļēāļāļļāļĢāļāļīāļ
Pingback: āļāļĢāļ°āđāļāđāļēāļāđāļēāļāļĢāļ°āļŠāļāļ
Pingback: hit789
Pingback: wing888
Pingback: pg333
Pingback: lucabet88
Pingback: āļŠāļāļīāļāđāļāļāļĢāđāļŠāļīāļāļāđāļē
Pingback: office for rent phuket
Pingback: rent a boat in dania beach
Pingback: bk8
Pingback: āļŠāļĨāđoāļ pg āđāļ§āđāļāļāļĢāļāđāļāļāļŦāļāļąāļ
Pingback: Bugatti historia
Pingback: āļŠāļīāļāđāļāļ·āđāļāļĢāļāļāļĢāļĢāļāļļāļ
Pingback: āļāļąāļāđāļāļīāđāļĨ
Pingback: āđāļāđāļāđāļāļāļāļąāļ§āļĢāđ
Pingback: āļĢāļāļĢāļąāļāļŠāđāļāļŠāļāļēāļĄāļāļīāļāđāļāļāļĩāđāļāļļāđāļ
Pingback: som777
Pingback: āđāļāđāļēāļĢāļāļāļđāđāļāļĢāđāļāļĄāļāļāļāļąāļ
Pingback: lucabet
Pingback: āđāļāđāļ AIS
Pingback: Joker Slot āļāđāļēāļĒāļŠāļĨāđāļāļāđāļāļāļāđāļāļĒāļāļąāļāļāļąāļ 1
Pingback: jebjeed888
Pingback: eft exploit
Pingback: āļāļēāļāđāļāđāļēpg
Pingback: Nexus Market