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
Amit Singh.ax1042Amit Singh.ax1042 

trigger to copy one record of child object into another childs object?

hi friends,

i am getting problem while creating trigger on one of my object...

i have created following objects-

Job

Job Task

Job Task Role

 

and

Schedule Template

Schedule Task

Schedule Task Role

 

1)job Task is child of Job,And Job Task Role is child of Job Task

2)Schedule Task is child of Schedule Template,And Schedule Task Role is child of Schedule Task

and

Job Task is same as Schedule Task (i am talking about fields-fields are same)

Job Task Role is same as Schedule Task Role (" "    "      ").

 

and there is look up relationship between job and schedule template (job is child and schedule template is master..)

 

i want that when i create a job, ishould select a schedule template.... then.. when i save..all schedule task of that particular template should be copy into job task (record)....and all schedule task role should be in job task role...

 

example-

i have created a schedule template as ST and

in ST there is two record for schedule task as  task 1 and task 2

in task 1 there is one record for schedule task role say role 1

in task 2 there is one record for schedule task role say role 2

 

and now i am going to create a job

job1

in this if i am selecting schedule template as ST then all schedule task should copy into job task and all schedule task role should copy into jobtaskrole (for repected job task, i mean role1 should copy into task1 and role 2 should copy into task2)...

 

i have written following trigger for this... and it is working fine ...

all schedule task i am getting into job task... but problem is for job task role (i am able to copy schedule task role into job task role table but not for particular Job task----- in example role 1 and role 2 both schedule task role is copied for task 2 )...

 

how to solve this....

please make some modification into following codes..

 

 

 

 

 

trigger CopyScheduleTaskInJobTask on Job__c (after insert,after update)
{
    Set<Id> stScheduleTemplateId = new Set<Id>();
    Set<Id> stScheduleTaskId = new Set<Id>();
    Set<Id> stJobTaskId = new Set<Id>();
  
    List<Job_Task__c> lstJobTask = new List<Job_Task__c>();
    List<Job_Task_Role__c> lstJobTaskRoleNew=new List<Job_Task_Role__c>();
    List<Schedule_Template__c> lsttemp = new List<Schedule_Template__c>();
    String strJobId,strSTId,strJobTaskId,str;
  
   If(Trigger.isAfter)
   {
       for(Job__c j :Trigger.New)
       {
              stScheduleTemplateId.add(j.Schedule_Template__c);
              strJobId = j.Id;
              strSTId=j.Schedule_Template__c;
       }
     
      system.debug('set..............'+stScheduleTemplateId);
       
      List<Schedule_Task__c> lstScheduleTask = new List<Schedule_Task__c>();
      lstScheduleTask = [Select Name from Schedule_Task__c where Schedule_Template__c IN: stScheduleTemplateId ORDER BY Name];
     
     
       for(Schedule_Task__c s : lstScheduleTask)
       {
            Job_Task__c jt = new Job_Task__c();
            jt.Name = s.Name;
            jt.Job__c = strJobId;
           
            lstJobTask.add(jt);
        }
             insert lstJobTask;
            
              for(Schedule_Task__c st : lstScheduleTask) //For every task has one or more role. These roles are coming from ScheduleTaskRole__c
             {
                 stScheduleTaskId.add(st.Id); // here need to pass ScheduleTaskId, so that all the roles can be retrived whose Schedule Task is stScheduleTaskId
             }
      
      
              Map<Id,Id> mpCustom = new Map<Id,Id>();
             
              for(Job_Task__c jt2: [select Id,Name,Job__r.Schedule_template__c from Job_Task__c where Job__r.Schedule_template__c IN : stScheduleTemplateId ])
               {
                  
                    mpCustom.put(jt2.Job__r.Schedule_template__c,jt2.Id);
                    strJobTaskId = mpCustom.get(jt2.Job__r.Schedule_template__c);
               }
              
               system.debug('set..............'+stScheduleTaskId);
      
               List<Schedule_Task_Role__c> lstScheduleTaskRole=new List<Schedule_Task_Role__c>(); // Created list instance for ScheduleTaskRole__c
               lstScheduleTaskRole=[Select Id,Name,Schedule_Task__c,Schedule_Task__r.Schedule_Template__c,Schedule_Task__r.Name,Schedule_Task__r.Id from Schedule_Task_Role__c where Schedule_Task__c IN : stScheduleTaskId];
      
    
      
               for(Schedule_Task_Role__c st1:lstScheduleTaskRole)
               {   
                    Job_Task_Role__c jt1=new Job_Task_Role__c(); // INstance of Job_Task_Role__c
                    // Now assign role and schedule task from ScheduleTaskRole__c to Job_Task_Role__c
                   //  jt1.Job__c=strJobId; // assign JobId to Job_Task_Role__c.Job__c
                       jt1.Name=st1.Name;
                     jt1.Job_Task__c=mpCustom.get(st1.Schedule_Task__r.Schedule_Template__c);
                  
                   // jt1.Job_Task__c= strJobTaskId;
                    lstJobTaskRoleNew.add(jt1); // add into the list instance
                }
   
                insert lstJobTaskRoleNew;  // Insert list instance      
  
            
      }
}

