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