【发布时间】: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