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
Irish@accIrish@acc 

Need help creating a related list record

Hi All,

 

I have written a trigger which will add a record in the related list, the condition for adding the record is that...

1. <Is Launch>: TRUE first, FALSE later 2. <Track Root Cause>: TRUE first, FALSE later. 3. <Actual Completion Date>: descending (later first)

 

it should check the first two value if condition doesn't met then sort the related list based on Actual completion date and copy that record in the new record..below are my code....

trigger CreateRootCause1 on Project__c (after update) {

for(Project__c prj:Trigger.new){
if(trigger.oldmap.get(prj.Id).Project_State__c!=trigger.newmap.get(prj.Id).Project_State__c)
{
  if(trigger.newmap.get(prj.id).Project_State__c=='In Cancellation'||  trigger.newmap.get(prj.id).Project_State__c=='In Closing'){
 List<Task__c> lsttask=new List<Task__c>();
 lsttask=[select id,name,Project__c,Is_Launch__c,Actual_completion_date__c,Target_Date_current_baseline__c from Task__c where (Is_Launch__c=true OR Track_Root_Cause__c=true) AND Project__c=:prj.id Order by Actual_completion_date__c desc limit 1];
 List<Root_Cause__c> lstrc=new List<Root_Cause__c>();
for(Task__c t:lsttask){
     Root_Cause__c RCDel= new Root_Cause__c();
     RCDel.TaskName__c=t.Id; 
     RCDel.Name=t.Name +' - '+'Final';
     RcDel.Project_Name__c=prj.id;         
     RCDel.Date__c=System.Today();            
     RCDel.Launch_Date_Delay__c=0;
     RCDel.Apex_Context__c=TRUE;          
                
     lstRC.add(RCDel);           
      
}
    insert lstRc;
}
}
}




}
Irish@accIrish@acc
here am not abe to achieve the 3rd condition if both the second condition fails then it should copy the record from 3rd one
Bindhyachal Kumar SinghBindhyachal Kumar Singh

Hi Irish@acc

 

Use following instead of you query:

 

lsttask=[select id,name,Project__c,Is_Launch__c,Actual_completion_date__c,Target_Date_current_baseline__c from Task__c where Project__c=:prj.id Order by Actual_completion_date__c,Is_Launch__c,Track_Root_Cause__c desc limit 1];