SOQL循環(huán)
當(dāng)我們不想創(chuàng)建List并直接遍歷SOQL查詢的返回記錄集時(shí),將使用此類型的for循環(huán)。 我們將在下一章更深入地研究SOQL查詢。 現(xiàn)在,只記得它返回的記錄和字段的列表在查詢中給出。
語法:
for (variable : [soql_query]) { code_block }
或者:
for (variable_list : [soql_query]) { code_block }
這里要注意的一點(diǎn)是,variable_list或變量應(yīng)該始終與Query返回的記錄具有相同的類型。 在我們的示例中,它與APEX_Invoice_c的類型相同。
流程圖:
示例:
我們的For循環(huán)示例使用SOQL for循環(huán)。
//The same previous example using For SOQL Loop
List<apex_invoice__c> PaidInvoiceNumberList = new List<apex_invoice__c>();//initializing the custom object records list to store the Invoice Records
List<string> InvoiceNumberList = new List<string>();//List to store the Invoice Number of Paid invoices
for (APEX_Invoice__c objInvoice: [SELECT Id,Name, APEX_Status__c FROM APEX_Invoice__c WHERE CreatedDate = today]) {//this loop will iterate and will process the each record returned by the Query
if (objInvoice.APEX_Status__c == 'Paid') {//Condition to check the current record in context values
System.debug('Value of Current Record on which Loop is iterating is '+objInvoice);//current record on which loop is iterating
InvoiceNumberList.add(objInvoice.Name);//if Status value is paid then it will the invoice number into List of String
}
}
System.debug('Value of InvoiceNumberList with Invoice Name :'+InvoiceNumberList);
更多建議: