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 Kr SinghAmit Kr Singh 

How to write trigger on parent object when updating parent object then child object record value need to concatenate and display in parent object

Hi All,
In my current scenario 
I am having two Object
1. projects__c(Parent object) (In Project object having field ProjectTaskDescription__c, TimesheetStartDate__c, TimesheetEndDate__c  ) 
2. Timesheet__c(Child Object) (In Timesheet object having field  TimesheetDescription__c, Timesheet_Date__c   )

Now  I want to write a trigger on the Parent object(Projects__c) when user will update TimesheetStartDate, TimesheetEndDate and once he will save then related timesheet object(Timesheet_Date__c) field date is between  Project(TimesheetStartDate, TimesheetEndDate )   then  (TimesheetDescription__c ) field  value need to concatenate and display into project object field ( ProjectTaskDescription__c ) ..

Please help me how to write trigger for this.
Please help me to write a trigger on this scenario.


Thanks in Advance.
 
 
AvaneeshAvaneesh
Hi Amit,

Can you please give an example 
"(TimesheetDescription__c ) field  value need to concatenate and display into project object field ( ProjectTaskDescription__c ) ." please
explain this line 

Thank you
Avaneesh Singh
Amit Kr SinghAmit Kr Singh
Yes "(TimesheetDescription__c ) field  value need to concatenate and display into project object field ( ProjectTaskDescription__c ) 
but when User is entering (TimesheetStartDate__c, TimesheetEndDate__c) in project then if Timesheet_Date__c is between two date then only these TimesheetDescription__c need to dispaly in Project 

Thanks,
AvaneeshAvaneesh
Hi 
here I write a trigger which satisfied your requirement if still any issue then let me know 

before writing this trigger use a recursive  apex class 
public Class AvoidRecursion{
    private static boolean firstRun = true;
    public static boolean isFirstRun(){
    if(firstRun){
      firstRun = false;
      return true;
    }else{
        return firstRun;
    }
    }
}

This is your trigger check proper name of field  and also a lookup name of timesheet that was in my trigger project 
trigger TriggerOnParentObj on Project__c (after update) {
    
    if(AvoidRecursion.isFirstRun()){ 
   set<id> projectRelatedset = new set<id>(); 
    for(Project__c pro:trigger.new){
        projectRelatedset.add(pro.id);
    }
    List<Project__c> bulkofProjectUpdate = new List<Project__c>();
    List<Timesheet__c> timeList = [select id,name,project__r.id,Timesheet_Date__c,TimesheetDescription__c from Timesheet__c where project__r.id IN :projectRelatedset ];
    Map<id,Project__c> currentProjectMap = new Map<id,Project__c>([select id,ProjectTaskDescription__c,TimesheetEndDate__c,TimesheetStartDate__c 
                                                                from project__c where id IN :projectRelatedset]);
    
    for(Timesheet__C tt:timeList){
        if(currentProjectMap.containsKey(tt.project__r.id) && tt.Timesheet_Date__c !=NULL){
            Project__c pro =currentProjectMap.get(tt.project__r.id);
            if(pro.TimesheetStartDate__c >=tt.Timesheet_Date__c && pro.TimesheetEndDate__c <=tt.Timesheet_Date__c){
             pro.ProjectTaskDescription__c = (pro.ProjectTaskDescription__c!=NULL) ? pro.ProjectTaskDescription__c + '  ' + tt.TimesheetDescription__c:tt.TimesheetDescription__c;
            bulkofProjectUpdate.add(pro);
            }
        }
    }    
    if(bulkofProjectUpdate.size()>0)
    update bulkofProjectUpdate;
    }  
}

if this code is helpful please mark as best answer else let me know your problem 


Thank you
Avaneesh Singh