SteveBowerSteveBower

 

Suggestion #1.  Where is the code for your test case?  Write your test case now and then use it to test your code.   I believe that if you had written your test case to test several records in one trigger call, you might have seen some of your problems.

 

Suggestion #2.  Add comments to your code.  I mean if you're going to create a Map<Id, Id>, have the courtesy to the poor programmer who is going to come after you to try to explain what you're mapping to what.  Sure, he can read the code, but comments are professional.

 

 

#3  You have several instances where, inside of For loops you assign something to a constant.   For example:

 

 for(Job__c j :Trigger.New)
       {
              stScheduleTemplateId.add(j.Schedule_Template__c);
              strJobId = j.Id;
              strSTId=j.Schedule_Template__c;

      }

 

This is functionally equivalent to:     

 

 for(Job__c j :Trigger.New) stScheduleTemplateId.add(j.Schedule_Template__c);

// And now, just use the last one in the trigger.
strJobId = Trigger.new[Trigger.size()-1)].id;

strSTId=Trigger.new[Trigger.size()-1)].Schedule_Template__c;

 

 

 

Which probably isn't what you want to be doing.  You do this above, as well as here:



strJobTaskId = mpCustom.get(jt2.Job__r.Schedule_template__c);

 

I think this is the immediate cause of your problem, but clearly you aren't handling the case where there is more than one record in the trigger.

 

 

#4 (Not a bug, but not necessary)  There is no reason to create an empty List, and then create another list.  The code:

 

 List<Schedule_Task_Role__c> lstScheduleTaskRole=new List<Schedule_Task_Role__c>(); // Created list instance for ScheduleTaskRole__c

 lstScheduleTaskRole=[Select Id,Name,Schedule_Task__c,Schedule_Task__r.Schedule_Template__c,Schedule_Task__r.Name,Schedule_Task__r.Id from Schedule_Task_Role__c where Schedule_Task__c IN : stScheduleTaskId];

 

is exactly equivalent to:

 List<Schedule_Task_Role__c> lstScheduleTaskRole = [Select Id,Name,Schedule_Task__c,Schedule_Task__r.Schedule_Template__c,Schedule_Task__r.Name,Schedule_Task__r.Id from Schedule_Task_Role__c where Schedule_Task__c IN : stScheduleTaskId];

 

 

#5 I think there are instances where you might want to use a Map<Id, List<Schedule_Tasks_Role__c>>() sort of structure...

 

#6 Seems to me that if the fields in the Schedule Task Role object are the same as those for a Job Task Role; and the Schedule Task is the same as the Job Task; and the Job is the same as the Schedule Template, then why not just use one set of objects instead of two and have a differentiating flag in the top level object (or a record type) that says if this record is a Template or not.

 

I'd probably rethink the object's that you're using and then start from scratch since handling multiple records in the trigger will mean you have to rewrite most all of it.

 

Hope this helps...  Best, Steve.