课程表

Apex课程

工具箱
速查手册

Apex - For循环

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

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

流程图:

流程图


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

注意:在执行此示例之前,在“发票对象”中创建至少一个记录。
  1. //Initializing the custom object records list to store the Invoice Records created today
  2. List<apex_invoice__c> PaidInvoiceNumberList = new List<apex_invoice__c>();
  3. //SOQL query which will fetch the invoice records which has been created today
  4. PaidInvoiceNumberList = [SELECT Id,Name, APEX_Status__c FROM APEX_Invoice__c WHERE CreatedDate = today];
  5. //List to store the Invoice Number of Paid invoices
  6. List<string> InvoiceNumberList = new List<string>();
  7. //This loop will iterate on the List PaidInvoiceNumberList and will process the each record
  8. for (APEX_Invoice__c objInvoice: PaidInvoiceNumberList) {
  9. //Condition to check the current record in context values
  10. if (objInvoice.APEX_Status__c == 'Paid') {
  11. //current record on which loop is iterating
  12. System.debug('Value of Current Record on which Loop is iterating is '+objInvoice);
  13. //if Status value is paid then it will the invoice number into List of String
  14. InvoiceNumberList.add(objInvoice.Name);
  15. }
  16. }
  17. System.debug('Value of InvoiceNumberList '+InvoiceNumberList);

转载本站内容时,请务必注明来自W3xue,违者必究。
 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号