For循環(huán)
for循環(huán)是一種重復(fù)控制結(jié)構(gòu),它允許您有效地編寫需要執(zhí)行特定次數(shù)的循環(huán)。 考慮一個(gè)業(yè)務(wù)案例,當(dāng)我們想一次性處理或更新100條記錄。 沒有循環(huán)語法,這將是困難的。
語法:
for (variable : list_or_set) { code_block }
流程圖:
示例:
考慮我們有一個(gè)Invoice對(duì)象,它存儲(chǔ)CreatedDate,Status等日常發(fā)票的各種信息。在這個(gè)例子中,我們將獲取今天創(chuàng)建的發(fā)票,狀態(tài)為付費(fèi)。
注意:在執(zhí)行此示例之前,在“發(fā)票對(duì)象”中創(chuàng)建至少一個(gè)記錄。
//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 the 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);
更多建議: