You need to sign in to do that
Don't have an account?
Laura 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!
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
You need to write a trigger on the task object.
Thanks ,
Raj
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();
}
Thanks
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?
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;
}
as the trigger has zero code coverage, you cannot deploy same without 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;
}
}