目录

for循环

for循环是一种重复控制结构,允许您有效地编写需要执行特定次数的循环。 考虑一个商业案例,其中,我们需要一次处理或更新100条记录。 这是Loop语法有助于简化工作的地方。

语法 (Syntax)

for (variable : list_or_set) { code_block }

流程图 Apex For Loop

例子 (Example)

考虑到我们有一个Invoice对象,它存储CreatedDate,Status等日常发票的信息。在这个例子中,我们将获取今天创建的发票,状态为Paid。

Note - 在执行此示例之前,请在“发票对象”中创建至少一条记录。

// 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);
↑回到顶部↑
WIKI教程 @2018