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
AdiiAdii 

Write a trigger to update a check box field on account object named all cases closed based on all the cases of that account is closed and all the tasks of that case are completed??

ShirishaShirisha (Salesforce Developers) 
Hi Adi,

Greetings!

Please find the sample code below:
 
trigger setCaseClosedCheckboxAccount on Case (after update) 
{

    Map<Id,Id>  AccountToCaseMap = new Map<Id,Id>();

    // Check for correct event
    if(Trigger.isAfter && Trigger.isUpdate)
        for(Case c : trigger.new)
            AccountToCaseMap.put(c.AccountId,c.Id);

    List<Account> AccountUpdate = new List<Account>{};

    for (Account acc: [SELECT Id,Name, CaseClosed__c FROM Account WHERE Id IN:  AccountToCaseMap.keySet()]) 
    {
        Id caId = AccountToCaseMap.get(acc.Id);
        Case ca = trigger.newMap.get(caId);
        if (ca.Status=='Closed' )
        {
            acc.CaseClosed__c=true;
            AccountUpdate.add(acc);
        }

        else if (ca.Status=='Closed' && ca.Status!=null )
        {
            acc.CaseClosed__c =true;
            AccountUpdate.add(acc);
        }
    }

    // update records
    update AccountUpdate;
}
You can make changed accordingly as per your requirement in the above code.

Kindly let me know if it helps you and close your query by marking it as best answer so that it can help others in the future.

Warm Regards,
Shirisha Pathuri
 
AdiiAdii
Thanks, Shirisha.  I was doing same method, if I get stuck somewhere in the middle, I will tell you.
AdiiAdii
**********AllTaskCompleted.apxt**********************
trigger AllTaskCompleted on Task (after update) {
     if(Trigger.isUpdate && Trigger.isAfter)    {  
        AllTaskCompletedHandler ob =new AllTaskCompletedHandler();
        ob.afterUpdate13(trigger.new);
    }
}

***********AllTaskCompletedHandler***********************

public class AllTaskCompletedHandler {
    public void afterUpdate13(list<Task> NewTask ){  
        System.debug('trigger initiated');  
        Set<Id> d = new Set<Id>();
        list<Case> ss1 = new list<Case>{};
        map<id,id> AccCaseMap = new map<id,id>();
        for(Task t : NewTask)
        {
            if(t.Status == 'Completed'){
                String WhatIdObj = t.WhatId;
                if(WhatIdObj.startsWith('500')){
                    d.add(t.WhatId);
                    system.debug('d'+d);
                }
                AccCaseMap.put(t.WhatId, t.Id);
                system.debug('AccCaseMap'+AccCaseMap);
            }                
        }

        
       
        if(!AccCaseMap.isEmpty()){
            AllTaskCompletedHelper.afterUpdate1(AccCaseMap);
        }
    }  
}



************** AllTaskCompletedHelper*********************

public class AllTaskCompletedHelper {
    public static void afterUpdate1(Map <Id,Id> AccCaseMap){   
        list<Account>AccUpdate = new list<Account>{};
        list<Account>Accountlist = new list<Account>{};
        list<Case> ss33 = new list<Case>{};
        list<Case> ss = new list<Case>{};
        list<Task> Tasklist =  [select Id,WhatId,Status from Task where WhatId IN: AccCaseMap.keySet()];
        system.debug('Tasklist'+Tasklist); 
        For(Task t :Tasklist)
        {
            if(t.Status == 'Completed')
            {
                Case c = new case(Id = t.WhatId);
                ss.add(c);
                system.debug('c'+c);  
            }
        }
        list<Case> accc1 = [Select Id,Status,AccountId From Case where Id IN : AccCaseMap.keySet()];
        system.debug('accc1'+accc1);
        For(Case c : accc1)            
        {
            Account a = new Account(Id = c.AccountId);
            system.debug('a.Id'+a.Id);
            Accountlist.add(a);
            system.debug('Accountlist'+Accountlist);
            
        }
        list<Account> v =  [Select Id ,all_cases_closed__c from Account where Id IN : Accountlist];
        system.debug('v'+v);
        list<Case> ALLCase = [Select Id,Status,AccountId From Case where AccountId IN : Accountlist]; 
        system.debug('ALLCase'+ALLCase);
        For(Case cv : ALLCase)
        {
            if(cv.Status == 'Closed' )
            {
                ss33.add(cv);
                system.debug('ss3'+ss33);
            }
        }
        for(Account a : Accountlist)
        {
            if(a.all_cases_closed__c != true){
                   
                    a.all_cases_closed__c =true;
                    system.debug('a'+a);
                    AccUpdate.add(a);
                    system.debug('AccUpdate'+AccUpdate);
                }
        }
        update AccUpdate;
        system.debug('AccUpdate'+AccUpdate);
    }
}
I write this code but it's not working properly. I don't understand that how we check all the task for a particular case is completed.....
Can AnyOne help me?....
 
ShirishaShirisha (Salesforce Developers) 
Hi Adi,

I would suggest you to capture the debug logs while executing to see,which code is causing issue or not working as expected to proceed.

Please refer the below documentation for how to enable the debug logs to proceed.

https://help.salesforce.com/apex/HTViewHelpDoc?id=code_add_users_debug_log.htm&language=en_us

Regards,
Shirisha Pathuri
AdiiAdii
Thank you Shirisha for telling this. I did it and yes this trigger has completed with all conditions...Thanks once again