Hey guys, today in this post we are going to learn about how to update the Custom last modified date field in the parent object when the Records in the child object is updated using Apex Trigger in Salesforce.
Real time scenarios:- Write a trigger on Child object and update the custom Last Modified Date Field, LastModifiedDate__c (Date/Time Type) in the Parent Object When the Records in the Child Object is Updated.
parentObjTrigger:- Custom Fields & Relationships
Create a custom field on parent Object name as LastModifiedDate__c (Date/Time Type).
childObjTrigger:- Custom Fields & Relationships
Create a lookup relationship field on child Object name as childLookup__c :: Lookup(parentObjTrigger).
Files we used in this post example:-
LastModifiedDateTrigger.apxt | Apex Class Trigger | It will be fire whenever update a record on child object |
Parent Cusotm Object:- parentObjTrigger__c Custom Field:- LastModifiedDate__c |
Lightning Component | Trigger on child custom object and update the custom Last Modified Date Field in the Parent Object Whenever the records in the Child Object is changed. |
Child Custom Object:- childObjTrigger__c Custom Field:- childLookup__c : Lookup(parentObjTrigger) |
Child Object with Custom Lookup Field | childLookup__c : Lookup(parentObjTrigger) field it is help to communicate between parent object. |
Final Output
Other post that would you like to learn
Step 1:- Apex Class Trigger : LastModifiedDateTrigger.apxt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
trigger LastModifiedDateTrigger on childObjTrigger__c (before insert, after insert, before update, after update) { Map<Id,Datetime> idLastModifiedDateMap = new Map<Id,Datetime >(); List<Datetime> childIdList = new List<Datetime>(); Set<Id> setIdList = new Set<Id>(); if(trigger.isBefore){ system.debug('trigger before event'); }else if(trigger.isAfter){ system.debug('trigger after event'); if(trigger.isUpdate){ for(childObjTrigger__c temp : trigger.new) { childIdList.add(temp.LastModifiedDate); } system.debug('childIdList ' + childIdList); List<parentObjTrigger__c> prntList= [Select Id, LastModifiedDate__c, (Select Id, LastModifiedDate From childObjTriggers__r Where LastModifiedDate=:childIdList) From parentObjTrigger__c]; List<parentObjTrigger__c> lastDate = new List<parentObjTrigger__c>(); for(parentObjTrigger__c p1:prntList){ for(childObjTrigger__c c1:p1.childObjTriggers__r){ system.debug('c1 ' + c1.LastModifiedDate); p1.LastModifiedDate__c=c1.LastModifiedDate; lastDate.add(p1); } } update prntList; system.debug('lastDate1### ' + lastDate); } } } |
Other related post that would you like to learn…
You’re a beautiful inspiration. It really helps me in any situation. Where I stuck. Many of my friends told me to comment there post but I stuck what I should comment. Finally I got your post it always help me. Thanks for the lovely post.