
- 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
廣告