【问题标题】:Salesforce triggering Contact (PA) owner change when Account Owner is changed fails if the previous owners of the Contacts (PA) aren't the same user如果联系人 (PA) 的先前所有者不是同一用户,则在更改帐户所有者时 Salesforce 触发联系人 (PA) 所有者更改失败
【发布时间】:2018-11-15 11:10:53
【问题描述】:

我有一个设置,我们有帐户和个人帐户。在某些情况下,个人客户通过 AccountContactRelation 链接到客户。如果客户的特殊记录类型的所有者发生更改,我们希望将所有相关个人客户的所有者更改为与新客户所有者相同的所有者。

我为此编写了一个 APEX 触发器,如果​​所有相关个人帐户的当前所有者都是同一个用户,那么一切正常。另一方面,如果某些个人帐户具有不同的用户作为所有者,则触发器将失败并显示错误消息:

ArmAccountOwnerChange: execution of BeforeUpdate, caused by: System.DmlException: Update failed. First exception on row 0 with id 0010Q0000050J0FQAU; first error: INVALID_FIELD, All accounts must have the same current owner and new owner.: [OwnerId] Trigger.ArmAccountOwnerChange: line 23, column 1

这里是触发器。知道为什么会这样吗?

trigger ArmAccountOwnerChange on Account (before update) {

    List<AccountContactRelation> acr = New List<AccountContactRelation>();
    List<Account> acc = New List<Account>();
    RecordType rec = [Select Id From RecordType Where DeveloperName = 'ARM_Account' Limit 1];

    for(Account a: Trigger.new) {

        if(a.OwnerId != Trigger.oldMap.get(a.id).OwnerId && a.RecordTypeId == rec.id) {
            acr = [SELECT Id, ContactId From AccountContactRelation Where AccountId = :a.id];

                If(acr.size() > 0) {
                    for(AccountContactRelation b: acr) {
                        acc.addAll([Select Id, OwnerId From Account Where PersonContactId = :b.ContactId]);
                    } 
                }

                If(acc.size() > 0) {
                    for(Account c: acc) {
                        c.OwnerId = a.OwnerId;
                    }
                }
            update acc;
        }
}       

}

【问题讨论】:

    标签: triggers salesforce apex


    【解决方案1】:

    您应该为每个平等的所有者设置一个帐户列表,然后对该列表中的每个执行更新。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-02-07
      • 1970-01-01
      • 2021-10-29
      • 2011-12-06
      • 2014-01-28
      • 2020-08-15
      • 1970-01-01
      相关资源
      最近更新 更多