- Apex 编程教程
- Apex - 主页
- Apex - 概述
- Apex - 环境
- Apex - 示例
- Apex - 数据类型
- Apex - 变量
- Apex - 字符串
- Apex - 数组
- Apex - 常量
- Apex - 决策制定
- Apex - 循环
- Apex - 集合
- Apex - 类
- Apex - 方法
- Apex - 对象
- Apex - 接口
- Apex - DML
- Apex - 数据库方法
- Apex - SOSL
- Apex - SOQL
- Apex - 安全性
- Apex - 调用
- Apex - 触发器
- Apex - 触发器设计模式
- Apex - 管理员限制
- Apex - 批处理
- Apex - 调试
- Apex - 测试
- Apex - 部署
- Apex 有用资源
- Apex - 快速指南
- Apex - 资源
- Apex - 讨论
Apex - For 循环
for 循环是一种重复控制结构,可让你有效编写需要执行特定次数的循环。考虑一种业务案例,其中我们需要一次性处理或更新 100 条记录。这就是循环语法提供帮助并使工作更轻松的地方。
语法
for (variable : list_or_set) { code_block }
流程图
示例
考虑我们有一个 Invoice 对象,其中存储了每日发票信息,如 CreatedDate、Status 等。在此示例中,我们将获取当天创建的具有 Paid 的发票状态的发票。
注意 − 在执行此示例之前,请在 Invoice 对象中创建至少一条记录。
// Initializing the custom object records list to store the Invoice Records created today List<apex_invoice__c> PaidInvoiceNumberList = new List<apex_invoice__c>(); // SOQL query which will fetch the invoice records which has been created today PaidInvoiceNumberList = [SELECT Id,Name, APEX_Status__c FROM APEX_Invoice__c WHERE CreatedDate = today]; // List to store the Invoice Number of Paid invoices List<string> InvoiceNumberList = new List<string>(); // This loop will iterate on the List PaidInvoiceNumberList and will process each record for (APEX_Invoice__c objInvoice: PaidInvoiceNumberList) { // Condition to check the current record in context values if (objInvoice.APEX_Status__c == 'Paid') { // current record on which loop is iterating System.debug('Value of Current Record on which Loop is iterating is'+objInvoice); // if Status value is paid then it will the invoice number into List of String InvoiceNumberList.add(objInvoice.Name); } } System.debug('Value of InvoiceNumberList '+InvoiceNumberList);
apex_loops.htm
广告