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
Scott.BubScott.Bub 

Populate values in Child records with information from parent records.

Hello All,

A client of mine is trying to send out an email template from an object that lookups to another. I need to pull all of the information down to the children to send off multiple email alerts with all of the information on the parent. There are more than 15 fields that I need to populate in the child record from the parent so formula fields are out of the question along with field updates. Does anybody have some code or know how to create a trigger that basically triggers an update to a child record when the parent is saved? I also need the fields to populate at the time of the save so when the workflow rule triggers to send out the email alert we have all of the information that we need.

The objects are Work Order and ServiceMax Event. The ServiceMax Event object is the child to the Work Order object. ServiceMax Events are created when dispatching Work Orders to technicians and each technician is associated to a ServiceMax Event, but only one is associated directly to the Work Order. We need an email alert to trigger to each technician upon confirmation, but only one is directly related to the Work Order. So, when confirming the work order I need a trigger to edit all of the children to bring over the information on the Work Order object to the ServiceMax Event records that are related and upon the save an email alert will go out to each of them.

Does anyone know a way to do this? Please help!
Best Answer chosen by Scott.Bub
Arthur LockremArthur Lockrem
You are correct, a trigger can do this for you.  Please adjust object and field names as needed.
trigger WorkOrder_UpdateServiceMaxEvents on Work_Order__c (before insert, before update) {

	List<Work_Order__c> workOrders =
		[SELECT Id, Name, Field_1__c, Field_2__c,
			(SELECT Id, Name, Parent_Field_1__c, Parent_Field_2__c
			 FROM ServiceMax_Events__r)
		 FROM Work_Order__c
		 WHERE Id IN : trigger.new];
		 
	List<ServiceMax_Event__c> updateServiceMaxEvents = new List<ServiceMax_Event__c>();
		 
	for (Work_Order__c workOrder : workOrders) {
		for (ServiceMax_Event__c serviceMaxEvent : workOrder.ServiceMax_Events__r) {
			serviceMaxEvent.Parent_Field_1__c = workOrder.Field_1__c;
			serviceMaxEvent.Parent_Field_2__c = workOrder.Field_2__c;
			updateServiceMaxEvents.add(serviceMaxEvent);
		}
	}
	
	update updateServiceMaxEvents;
}
I haven't tested this.  The concept is the important part.

All Answers

Arthur LockremArthur Lockrem
You are correct, a trigger can do this for you.  Please adjust object and field names as needed.
trigger WorkOrder_UpdateServiceMaxEvents on Work_Order__c (before insert, before update) {

	List<Work_Order__c> workOrders =
		[SELECT Id, Name, Field_1__c, Field_2__c,
			(SELECT Id, Name, Parent_Field_1__c, Parent_Field_2__c
			 FROM ServiceMax_Events__r)
		 FROM Work_Order__c
		 WHERE Id IN : trigger.new];
		 
	List<ServiceMax_Event__c> updateServiceMaxEvents = new List<ServiceMax_Event__c>();
		 
	for (Work_Order__c workOrder : workOrders) {
		for (ServiceMax_Event__c serviceMaxEvent : workOrder.ServiceMax_Events__r) {
			serviceMaxEvent.Parent_Field_1__c = workOrder.Field_1__c;
			serviceMaxEvent.Parent_Field_2__c = workOrder.Field_2__c;
			updateServiceMaxEvents.add(serviceMaxEvent);
		}
	}
	
	update updateServiceMaxEvents;
}
I haven't tested this.  The concept is the important part.
This was selected as the best answer
Scott.BubScott.Bub
Thank you very much for the quick response. I will test this out and let you know how it works. I needed this especially for the Long Text Area fields.
Arthur LockremArthur Lockrem
You're welcome.  If this resolved your issue please select the answer and mark the question as solved.