//根据案件状态,更新账户评级。仅当案件状态已关闭时。 如果情况 1 关闭,则评级为冷;如果情况 2 关闭,则评级为热。 我写了下面的代码,但似乎不起作用。
public with sharing class AccountRatingUpdateViaCaseStatusClass {
public static void checkCaseStatus(List<Case> caseList, Map<Id, Case> oldMap){
System.debug(' rsn Trigger is executing : ' + Trigger.isExecuting);
Set<Id> caseIDs = new Set<Id>();
if(!caseList.isEmpty()){
for(Case cc : caseList){
if(oldMap != null){
Case oldCases = oldMap.get(cc.Id); // to get old cases
if(cc.AccountId != oldCases.AccountId){ // when user changes parent account of case
System.debug('rsn 1: Account changed for Case - Old Account: ' + oldCases.AccountId + ', New Account: ' + cc.AccountId);
caseIDs.add(oldCases.AccountId);
caseIDs.add(cc.AccountId);
}
else if(oldCases.IsClosed != cc.IsClosed){ // for update
System.debug('rsn 2: Case status changed - Case: ' + cc.Id);
caseIDs.add(cc.AccountId);
}
else if(cc.IsClosed == true){ // insert, delete, undelete
System.debug('rsn 3: Case closed - Case: ' + cc.Id);
caseIDs.add(cc.AccountId);
}
}
}
}
List<AggregateResult> filteredCase = [SELECT AccountId, COUNT(Id) numClosedCases
FROM Case WHERE AccountId IN :caseIDs AND IsClosed = true GROUP BY AccountId];
System.debug('rsn 5: Filtered cases: ' + filteredCase);
List<Account> accountsToUpdate = new List<Account>();
if(!caseIDs.isEmpty()){
System.debug('rsn 4: Case IDs to process: ' + caseIDs);
for(AggregateResult ar : filteredCase){
System.debug('rsn 6: AggregateResult: ' + ar);
Id accountId = (Id)ar.get('AccountId');
Integer numClosedCases = (Integer)ar.get('numClosedCases');
String accountRating = '';
if(numClosedCases >= 2){
accountRating = 'Hot';
} else if(numClosedCases == 1){
accountRating = 'Cold';
}
// Fetch existing Account record and update rating
Account acc = new Account();
acc.Id = accountId;
acc.Rating = accountRating;
accountsToUpdate.add(acc);
}
System.debug('rsn 7: Accounts to update: ' + accountsToUpdate);
if(!accountsToUpdate.isEmpty()){
update accountsToUpdate;
}
System.debug('rsn 8: Accounts updated');
}
}
}
`在 Case 上触发 AccountRatingUpdateViaCaseStatusTrigger(插入后、更新后、删除后、取消删除后){ // AccountRatingUpdateViaCaseStatusClass.checkCaseStatus(trigger.new, trigger.oldmap);
if(Trigger.isAfter && Trigger.isUpdate){
AccountRatingUpdateViaCaseStatusClass.checkCaseStatus(Trigger.new, trigger.oldmap);
}
else if(Trigger.isAfter && Trigger.isDelete){
AccountRatingUpdateViaCaseStatusClass.checkCaseStatus(Trigger.old, null);
}
else if(Trigger.isAfter && (trigger.isInsert || trigger.isUndelete)){
AccountRatingUpdateViaCaseStatusClass.checkCaseStatus(Trigger.new, null); //A insert and undelete
}
}`
它正在执行但不更新值
Try below one by creating a custom field IsClosed__c (Checkbox field) should be {unchecked by default} on Case object, it will work for all the scenarios in bulk(Insert, Update(which includes Reparenting),Delete and Undelete).
Trgr: ->
----
trigger AccountRatingTrgr on Case(after insert, after update, after delete, after undelete){
if(Trigger.isAfter){
if(Trigger.isUpdate){
AccountRatingTrgrCls.AccountRatingTrgrMethod(Trigger.New,Trigger.OldMap);
}else if(Trigger.isDelete){
AccountRatingTrgrCls.AccountRatingTrgrMethod(Trigger.Old,null);
}else{
AccountRatingTrgrCls.AccountRatingTrgrMethod(Trigger.New,null);
}
}
}
Modular Class: ->
-------------
public class AccountRatingTrgrCls{
public static void AccountRatingTrgrMethod(List<Case> caseList,Map<Id,Case> oldCaseMap){
String accountRating;
Set<Id> setOfAccountIds = new Set<Id>();
Map<Id,Decimal> accountMap = new Map<Id,Decimal>();
List<Account> accountList = new List<Account>();
List<Account> accountListToUpdate = new List<Account>();
if(caseList!=null && caseList.size()>0){
for(Case cses : caseList){
if(cses.IsClosed__c){
if(oldCaseMap==null){
setOfAccountIds.add(cses.AccountId);
}else{
if(cses.AccountId != oldCaseMap.get(cses.Id).AccountId){
setOfAccountIds.add(cses.AccountId);
setOfAccountIds.add(oldCaseMap.get(cses.Id).AccountId);
}else{
setOfAccountIds.add(cses.AccountId);
}
}
}
}
if(setOfAccountIds.size()>0 && setOfAccountIds!=null){
for(AggregateResult agr : [Select AccountId, Count(Id) countOfCaseIds FROM Case WHERE AccountId IN: setOfAccountIds GROUP BY AccountId]){
accountMap.put((Id)agr.get('AccountId'),(Integer)agr.get('countOfCaseIds'));
}
}
accountList = [Select Id,Rating FROM Account WHERE ID IN: setOfAccountIds];
if(accountList.size()>0 && accountList!=null){
for(Account accounts : accountList){
if(!accountMap.isEmpty() && accountMap!=null){
accountRating='';
if(accountMap.containsKey(accounts.Id)){
if((Integer)accountMap.get(accounts.Id)==0){
accountRating = '';
}else if((Integer)accountMap.get(accounts.Id)>0 && (Integer)accountMap.get(accounts.Id)<=2){
accountRating = 'Cold';
}else if((Integer)accountMap.get(accounts.Id)>2 && (Integer)accountMap.get(accounts.Id)<=6){
accountRating = 'Warm';
}else if((Integer)accountMap.get(accounts.Id)>6){
accountRating = 'Hot';
}
accountListToUpdate.add(new Account(Id=accounts.Id,Rating=(String)accountRating));
}else{
accountListToUpdate.add(new Account(Id=accounts.Id,Rating=''));
}
}else{
accountListToUpdate.add(new Account(Id=accounts.Id,Rating=''));
}
}
}
if(accountListToUpdate.size()>0 && accountListToUpdate!=null){
UPDATE accountListToUpdate;
}
}
}
}