function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Rakesh M 20Rakesh M 20 

Hi Alll

My Requirements
---------------------------------------------
Status change from New -> Working. 
Update Account case Status to In Progress
Note: Check if any other case is still in the new status. Don’t update.

my code
---------------------------------
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);
            }
        }
    }
its working for only one record not for multiple records so can anyone help me to find out the solution.

thanks in advance
ANUTEJANUTEJ (Salesforce Developers) 
Hi Rakesh,

Can you comment on the are you checking if there are any cases that are new as I don't see any code in the above snippet that checks this?

Also, can you mention what the Case_status__c is so as to test in our personal demo org and respond back.

Looking forward to your response.

Thanks.
Rakesh M 20Rakesh M 20
Hi Anutej,
please find below complete requirement and code that i have written 


Create A field on Account
1.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 Progress
Note: Check if any other case is still in the new status. Don’t update.
3. Status Changed to Closed From Working
Update Account case Status to Partially Done/ All Done
All the Case is closed - All Done
At Least one case is not closed - Partially Done
Test Case for 2
Account 1
Case 1 with new status
Case 2 with new status
Update Case 2 status to Working
Expected: No Change
Update Case 1 Status to Working
Expected: 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;
            }
        }
    }
    
}