Apex - For循环

2019-10-26 16:26 更新

For循环

for循环是一种重复控制结构,它允许您有效地编写需要执行特定次数的循环。 考虑一个业务案例,当我们想一次性处理或更新100条记录。 没有循环语法,这将是困难的。

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

流程图:

流程图


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

注意:在执行此示例之前,在“发票对象”中创建至少一个记录。
//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);

以上内容是否对您有帮助:
在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号