Visualforce Apex触发更新错误记录

时间:2013-04-29 10:01:57

标签: triggers salesforce apex-code visualforce

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

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;
                }
            }    
        }
    }
}

2 个答案:

答案 0 :(得分: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. 使用状态“已取消”
  6. 向Beta Corp添加相关作业
  7. 在单个DML操作中更新两个帐户,执行一些微不足道的操作,例如将结算国家/地区从“美国”更改为“美国”
  8. 断言 Alpha Corp的相关分配仍为“已取消”
  9. 我怀疑您共享的代码不会通过单元测试,在这种情况下,您将能够解决问题并修复它。

答案 1 :(得分:0)

虽然Marty的代码回答了我提出的问题,但我也遇到了一些“Too Many Code Statements”错误。以下是似乎解决的最终代码:

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;

}