2 answers
Hi Anutej,please find below complete requirement and code that i have written Create A field on Account1.Case Status (Not Started, In Progress, Partially Done, All Done)1.After first case created, update the Case status on account to ‘Not Started’, 2. Status change from New -> Working. Update Account case Status to In ProgressNote: Check if any other case is still in the new status. Don’t update.3. Status Changed to Closed From WorkingUpdate Account case Status to Partially Done/ All DoneAll the Case is closed - All DoneAt Least one case is not closed - Partially DoneTest Case for 2Account 1Case 1 with new statusCase 2 with new statusUpdate Case 2 status to WorkingExpected: No ChangeUpdate Case 1 Status to WorkingExpected: Account case Status to In Progress, because No cases in new status, all are in Working--------------------------------------------------------------------------------------------------------------------------public class CaseHelperclass { public static void changestatus(List<Case> casevalue) { List<Account> aclist = new List<Account>(); for(Case c : casevalue) { if(c.Status == 'New') { Account acc = new Account(); acc.Case_Status__c='Not Started'; acc.Id=c.AccountId; aclist.add(acc); } } update aclist; } public static void updatenewtoworking(List<Case> data) { Set<Id> acid = new Set<Id>(); List<Account> aclist = new List<Account>(); Map<Id,List<Case>> maplist = new Map<Id,List<Case>>(); for(Case c : data) { if(c.Status=='Working' && c.AccountId !=null) { if(!maplist.containsKey(c.AccountId)) { maplist.put(c.AccountId, new List<Case>()); } maplist.get(c.AccountId).add(c); acid.add(c.AccountId); System.debug('acid id-------'+acid); } } if(acid.size()>0) { aclist = [select id,Case_Status__c from Account where Id IN : acid]; System.debug('-----queries result'+aclist); for(Account acc : aclist) { if(maplist.containsKey(acc.Id)) { acc.Case_Status__c ='In Progress'; } } if(aclist.size()>0) { update aclist; System.debug('-----------SECOND CONDTION'+aclist); } } } public static void updateworkingtoclosed(List<Case> data) { Set<Id> acid = new Set<Id>(); List<Account> aclist = new List<Account>(); Map<Id,List<Case>> maplist = new Map<Id,List<Case>>(); for(Case c : data) { if(c.Status=='Escalated' && c.AccountId !=null) { if(!maplist.containsKey(c.AccountId)) { maplist.put(c.AccountId, new List<Case>()); } maplist.get(c.AccountId).add(c); acid.add(c.AccountId); } } if(acid.size()>0) { aclist = [select id,Case_Status__c from Account where Id IN : acid]; System.debug('-----queries result'+aclist); for(Account acc : aclist) { if(maplist.containsKey(acc.Id)) { acc.Case_Status__c ='All Done'; } else { acc.Case_Status__c ='Partially Done'; } } if(aclist.size()>0) { update aclist; } } } }