【问题标题】:apex trigger copy record after opportunity update机会更新后的顶点触发器复制记录
【发布时间】:2014-06-26 08:18:03
【问题描述】:

我对 Apex 和触发写作比较陌生。这是我想要完成的任务:

是否可以将自定义对象中的记录复制到在同一对象中创建的新记录中?在我的例子中,当机会设置为“close won”时,我需要在名为 Project__c 的自定义对象中创建一个新记录。但关键是有一个名为 proj1 的现有记录,其中包含需要复制到进入对象 Project__c 的每个新记录的默认数据。

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

【问题讨论】:

    标签: salesforce apex


    【解决方案1】:

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

    例如

    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.stackexchange.com 用于解决 Salesforce 特定问题。

    【讨论】:

    • 感谢您的帮助!!这帮助我完成了触发器。
    猜你喜欢
    • 2017-07-22
    • 1970-01-01
    • 2013-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-30
    相关资源
    最近更新 更多