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
Laura BrockLaura Brock 

I created a new custom object (Payroll Checklists). Multiple tasks will be associated with each checklist. Upon completion of all tasks on the custom object, I need the field 'Checklist Complete?' to be auto-checked. How can I do this?

API name of custom object is "Payroll_Checklist__c".  I created a Field on this object called 'Checklist Complete?' (API Name = "Checklist_Complete__c").  Each Checklist can have 3 - 9 different tasks associated with it.  When every task status is 'Completed' (or ultimately removed from the Checklist), I want the checkbox to be trigged from default 'false' to 'true'.  It seems regular workflow will not work on this (the custom object is not 'related' to tasks).  How can I achieve this?  Seems doable, but a bit beyond my capabilities!
Best Answer chosen by Laura Brock
Anurag SaxenaAnurag Saxena

Here is the Updated code please try it once.


trigger TaskCompletion on Task (after insert,after update) {
    
    list<task> lst_Task = new list<task>(); 
    
    set<id> payrollchecklist_ID = new set<id>();
     
    String Payroll_Checklist_prefix= Payroll_Checklist__c.sObjectType.getDescribe().getKeyPrefix();
    string str;
    for(task task_obj :trigger.new)
    {
        str =String.valueOf(task_obj.WhatId);
        if(task_obj.whatid!=null && str.startsWith(Payroll_Checklist_prefix))
            payrollchecklist_ID.add(task_obj.whatid);
    }
    
    List<Payroll_Checklist__c> Payroll_chklst = new list<Payroll_Checklist__c>([SELECT ID,Checklist_Complete__c, (Select id,whatid,isclosed from tasks ) FROM Payroll_Checklist__c WHERE ID IN : payrollchecklist_ID ]);
    
   
    for (Payroll_Checklist__c pc: Payroll_chklst ) 
    { 
         Integer count = 0; 
         for(Task tsk : pc.Tasks) 
         { 
            if(tsk.WhatId != null  && !tsk.IsClosed)  
                count += 1; 
         }   
         if(count == 0)
             pc.Checklist_Complete__c= true; 
     } 
    update Payroll_chklst;    
}

All Answers

Raj VakatiRaj Vakati
Hi Laura, 

You need to write a trigger on the task object.  
Thanks ,
Raj 
Anurag SaxenaAnurag Saxena
Use this it will work:)



trigger TaskCompletion on Task (after insert,after update) {
    
    list<task> lst_Task = new list<task>(); 
    set<id> cID = new set<id>();
    
    Map<id,Payroll_Checklist__c> MP_chklst;
    
        for(task task_obj :trigger.new)
    {
            cID.add(task_obj.whatid);
    }

    MP_chklst = new Map<id,Payroll_Checklist__c>([SELECT ID,Checklist_Complete__c FROM Payroll_Checklist__c WHERE ID IN : cID ]);
    
    for(task task_obj :trigger.new)
    {
        if(task_obj.status == 'Completed')
        {
          MP_chklst.get(task_obj.whatid).Checklist_Complete__c = true;
        }
    }
    
        update MP_chklst.values();
}
Anurag SaxenaAnurag Saxena
if this will help you,mark as best answer

Thanks
Laura BrockLaura Brock
We are so close! This works, but the ‘Completed’ is being marked after one task is completed. The Checklist might have multiple tasks associated with it – I need the ‘Completed’ box to be checked once all related tasks have been completed. What would I need to change in the code to achieve that?
Anurag SaxenaAnurag Saxena

Let me re-iterate your requirement:

Suppose you have five tasks on a payroll checklist.
Once all these five tasks will be completed, then the only checkbox on the payroll checklist should get checked.

Could you please confirm this?

Laura BrockLaura Brock
Correct: If there were five tasks on Payroll checklist, once ALL have been completed, the ‘Checklist Complete?’ box should be checked. Another Payroll checklist might have nine tasks associated with it. Again, I only want the ‘Checklist Complete?’ to be checked after completion of all nine tasks. Thank you!
Anurag SaxenaAnurag Saxena

Here is the Updated code please try it once.


trigger TaskCompletion on Task (after insert,after update) {
    
    list<task> lst_Task = new list<task>(); 
    
    set<id> payrollchecklist_ID = new set<id>();
     
    String Payroll_Checklist_prefix= Payroll_Checklist__c.sObjectType.getDescribe().getKeyPrefix();
    string str;
    for(task task_obj :trigger.new)
    {
        str =String.valueOf(task_obj.WhatId);
        if(task_obj.whatid!=null && str.startsWith(Payroll_Checklist_prefix))
            payrollchecklist_ID.add(task_obj.whatid);
    }
    
    List<Payroll_Checklist__c> Payroll_chklst = new list<Payroll_Checklist__c>([SELECT ID,Checklist_Complete__c, (Select id,whatid,isclosed from tasks ) FROM Payroll_Checklist__c WHERE ID IN : payrollchecklist_ID ]);
    
   
    for (Payroll_Checklist__c pc: Payroll_chklst ) 
    { 
         Integer count = 0; 
         for(Task tsk : pc.Tasks) 
         { 
            if(tsk.WhatId != null  && !tsk.IsClosed)  
                count += 1; 
         }   
         if(count == 0)
             pc.Checklist_Complete__c= true; 
     } 
    update Payroll_chklst;    
}
This was selected as the best answer
Laura BrockLaura Brock
THANK YOU!
Laura BrockLaura Brock
Okay Anurag, this worked beautifully in UAT. Now, I created same fields/objects in Production but am just realizing I can’t actually create a ‘new’ trigger in Production, but have to actually move and account for test codes. That is also getting very much outside what I have done before. Any additional guidance you can provide on this?
Anurag SaxenaAnurag Saxena
You need to create a test class for that and then deploy in production.
as the trigger has zero code coverage, you cannot deploy same without test class.
Here is my contact Detail, it would be easier to communicate on Skype-
anurag.twopirconsulting
 
Thanks
 
Anurag SaxenaAnurag Saxena
Here is the required Test class

public class TestChecklist 
{
    static testMethod void testmychecklist() 
    {
 //assumed Payroll Checklist has autonumbered genrated field
        Payroll_Checklist__c pc = new Payroll_Checklist__c();
        pc.Checklist_Complete__c=false;
        
        
        insert pc;
            
        task e = new task();
        e.Subject = 'test Task';
        e.Description = 'test_sub';
        e.WhatId = pc.Id;
        e.status = 'Completed';
 
        insert e;

    }
}