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
Simran SuriSimran Suri 

Apex Trigger to copy & insert/update related records from Child Object to Parent object

Hi Folks,
I am fairly new to Apex Development/Triggers, so, I need your help urgently. I am working with WorkOrder, WorkOrderLineItem & SkillRequirement standard objects. When the Skill requirement related record is created/updated on the Work Order Line Item(child), it should copy the same related record & insert/update it to the Skill Requirement related list on the parent WorkOrder(parent) based on condition that RelatedRecordId is a Work Order Line Item and Skill does not already exists on parent work order. Below is a brief synopsis:
  • Roll-up of skills from WOLI to parent WO
  • When: Skill Requirement is created or updated
  • Criteria condition: RelatedRecord is a Work Order Line Item and Skill does not exist on parent work order
  • What: Create Skill Requirement for Work Order (same skill level)
I know it will be quite simple task for you but will appreciate your help/guidance/code snippet examples to get me started.
Thanks in advance.
 
Harshit Garg 6Harshit Garg 6
Hi Simran,

You can take help from below code.
List<Opportunity> opps = new List<Opportunity>();
     Opportunity oppty;

     for(Memo__c memo: Trigger.new)
      {
           oppty = new Opportunity(Id=memo.Opportunity__c);
           oppty.Last_Memo_Date__c = memo.CreatedDate;   //this is a custom DateTime field in Memo
           oppty.Last_Memo__c = memo.Subject__c;             //this is a custom text field
           oppty.Most_Recent_Memo__c = memo.id;              //this is a custom lookup field
           opps.add(oppty);
       }
      
      if(opps.size() > 0)
      Update opps;

}

}

Below is the link.Please follow the steps.

Link - http://salesforce.stackexchange.com/questions/42583/trigger-to-update-child-records-based-on-parent-record

Thanks,
Harshit garg
harshitgarg2591@gmail.com