Below apex class and apex trigger must be able to update child records every time its parent record is updated. Apex class holds the logic itself, while apex trigger calls the apex class. There is no error when I save it, but child records are not updated.
Apex Class:
public class UpdateDetailClass {
public static void UpdateDetailMethod(List<Master__c> masterList) {
Set<Id> masterIds = new Set<Id>();
for(Master__c master : masterList) {
masterIds.add(master.Id);
}
List<Detail__c> detailList = new List<Detail__c>([SELECT Id, Master__c, Master__r.isSelected__c, Type__c FROM Detail__c WHERE Master__c IN: masterIds]);
List<Detail__c> detailToUpdate = new List<Detail__c>();
for(Master__c master : masterList) {
for(Detail__c d : detailList) {
if(master.Id != null) {
if(master.isSelected__c == true) {
d.Master__c = master.Id;
d.Type__c = master.Type__c;
detailToUpdate.add(d);
}
}
}
}
update detailToUpdate;
}
}
Apex Trigger:
trigger updateTrigger on Master__c (after update) {
if(Trigger.isAfter && Trigger.isUpdate) {
UpdateDetailClass.UpdateDetailMethod(Trigger.New);
}
}
Master__c
record is last in the trigger collection will take all the detail records for itself...