在Batch Apex中使用Aggregate SOQL统计查询语句及结果
- 9月
- 26
- Posted by Lin
- Posted in APEX, Salesforce
In previous post, we had created a schedulable batch apex that implements Database.Batchable<sObject> in Salesforce,
but if you want to use SOQL having aggregate functions like SUM(), MAX(), COUNT() on results grouped by “GROUP BY” clause in start execution,
changing to interface Database.Batchable<AggregateResult> isn’t a workable way, because it fails with the below compile error :
To fix this, using Apex classes that implement Iterator<AggregateResult> and Iterable<AggregateResult> seems the only way for now.
Here is the outline what we should do.
1. Create an Apex class implements Iterator<AggregateResult>.
2. Create an Apex class implements Iterable<AggregateResult>.
3. Implementing to Database.Batchable<AggregateResult>, and Using Iterable at start execution in Batch Apex.
Let’s get started.Iterator
Create an Apex class named “AggregateResultIterator” with the following source code :
Iterable
Create an Apex class named “AggregateResultIterable” with the following source code :
It seems working fine! : )
Enjoy it!
Class must implement the global interface method: Iterable<AggregateResult> start(Database.BatchableContext) from Database.Batchable<AggregateResult>
The following sample code will explain this.To fix this, using Apex classes that implement Iterator<AggregateResult> and Iterable<AggregateResult> seems the only way for now.
Here is the outline what we should do.
1. Create an Apex class implements Iterator<AggregateResult>.
2. Create an Apex class implements Iterable<AggregateResult>.
3. Implementing to Database.Batchable<AggregateResult>, and Using Iterable
Let’s get started.
Iterator Apex class
Create an Apex class named “AggregateResultIterator” with the following source code : Iterable Apex class
Create an Apex class named “AggregateResultIterable” with the following source code : Batch Apex
Then implement Batch Apex from Database.Batchable<AggregateResult>, and use Iterable<AggregateResult>, AggregateResultIterable instead of Database.QueryLocator at start execution, as the following source code :Operation check
Run SampleAggregateBatch and check debug log.It seems working fine! : )
Enjoy it!
Post Tagged with AggregateResult, Batch Apex, Database.Batchable, Database.BatchableContext, GROUP BY, Iterable, Salesforce, Schedulable
近期文章
- Salesforce DevTools Release Notes (version 3.10.0) – mass edit custom fields on lightning
- Salesforce DevTools Release Notes (version 3.9.0)- Object Fields, SOQL Query Editor
- Salesforce DevTools Release Notes (version 3.8.0)
- Salesforce DevTools的开发者用户突破10,000名!THANK YOU!
- Lightning Component中使用Promise处理Apex服务端请求
Top Posts
- Salesforce DevTools – Saleforce开发者必备Chrome扩展 - 220.7K views
- Date format 与 DateTime format - 205.1K views
- Salesforce批量编辑 (Chrome扩展) – 在任何标准视图上进行批量插入, 拷贝, 更新, 删除, 支持Classic与Lightning! - 158.8K views
- 用Sublime Text 3架设Salesforce的快速开发环境 - 122.4K views
- 编写一个可被计划执行的Batch Apex - 119.9K views
分类目录
- APEX
- 应用
- Custom Label
- Custom Metadata
- Custom Setting
- Design & UI
- 交流会
- Heroku
- iOS & Object-c
- Lightnint Component
- Linux & Mac OS
- Metadata API
- Node.js
- Open Source
- Other
- PHP Framework
- Release Notes
- REST API
- Salesforce
- Salesforce DevTools
- Salesforce DX
- SOQL
- Tooling API
- Unit Tests
- Validation Rule
- Visualforce
- WordPress
您必须登录才能发表评论。