You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

第三方系统调用Salesforce API实现结果分页方案咨询

Salesforce Apex REST API 实现分页数据返回

不需要依赖VisualForce/LWC这类前端组件,直接用Apex REST API就能实现第三方系统调用的分页需求,以下是具体实现方案:

一、基础分页实现(适合数据量≤2000条)

通过SOQL的OFFSET和LIMIT参数配合,接收第三方传入的页码和每页条数,直接返回结构化分页结果:

@RestResource(urlMapping='/GetPagedData/*')
global with sharing class PagedDataController {
    @HttpGet
    global static Map<String, Object> getPagedRecords() {
        RestRequest req = RestContext.request;
        // 读取分页参数,设置默认值避免空指针
        Integer pageIndex = Integer.valueOf(req.params.get('PageIndex') ?? '1');
        Integer pageSize = Integer.valueOf(req.params.get('PageSize') ?? '20');
        
        // 计算偏移量:(当前页码-1)*每页条数
        Integer offset = (pageIndex - 1) * pageSize;
        
        // 执行分页查询(注意OFFSET最大支持2000,超过需用滚动查询)
        List<Account> records = [
            SELECT Id, Name, Industry, BillingCity 
            FROM Account 
            LIMIT :pageSize 
            OFFSET :offset
        ];
        
        // 查询总记录数,用于计算总页数
        AggregateResult totalResult = [
            SELECT COUNT(Id) total 
            FROM Account
        ];
        Integer totalRecords = (Integer)totalResult.get('total');
        Integer totalPages = (totalRecords + pageSize - 1) / pageSize; // 向上取整计算总页数
        
        // 构造返回结果
        Map<String, Object> response = new Map<String, Object>{
            'PageIndex' => pageIndex,
            'PageSize' => pageSize,
            'TotalRecords' => totalRecords,
            'TotalPages' => totalPages,
            'Data' => records
        };
        
        return response;
    }
}

二、大数据量分页方案(规避OFFSET 2000限制)

如果数据量超过2000条,用OFFSET会报错,此时可以基于唯一排序字段(比如Id、LastModifiedDate)做滚动查询,每次返回当前页最后一条记录的标识,供下一页调用时作为查询条件:

@RestResource(urlMapping='/GetLargePagedData/*')
global with sharing class LargePagedDataController {
    @HttpGet
    global static Map<String, Object> getPagedLargeRecords() {
        RestRequest req = RestContext.request;
        Integer pageSize = Integer.valueOf(req.params.get('PageSize') ?? '20');
        String lastRecordId = req.params.get('LastRecordId') ?? '';
        
        // 拼接查询语句,基于Id滚动
        String soql = 'SELECT Id, Name, Industry, BillingCity FROM Account ORDER BY Id ASC LIMIT :pageSize';
        if(String.isNotBlank(lastRecordId)){
            soql += ' WHERE Id > :lastRecordId';
        }
        
        List<Account> records = Database.query(soql);
        // 获取下一页查询用的标识
        String nextLastId = records.isEmpty() ? '' : records[records.size()-1].Id;
        
        // 查询总记录数(不需要的话可以省略)
        AggregateResult totalResult = [SELECT COUNT(Id) total FROM Account];
        Integer totalRecords = (Integer)totalResult.get('total');
        
        Map<String, Object> response = new Map<String, Object>{
            'PageSize' => pageSize,
            'TotalRecords' => totalRecords,
            'Data' => records,
            'NextLastRecordId' => nextLastId // 传给下一页作为查询条件
        };
        
        return response;
    }
}

三、关键注意事项

  • 权限控制:类用with sharing确保遵守Salesforce的对象共享规则,第三方调用账号需要拥有对应对象的API访问权限和字段权限。
  • 参数校验:可以额外加入参数合法性校验,比如限制PageSize不能超过200(单查询最大LIMIT为2000,建议合理设置),PageIndex不能小于1。
  • 调用方式:第三方系统直接通过HTTP GET请求调用,示例地址:
    https://你的Salesforce域名/services/apexrest/GetPagedData?PageIndex=2&PageSize=10
    返回结果为JSON格式,包含分页元数据和数据列表。

内容的提问来源于stack exchange,提问作者Manav Gupta

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.03 03:31:03