【问题标题】:Trigger on Task Salesforce触发任务 Salesforce
【发布时间】:2014-02-03 05:06:37
【问题描述】:

我正在尝试编写一个触发器,以将“姓名”字段(API 中的 WhoId)更新为与任务相关的帐户的自定义主要联系人字段的“姓名”(ContactId)。

trigger updateNameToPrimary on Task (after insert, after update) {
    for(Task t : Trigger.new) {
        t.WhoID = [SELECT Account.Id 
                   FROM Account 
                   WHERE Id = :t.Id].Custom_Primary_Contact__c;        
    }
}

我一直在做一些测试,但认为它不起作用,无法弄清楚原因。只是在寻找正确方向的一点,因为我仍在学习过程中。

【问题讨论】:

    标签: salesforce apex


    【解决方案1】:

    将触发器从“更新后插入后”更改为“更新前插入前”。 像这样的

    trigger updateNameToPrimary on Task (before insert, before update) {
        set<Id> accIdSet = new  set<Id>();
        for(Task t : Trigger.new) {
           if(t.AccountId!=null)
            accIdSet.add(t.AccountId);        
        }
       map<Id,Account> accMap = new map<Id,Account>([select Custom_Primary_Contact__c from Account where Id in:accIdSet]); 
       for(Task t : Trigger.new) {
            if(t.AccountId!=null && accMap.containsKey(t.AccountId))
            t.WhoID = accMap.get(t.AccountId).Custom_Primary_Contact__c;      
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-17
      • 1970-01-01
      • 2018-02-16
      • 1970-01-01
      • 2017-06-19
      相关资源
      最近更新 更多