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
dlCamelotdlCamelot 

Trigger Fix? Child Record created based on Parent Record Insert/Update

Hi All,

I am trying to draft a my first trigger from scratch (up to this point, I have only been tweaking code already in existance).  I'm struggling to develop a multi-object trigger.  I would like a child record (task) to be created upon a parent record meeting certain criteria.  Here's the work so far:

trigger ClosedOpportunityTask on Opportunity (after insert,after update) {
    //Maps,Sets,Lists to Be Used
    Set<Id> oppIds = new set<Id>();
    Map<Id,Opportunity> mapTask2Opp = new map<Id,Opportunity>();
    List<Task> taskList = new List<Task>();
    
    //Bulk Trigger Loop
    for (Opportunity o: trigger.new){
        oppIds.add(o.Id);
        mapTask2Opp.put(o.id, o);
    }
   
   //Tasks to be Created
   taskList = [SELECT Id, Subject FROM Task WHERE whatid IN : oppIds];
   
   //Create new task only if one doesn't already exist 
    if(taskList.size()= 0){
        for(Task t: Tasklist){
            t.What = ? //needs to get the parent opp's id
            t.Subject = 'Follow Up Test Task';
        }
        update taskList;
    } 
}


Right now, my logic for finding records without tasks 'taskList.size()= 0' is not effective, nor do I have a good way to link the task back to the parent via the 'Related To' field (API = 'What').

Can anyone lead me in the right direction for fixing?  If you can, please also explain why.  I'd really like to learn from my mistakes!  :)
 
@anilbathula@@anilbathula@
Hi Desha Corey

 try this code :-
trigger ClosedOpportunityTask on Opportunity (after insert,after update) {

    //Maps,Sets,Lists to Be Used
   Map<Id,Opportunity> mapTask2Opp = new map<Id,Opportunity>();    
   List<Task> taskList = new List<Task>();
    
    //Bulk Trigger Loop
    for (Opportunity o: trigger.new){	     
		 mapTask2Opp.put(o.id, o);        
    }
   
   //Tasks to be Created
  list<task> tList = [SELECT Id, Subject FROM Task WHERE whatid IN : mapTask2Opp.keyset()];
   
   //Create new task only if one doesn't already exist 
    if(tList.isEmpty()){
	        for(opportunity op:mapTask2Opp.values()){
			task t=new task();
			t.Whatid = op.id;
			t.Subject = 'Follow Up Test Task';
			tasklist.add(t);
		}
        
        insert tasklist;
    } 
}

Thanks
Anil.B
dlCamelotdlCamelot
Hmm, did not seem to work on a bulk test of records, but this is much closer than what I had. Thank you!  

For clarification, what does "opportunity op:mapTask2Opp.values()" in the FOR loop look for?
@anilbathula@@anilbathula@
Hi Desha Corey,

your correct its not working for bulk records.
mapTask2Opp.values() it contains opportunity object which i am putting on the top.

try these code its working:-
trigger ClosedOpportunityTask on Opportunity (after insert,after update) {

    //Maps,Sets,Lists to Be Used
   Map<Id,Opportunity> mapTask2Opp = new map<Id,Opportunity>();    
   List<Task> taskList = new List<Task>();
    
    //Bulk Trigger Loop
    for (Opportunity o: trigger.new){        
         mapTask2Opp.put(o.id, o);        
    }
   
   //Tasks to be Created
  list<opportunity> oList = [SELECT Id, (select id from tasks)name FROM opportunity WHERE id IN : mapTask2Opp.keyset()];
   
   //Create new task only if one doesn't already exist 
    if(!oList.isEmpty()){
            for(opportunity op:olist){
               if(op.tasks.isempty()){
                        task t=new task();
                        t.Whatid = op.id;
                        t.Subject = 'Follow Up Test Task';
                        tasklist.add(t);
            }
        }
        
        insert tasklist;
    } 
}

Thanks
Anil.B