创建 List FOR(列表 FOR)循环
跟随 Trail Together 进行学习
进行这一步骤时,想要跟专家一起学习吗?观看此视频,它是 Trail Together 系列的一部分。
(这部分内容从 17:24 开始,如果您想要倒回去再次观看步骤的开头部分可以从这里开始。)
调整循环
增加一个 List FOR(列表 FOR)循环,对您创建的 bankAccts 列表进行迭代。
- 在 Developer Console 中,请打开 Execute Anonymous(执行匿名)窗口。
- 把现有代码替换为这个代码:
//From the BankAcct class, instantiate an object named chkAcct BankAcct chkAcct = new BankAcct(); //Set the accttype attribute of the chkAcct object to Checking chkAcct.accttype = 'Checking'; //Set the acctName attribute of the chkAcct object to D.Castillo-Chk chkAcct.acctName = 'D.Castillo-Chk '; //Invoke the makeDeposit method with 150 as the argument chkAcct.makeDeposit(150); //From the BankAcct class, instantiate an object named bankAccts BankAcct savAcct = new BankAcct(); //Set the accttype attribute of the savAcct object to Savings savAcct.accttype = 'Savings'; //Set the acctName attribute of the savAcct object to D.Castillo-Sav savAcct.acctName = 'D.Castillo–Sav'; //Invoke the makeDeposit method with 220 as the argument savAcct.makeDeposit(220); //From the BankAcct class, instantiate an object named savAcct List<BankAcct> bankAccts = new List<BankAcct>(); System.debug('The BankAcct List has ' + bankAccts.size() + ' bank accounts.'); //Add the chkAcct object to the bankAccts list bankAccts.add(chkAcct); //Add the savAcct object to the bankAccts list bankAccts.add(savAcct); System.debug('The BankAcct List has ' + bankAccts.size() + ' bank accounts.'); System.debug('Here is the list: ' + bankAccts); //Declare a list FOR loop that uses an iteration variable named tempacct //to loop through the bankAccts list for (BankAcct tempacct:bankAccts){ system.debug(tempacct.acctName + ' is a ' + tempacct.accttype + ' account with a balance of $'+ tempacct.getBalance()); }
- 确认 Open Log(打开日志)被选中,然后单击 Execute(执行)。执行日志打开。
- 选中 Debug Only(仅限调试)。
- 查看调试消息,然后关闭日志。
在 createContact 方法中,增加一个 List FOR(列表 FOR)循环,对 Candidate(候选人)列表进行迭代。
- 在 CreateContactFromCan 类中,把现有代码替换为这个代码:
public with sharing class CreateContactFromCan { //Declare a method that returns void and accepts a Candidate list named candsFromTrigger public void createContact (List<Candidate__c> candsFromTrigger){ //Instantiate a Contact list named conList List<Contact> conList = new List<Contact>(); //Declare a list FOR loop that uses an iteration variable named currentCandidate //to loop through the candsFromTrigger list for(Candidate__c currentCandidate:candsFromTrigger){ } } }
- 保存 CreateContactFromCan 类。