0

我无法弄清楚为什么这个触发器有时会更新不符合条件的记录。这个想法是,当一个帐户从“暂停”变为活动服务时,任何取消的分配都会返回到待处理状态。我不知道是什么触发了它,但似乎每个人在一段时间内,对于服务没有变化的帐户,分配都不会取消。继承人的代码:

trigger cancelAssignments on Account (before update) {
    List<Assignment__c> masterListA = [select Id, Status__c, Practice__c from Assignment__c where Practice__c IN :Trigger.newMap.keySet() and type_of_work__c != 'a0Qa000000G1WmVEAV' AND (status__c = 'Feedback Needed' OR status__c = 'Pending Review' OR status__c = 'Accepted')];    
    List<Assignment__c> masterListB = [select Id, Status__c, Practice__c from Assignment__c where Practice__c IN :Trigger.newMap.keySet() and type_of_work__c != 'a0Qa000000G1WmVEAV' AND (status__c = 'Canceled')]; 

    for (Account oAccount : trigger.new) {
        if (oAccount.current_services__c == null || oAccount.current_services__c == 'Hold'){
            for (Account oAcct : trigger.old){
                if (oAcct.current_services__c != null && oAcct.current_services__c != 'Hold'){
                    List<Assignment__c> assignmentsToUpdate = new List<Assignment__c>();
                    for (Assignment__c rd : masterListA){
                        if (rd.practice__c == oAccount.id){
                        rd.Status__c = 'Canceled';
                        assignmentsToUpdate.add(rd); 
                    }
                    }update assignmentsToUpdate;
                }
            }
        }
        else if (oAccount.current_services__c != 'Hold' && oAccount.current_services__c != null  ){
            for (Account oAcctB : trigger.old){
                if (oAcctB.current_services__c == 'Hold'){
                    List<Assignment__c> assignmentsToUpdateB = new List<Assignment__c>();
                    for (Assignment__c rdB : masterListB){
                        if (rdB.practice__c == oAccount.id){
                        rdB.Status__c = 'Pending Review';
                        assignmentsToUpdateB.add(rdB); 
                    }
                    }update assignmentsToUpdateB;
                }
            }    
        }
    }
}
4

2 回答 2

0

问题可能如下:

  • masterListB抓取多个帐户的作业
  • 循环中的代码for (Account oAcctB : trigger.old)从不检查处于“保留”状态的旧帐户是否是同一个帐户

一种解决方案可能是进行以下编辑:

/* Old condition replaced:
   if (oAcctB.current_services__c == 'Hold') { */

if (oAcctB.current_services__c == 'Hold' and oAcctB.Id == oAccount.Id) {

为了证明这是否是正确的解决方案,我建议创建一个像这样工作的单元测试:

  1. 创建两个帐户,“Alpha Corp”和“Beta Corp”
  2. 将 Alpha Corp 的当前服务设置为“保留”
  3. 将 Beta Corp 的当前服务设置为“活动”
  4. 向 Alpha Corp 添加状态为“已取消”的相关分配
  5. 向 Beta 公司添加状态为“已取消”的相关分配
  6. 在单个 DML 操作中更新两个帐户,做一些微不足道的事情,比如将账单国家从“美国”更改为“美国”
  7. 断言Alpha Corp 的相关转让仍然“取消”

我怀疑您共享的代码不会通过单元测试,在这种情况下,您可以将问题归零并修复它。

于 2013-05-01T10:59:43.390 回答
0

虽然 Marty 的代码回答了我提出的问题,但我也遇到了一些“代码语句过多”的错误。这是似乎可以解决该问题的最终代码:

trigger cancelAssignments on Account (before update) {
List<account> quitingAccounts = new List<account>();
List<account> returningAccounts = new List<account>();
List<Assignment__c> assignmentsToCancel;
List<Assignment__c> assignmentsToReturn;
for (Account oAccount : trigger.new)
    {
    if (oAccount.current_services__c == null || oAccount.current_services__c == 'Hold')
        {
        for (Account oAcct : trigger.old)
            {
            if (oAcct.current_services__c != null && oAcct.current_services__c != 'Hold' && oAcct.Id == oAccount.Id)
                {                    
                quitingAccounts.add(oAcct);                                      
                }
            }
        }
    else if (oAccount.current_services__c != 'Hold' && oAccount.current_services__c != null  )
        {
        for (Account oAcctB : trigger.old)
            {
            if (oAcctB.current_services__c == 'Hold'  && oAcctB.Id == oAccount.Id)
                {
                returningAccounts.add(oAcctB);
                }
            }    
        }
    }        
assignmentsToCancel = [select Id, Status__c, Practice__c from Assignment__c  where Practice__c IN :quitingAccounts and type_of_work__c != 'a0Qa000000G1WmVEAV' AND (status__c = 'Feedback Needed' OR status__c = 'Pending Review' OR status__c = 'Accepted')];
assignmentsToReturn = [select Id, Status__c, Practice__c from Assignment__c  where Practice__c IN :returningAccounts and type_of_work__c != 'a0Qa000000G1WmVEAV' AND status__c = 'Canceled'];
for (Assignment__c rd : assignmentsToCancel)
    {
    rd.Status__c = 'Canceled';
    }
for (Assignment__c rd : assignmentsToReturn)
    {
    rd.Status__c = 'Pending Review';
    }
update assignmentsToCancel;
update assignmentsToReturn;

}

于 2013-05-08T12:41:03.027 回答