1

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.

2

I believe this is due to the Trigger order of operations. Your trigger is on your Parent_object, and I assume your workflow is as well. If you look at the order of execution for triggers, Workflow rules aren;t fired until after 'after context' triggers are executed.

https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_triggers_order_of_execution.htm

So at the time of execution for this trigger, your tasks are not created yet. I think what you really want is to make the trigger on the task object, rather than the Parent_object. When the tasks, are created you can query for the Parent objects, and make your changes accordingly.

  • Thanks for the update... you are right.. i need to make the trigger on task object – test May 29 '14 at 14:01
  • No prob. Glad to help. – Chris Duncombe May 29 '14 at 14:10
  • I tried giving it in task trigger but it is hitting the SQL Limits – test Jun 3 '14 at 14:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.