Apex - SOQL循环
2019-10-26 16:26 更新
SOQL循环
当我们不想创建List并直接遍历SOQL查询的返回记录集时,将使用此类型的for循环。 我们将在下一章更深入地研究SOQL查询。 现在,只记得它返回的记录和字段的列表在查询中给出。
语法:
for (variable : [soql_query]) { code_block }或者:
for (variable_list : [soql_query]) { code_block }
这里要注意的一点是,variable_list或变量应该始终与Query返回的记录具有相同的类型。 在我们的示例中,它与APEX_Invoice_c的类型相同。
流程图:
示例:
我们的For循环示例使用SOQL for循环。
//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);
以上内容是否对您有帮助:
更多建议: