You need to sign in to do that
Don't have an account?
Vinod Kumar 92
Child has Lookup relationship to parent, Count the number of child records on the each parent object
Count the number of child records on the each parent object, child has Lookup relationship to parent.
Take care that the trigger should follow one trigger per object architecture, should be bulkified and should have 90+% test code coverage.
Here Obj1: Parent__c
Child_Count__c
Obj: Child__c
Parent__c -Lookup to parent
Note:.1 Implement below apex class, it has to work in all cases(insert,update,delete,undelete) and call the methods with the help of below trigger.
2. This code working gud except update functionality. So can any body check out and give me right code for update functionality ASAP. Thanks in advance.
Apex Class:
public class ChildCountHelper{
//List<Parent__c> parentList = [select id, child_count__c, (select id from child__r) from Parent__c where id in :parentIDSet];
public List<ID> conList= new List<ID>();
public static void handleBeforeInsert(List<Child__c> childList){
Set<ID> parentIDSet = new Set<ID>();
for(Child__c childRec: childList){
parentIDSet.add(childRec.Parent__c);
}
Map<ID, Parent__c> parentMap = new map<ID, parent__c>([select id, child_count__c from Parent__c where id in :parentIDSet]);
for(Child__c childRec: childList){
if(parentMap.get(childRec.Parent__c).child_count__c == null){
parentMap.get(childRec.Parent__c).child_count__c = 0;
}
parentMap.get(childRec.Parent__c).child_count__c ++;
}
update parentMap.values();
}
public static void handleBeforeUpdate(List<Child__c> newChildList, List<Child__c> oldChildList){
Set<ID> parentIDSet = new Set<ID>();
Map<ID, ID> oldChildMap = new Map<ID, ID>();
for(Child__c childRec: newChildList){
parentIDSet.add(childRec.Parent__c);
}
for(Child__c childRec: oldChildList){
parentIDSet.add(childRec.Parent__c);
oldChildMap.put(childRec.Id, childRec.Parent__c);
}
Map<ID, Parent__c> parentMap = new map<ID, parent__c>([select id, child_count__c from Parent__c where id in :parentIDSet]);
for(Child__c childRec: newChildList)
{
/*if(ChildRec.Parent__c!=null){ */
if(parentMap.get(childRec.Parent__c).child_count__c == null){
parentMap.get(childRec.Parent__c).child_count__c = 0;
}
// }
if(childRec.Parent__c != oldChildMap.get(childRec.id)){
if(oldChildMap.get(childRec.id) == null && childRec.Parent__c != null){
parentMap.get(childRec.Parent__c).child_count__c ++;
}else if(oldChildMap.get(childRec.id) != null && childRec.Parent__c == null){
parentMap.get(oldChildMap.get(childRec.id)).child_count__c --;
}else if(oldChildMap.get(childRec.id) != null && childRec.Parent__c != null){
parentMap.get(oldChildMap.get(childRec.id)).child_count__c --;
parentMap.get(childRec.Parent__c).child_count__c ++;
}
}
}
update parentMap.values();
}
public static void handleBeforeDelete(List<Child__c> childList){
Set<ID> parentIDSet = new Set<ID>();
for(Child__c childRec: childList){
parentIDSet.add(childRec.Parent__c);
}
Map<ID, Parent__c> parentMap = new map<ID, parent__c>([select id, child_count__c from Parent__c where id in :parentIDSet]);
for(Child__c childRec: childList){
if(parentMap.get(childRec.Parent__c).child_count__c == null){
parentMap.get(childRec.Parent__c).child_count__c = 0;
}
parentMap.get(childRec.Parent__c).child_count__c --;
}
update parentMap.values();
}
public static void handleBeforeUnDelete(List<Child__c> childList){
Set<ID> parentIDSet = new Set<ID>();
for(Child__c childRec: childList){
parentIDSet.add(childRec.Parent__c);
}
Map<ID, Parent__c> parentMap = new map<ID, parent__c>([select id, child_count__c from Parent__c where id in :parentIDSet]);
for(Child__c childRec: childList){
if(parentMap.get(childRec.Parent__c).child_count__c == null){
parentMap.get(childRec.Parent__c).child_count__c = 0;
}
parentMap.get(childRec.Parent__c).child_count__c ++;
}
update parentMap.values();
}
}
*****************************
//Trigger 1:
trigger ChildTrigger on Child__c (before insert, after update, after delete, after undelete) {
if (Trigger.isInsert) {
ChildCountHelper.handleBeforeInsert((List<Child__c>)Trigger.NEW);
}else if (Trigger.isUpdate) {
ChildCountHelper.handleBeforeUpdate((List<Child__c>)Trigger.NEW, (List<Child__c>)Trigger.OLD);
}else if (Trigger.isDelete) {
ChildCountHelper.handleBeforeDelete((List<Child__c>)Trigger.OLD);
}else if (Trigger.isUndelete) {
ChildCountHelper.handleBeforeUnDelete((List<Child__c>)Trigger.NEW);
}
Take care that the trigger should follow one trigger per object architecture, should be bulkified and should have 90+% test code coverage.
Here Obj1: Parent__c
Child_Count__c
Obj: Child__c
Parent__c -Lookup to parent
Note:.1 Implement below apex class, it has to work in all cases(insert,update,delete,undelete) and call the methods with the help of below trigger.
2. This code working gud except update functionality. So can any body check out and give me right code for update functionality ASAP. Thanks in advance.
Apex Class:
public class ChildCountHelper{
//List<Parent__c> parentList = [select id, child_count__c, (select id from child__r) from Parent__c where id in :parentIDSet];
public List<ID> conList= new List<ID>();
public static void handleBeforeInsert(List<Child__c> childList){
Set<ID> parentIDSet = new Set<ID>();
for(Child__c childRec: childList){
parentIDSet.add(childRec.Parent__c);
}
Map<ID, Parent__c> parentMap = new map<ID, parent__c>([select id, child_count__c from Parent__c where id in :parentIDSet]);
for(Child__c childRec: childList){
if(parentMap.get(childRec.Parent__c).child_count__c == null){
parentMap.get(childRec.Parent__c).child_count__c = 0;
}
parentMap.get(childRec.Parent__c).child_count__c ++;
}
update parentMap.values();
}
public static void handleBeforeUpdate(List<Child__c> newChildList, List<Child__c> oldChildList){
Set<ID> parentIDSet = new Set<ID>();
Map<ID, ID> oldChildMap = new Map<ID, ID>();
for(Child__c childRec: newChildList){
parentIDSet.add(childRec.Parent__c);
}
for(Child__c childRec: oldChildList){
parentIDSet.add(childRec.Parent__c);
oldChildMap.put(childRec.Id, childRec.Parent__c);
}
Map<ID, Parent__c> parentMap = new map<ID, parent__c>([select id, child_count__c from Parent__c where id in :parentIDSet]);
for(Child__c childRec: newChildList)
{
/*if(ChildRec.Parent__c!=null){ */
if(parentMap.get(childRec.Parent__c).child_count__c == null){
parentMap.get(childRec.Parent__c).child_count__c = 0;
}
// }
if(childRec.Parent__c != oldChildMap.get(childRec.id)){
if(oldChildMap.get(childRec.id) == null && childRec.Parent__c != null){
parentMap.get(childRec.Parent__c).child_count__c ++;
}else if(oldChildMap.get(childRec.id) != null && childRec.Parent__c == null){
parentMap.get(oldChildMap.get(childRec.id)).child_count__c --;
}else if(oldChildMap.get(childRec.id) != null && childRec.Parent__c != null){
parentMap.get(oldChildMap.get(childRec.id)).child_count__c --;
parentMap.get(childRec.Parent__c).child_count__c ++;
}
}
}
update parentMap.values();
}
public static void handleBeforeDelete(List<Child__c> childList){
Set<ID> parentIDSet = new Set<ID>();
for(Child__c childRec: childList){
parentIDSet.add(childRec.Parent__c);
}
Map<ID, Parent__c> parentMap = new map<ID, parent__c>([select id, child_count__c from Parent__c where id in :parentIDSet]);
for(Child__c childRec: childList){
if(parentMap.get(childRec.Parent__c).child_count__c == null){
parentMap.get(childRec.Parent__c).child_count__c = 0;
}
parentMap.get(childRec.Parent__c).child_count__c --;
}
update parentMap.values();
}
public static void handleBeforeUnDelete(List<Child__c> childList){
Set<ID> parentIDSet = new Set<ID>();
for(Child__c childRec: childList){
parentIDSet.add(childRec.Parent__c);
}
Map<ID, Parent__c> parentMap = new map<ID, parent__c>([select id, child_count__c from Parent__c where id in :parentIDSet]);
for(Child__c childRec: childList){
if(parentMap.get(childRec.Parent__c).child_count__c == null){
parentMap.get(childRec.Parent__c).child_count__c = 0;
}
parentMap.get(childRec.Parent__c).child_count__c ++;
}
update parentMap.values();
}
}
*****************************
//Trigger 1:
trigger ChildTrigger on Child__c (before insert, after update, after delete, after undelete) {
if (Trigger.isInsert) {
ChildCountHelper.handleBeforeInsert((List<Child__c>)Trigger.NEW);
}else if (Trigger.isUpdate) {
ChildCountHelper.handleBeforeUpdate((List<Child__c>)Trigger.NEW, (List<Child__c>)Trigger.OLD);
}else if (Trigger.isDelete) {
ChildCountHelper.handleBeforeDelete((List<Child__c>)Trigger.OLD);
}else if (Trigger.isUndelete) {
ChildCountHelper.handleBeforeUnDelete((List<Child__c>)Trigger.NEW);
}
Try with below code ,
Let me know if it helps !!
Thanks
Mnaoj
Thanks for giving reply. I'm also written a trigger, but here problem is i need to work on with above apex class. So please check out above code in that update functionality was not working. If u do the help means it will be appreciated. Thanks
If you need handler class please try with belwo code .
Trigger code Handlerclass Let me know if it helps !!
Thanks
Manoj
Thanks for giving reply. i tried with above code, update functionality is not working. So if u have any changes in above code, pls check out and give me reply.. it would be great and appreciated u. Thank you
I don't think record count wil increase or decrease on update .
Stil if you need please modify the trigger code like belwo .
Thanks
Manoj
Thanks for spending ur valuable time and giving response. i used above updated code, then to count is not updating. I think we have to do modfications in apex class only. So if possible do it, it wil be great. Thank you.