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
You can download file directly from github by Click Here.
Other related post that would you like to learn in Salesforce
Step 1:- Apex Class Trigger : LastModifiedDateTrigger.apxt
LastModifiedDateTrigger.apxt [Apex Class Trigger]
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);
}
}
}
Further post that would you like to learn in Salesforce
What triggers last modified date Salesforce?
LastModifiedDate is automatically updated whenever a user creates or updates the record. LastModifiedDate can be updated to any back-dated value if your business requires preserving original timestamps when migrating data into Salesforce.
How do I change the last modified date in Salesforce?
Created Date and Last Modified Date fields in Salesforce objects are audit fields and we can't edit or change these field directly or through code. You have to contact salesforce support team for them to grant permissions to you to edit or change these fields if needed.
What does last modified date mean in Salesforce?
Last Modified is the date and time stamp for changes (for example, changes to a phone number or an address) made to an individual record. This also shows the User who made the change. Last Activity shows when someone in your Organization last did something related to the record.
Related Topics | You May Also Like
Our Free Courses β
π Get Free Course β
π Salesforce Administrators π Salesforce Lightning Flow Builder π Salesforce Record Trigger Flow Builder |
π Get Free Course β
π Aura Lightning Framework π Lightning Web Component (LWC) π Rest APIs Integration |