I have a workflow on parent object which creates 3 task. When creating a task the description section is updated with text as 'TestLeader'.
Parent Object has field 'TestLeader'.
A trigger(after insert) on parent which loops through this 3 task if desctiption has text 'TestLeader' then update OwnerId field to 'TestLeader' of Parent.
trigger ParentTrigger on ParentObject__c (after insert,after update) {
if(Trigger.isInsert && Trigger.isAfter){
ParentTriggerController.OnAfterInsert(Trigger.new);
}
public Class ParentTriggerController
{
public static void onafterInsert(List<ParentObject__c> lstTest){
List<Task> updateTaskList = new List<Task>();
List<Task> TaskList = new List<Task>();
list<ParentObject__c> listtest = new List<ParentObject__c>();
for(ParentObject__c t: lsttest)
{
listtest= [SELECT TestLeader__c,(SELECT Id, OwnerId, Description
FROM Tasks)
FROM ParentObject__c
WHERE Id = :t.Id];
}
for(ParentObject__c po: listtest)
{
system.debug('Parent Object Id' + po.Id);
TaskList = [SELECT Id, OwnerId, Description
FROM Task
WHERE WhatId = : po.Id];
system.debug('TaskList' + TaskList);
for(Task task:TaskList)
{
system.debug('Task:' +task);
String taskDescription = task.Description;
Task updateTask = new Task();
updateTask.Id = task.Id;
if(taskDescription.contains('TestLeader'))
{
updateTask.OwnerId = po.TestLeader__c;
updateTaskList.add(updateTask);
}
}
}
if(!updateTaskList.isEmpty())
{
update updateTaskList;
}
}
}
I am not able to get the list of task here associated with ParentObject__c. It is giving no records. The task records are created by workflow.