Skip to main content

使用 DML 把 sObjects 保存到数据库

备注

备注

用中文(简体)学习?在此徽章中,Trailhead 实践挑战验证使用英文。括号中提供了译文,用作参考。确保复制粘贴英文值,将 Trailhead Playground 切换为英语,将区域设置切换为美国。按此处说明进行。

查看 Trailhead 本地化语言徽章了解如何利用 Trailhead 译文。

跟随 Trail Together 进行学习

进行这一步骤时,想要跟专家一起学习吗?观看此视频,它是 Trail Together 系列的一部分。

(这部分内容从 21:37 开始,如果您想要倒回去再次观看步骤的开头部分可以从这里开始。)

创建并调整 sObjects

创建 Recruiting Account sObject(招聘帐户 sObject)并把它保存到数据库。 

  1. 在 Developer Console 中,请打开 Execute Anonymous(执行匿名)窗口。
  2. 把现有代码替换为这个代码:
    //From the Account class, instantiate an object named acct
    Account acct = new Account();
    //Set the name attribute of the acct object to Recruiting
    acct.name='Recruiting';
    //Use DML to save the acct object to the database
    Database.insert(acct);
    System.debug('A new account named ' + acct.name + ' was added ');
  3. 确认 Open Log(打开日志)被选中,然后单击 Execute(执行)。执行日志打开。
  4. 选中 Debug Only(仅限调试)
  5. 查看调试消息,然后关闭日志。

在 createContact 方法中,创建联系人 sObject,填写它的属性,把它添加到 conList,然后把列表保存到数据库中。

  1. 在 CreateContactFromCan 类中,把现有代码替换为这个代码:
    public with sharing class CreateContactFromCan {
        //Declare a method that returns void and accepts a Candidate list named candsFromTrigger
        public static 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) {
                //Create a Contact and add it to conList
                //Set the FirstName, LastName, and Email attributes to match the
                //corresponding attributes of the currentCandidate object (First_Name__c,
                //Last_Name__c, and Email__c)
                conList.add(new Contact(
                    //Set the AccountId for the contact(we'll do this in the next unit)
                    //AccountId = candAccts[0].Id,
                    FirstName = currentCandidate.First_Name__c,
                    LastName = currentCandidate.Last_Name__c,
                    Email = currentCandidate.Email__c)
                );
            }
            //Save conList to the database
            Database.insert(conList);
        }
    }
  2. 保存 CreateContactFromCan 类。
继续免费学习!
注册帐户以继续。
有什么适合您的内容?
  • 为您的职业目标获取个性化推荐
  • 通过实践挑战和测验练习您的技能
  • 跟踪并与雇主分享您的进度
  • 与人联系以获取指导和就业机会