Apex - For循環(huán)

2019-10-26 16:26 更新

For循環(huán)

for循環(huán)是一種重復控制結(jié)構(gòu),它允許您有效地編寫需要執(zhí)行特定次數(shù)的循環(huán)。 考慮一個業(yè)務案例,當我們想一次性處理或更新100條記錄。 沒有循環(huán)語法,這將是困難的。

語法:
for (variable : list_or_set) { code_block }

流程圖:

流程圖


示例:
考慮我們有一個Invoice對象,它存儲CreatedDate,Status等日常發(fā)票的各種信息。在這個例子中,我們將獲取今天創(chuàng)建的發(fā)票,狀態(tài)為付費。

注意:在執(zhí)行此示例之前,在“發(fā)票對象”中創(chuàng)建至少一個記錄。
//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);

以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號