机会更新后的顶点触发复制记录

时间:2014-05-08 18:54:30

标签: salesforce apex

我对Apex相对较新并触发写作。以下是我想要完成的事情:

是否可以将自定义对象中的记录复制到在同一对象中创建的新记录中?在我的情况下,当一个机会设置为"关闭赢了"我需要在名为Project__c的自定义对象中创建一个新记录。但是,有一个名为proj1的现有记录,其中包含需要复制到每个进入对象Project__c的新记录的默认数据。

我知道如何在更新机会之后创建新记录,但我不确定如何从对象Project__c中获取记录proj1并复制它。任何帮助都会很棒!

1 个答案:

答案 0 :(得分:1)

您可以使用SOQL检索默认数据Project__c记录,并将其克隆为将要插入的新对象的基础。为此,您需要使用sObject.clone()方法。

E.g。

List<Project__c> toUpdate = new List<Project__c>();

// You might want to consider how you identify this template record. 
// E.g. Custom Setting with the record Id or unique name
// Make sure you select the fields you want to clone.
List<Project__c> templateProjects = [Select 
         Id, Name, OtherCustomFields__c, ThatShouldBeCloned__c 
         from Project__c where Name = 'proj1' limit 1];
if(templateProjects.size() == 1) {
    Project__c templateProject = templateProjects[0];
    // The default opt_preserve_id param in clone won't clone the Id.
    Project__c opportunityProject = templateProject.clone();

    // Set any fields based on the Opportunity that weren't cloned from the template record
    opportunityProject.OpportunitySpecificCustomField__c = opp.Name;

    toUpdate.add(opportunityProject);
} else {
     // What should you do if there isn't a template record available?
}

insert toUpdate;

顺便提一下,Salesforce特定问题有一个专用的salesforce.stackexchange.com