Apex-for-loop

提供:Dev Guides
移動先:案内検索

Apex-Forループ

*for* ループは、特定の回数実行する必要があるループを効率的に記述することができる繰り返し制御構造です。 100件のレコードを一度に処理または更新する必要があるビジネスケースを考えます。 ここで、ループ構文が役立ち、作業が容易になります。

構文

for (variable : list_or_set) { code_block }

流れ図

Apex For Loop

CreatedDate、Statusなどの日次請求書の情報を格納するInvoiceオブジェクトがあることを考慮してください。 この例では、今日作成された請求書を取得し、ステータスを「支払済」にします。

-この例を実行する前に、請求書オブジェクトに少なくとも1つのレコードを作成します。

//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);