I have create trigger and its handler class to count child records for particular parent.
Trigger countRecord on contact (after insert) { if(Trigger.isInsert){ contr.afterinsert(Trigger.new); }
}
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It only takes a minute to sign up.
Sign up to join this communityI have create trigger and its handler class to count child records for particular parent.
Trigger countRecord on contact (after insert) { if(Trigger.isInsert){ contr.afterinsert(Trigger.new); }
}
I think the issue is with your handler class: if there are no Subject Visits left when you delete then you need to query the parent Site record not the child records. One approach I've seen is to query the parent records with a subquery of the child records and setting the parent record to the size of the child list.
this is handler class
public class SubjectTriggerHandler {
public static void perform(Set<Id> Ids){
List<CTMS__Clinical_Site__c> CSToUpdate = new List<CTMS__Clinical_Site__c>([SELECT Id, Subject_Visit_Count__c, (SELECT Id FROM CTMS__Subject_Visits__r) FROM CTMS__Clinical_Site__c WHERE Id IN :Ids]);
for (CTMS__Clinical_Site__c site : CSToUpdate){
site.Subject_Visit_Count__c = site.CTMS__Subject_Visits__r.size();
}
update CSToUpdate;
}
}
ids
that won't be used by the handler