function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
sinsin 

Need to Buliky the Trigger

Hi, I am new to SFDC. I am having a trigger which is updating the fields in a Parent__c object after any  update to the Child__c object .There is no Master Detail Relationship just reffering Parent- Child  for ease to understand. 

These objects having a Lookup relation Lookup_Name__c.

 

These fields count the number of Child__c Object for different Status__c.

 

I want this trigger to fire only when there is a change n the status of the Child__c.Status__c object  or else do not take any action. Hence need to compare the old and new values.

 

Here is the code. Need help. Thanks in Advance.

=================================================

 

trigger CountTrigger on Child_Obj__c (before update) {

Set<Id> parentIds = new Set<Id>();

List<Parent_Obj__c> parents = new list<Parent_Obj__c>();

if (Trigger.isDelete){

 for(Child_Obj__c child:Trigger.oldMap.values()){

    parentIds.add(child.Lookup_Name__c);

   }

}

else{

for(Child_Obj__c child:Trigger.new){

   parentIds.add(child.Lookup_Name__c);

   }

}

List<Id> listParents = new List<Id>();

for (Id parentId : parentIds) {

  listParents.add(parentId);

  }

  Parent_Obj__c parent=

   [SELECT Id, Total_Count__c, A_Count__c FROM Parent_Obj__c WHERE Parent_Obj__c.Id in: listParents limit 1];

 parent.Total_Count__c =

    [SELECT COUNT() FROM Child_Obj__c WHERE Lookup_Name__c in: listParents ];

  parent. A_Count__c=

    [SELECT COUNT() FROM Child_Obj__c WHERE Lookup_Name__c in: listParents AND Status__c='A'];

  parent.B_Count__c =

    [SELECT COUNT() FROM Child_Obj__c WHERE Lookup_Name__c in: listParents AND Status__c='B'];

 parent.Total_Count__c = parent. A_Count__c + parent. B_Count__c;

parents.add(parent);

 update parents;

}

===========================================================

 

Best Answer chosen by Admin (Salesforce Developers) 
gbu.varungbu.varun

Hi, 

Just add trigger.oldmap.get(acc.id).Status<> trigger.newmap.get(acc.id).Status) to compare it. 

 

trigger CountTrigger on Child_Obj__c (before update) {
	Set<Id> parentIds = new Set<Id>();
	List<Parent_Obj__c> parents = new list<Parent_Obj__c>();
	if (Trigger.isDelete){
	 for(Child_Obj__c child:Trigger.oldMap.values()){
		parentIds.add(child.Lookup_Name__c);
	   }
	}
	else{
		for(Child_Obj__c child:Trigger.new){
		   if(trigger.oldmap.get(Child_Obj__c.id).FieldName <> trigger.newmap.get(acc.id).FieldName)
			parentIds.add(child.Lookup_Name__c);
		}
	}

	Parent_Obj__c p1= [SELECT Id, Total_Count__c, A_Count__c,B_Count__c, FROM Parent_Obj__c WHERE Parent_Obj__c.Id in: parentIds limit 1];

	Integer acount = [SELECT COUNT() FROM Child_Obj__c WHERE Lookup_Name__c in: parentIds AND Status__c='A'];
	Integer bcount = [SELECT COUNT() FROM Child_Obj__c WHERE Lookup_Name__c in: listParents AND Status__c='B']; 

	List<Parent_Obj__c> par = new List<Parent_Obj__c>();

	for(Parent_Obj__c parent:p1){
		parent.Total_Count__c = p1.size();
		parent.A_Count__c= acount;
		parent.B_Count__c = bcount;
		parent.Total_Count__c = parent. A_Count__c + parent. B_Count__c;
		par.add(parent);
	}
	update par;
}

 

 

Hit soultion accpted if it works.

 

All Answers

gbu.varungbu.varun

Hi I have minimized and bulkified your trigger. If you face any problem let me know. 

trigger CountTrigger on Child_Obj__c (before update) {
Set<Id> parentIds = new Set<Id>();
List<Parent_Obj__c> parents = new list<Parent_Obj__c>();
if (Trigger.isDelete){
 for(Child_Obj__c child:Trigger.oldMap.values()){
    parentIds.add(child.Lookup_Name__c);
   }
}
else{
for(Child_Obj__c child:Trigger.new){
   parentIds.add(child.Lookup_Name__c);
   }
}

Parent_Obj__c p1= [SELECT Id, Total_Count__c, A_Count__c,B_Count__c, FROM Parent_Obj__c WHERE Parent_Obj__c.Id in: parentIds limit 1];

Integer acount = [SELECT COUNT() FROM Child_Obj__c WHERE Lookup_Name__c in: parentIds AND Status__c='A'];
Integer bcount = [SELECT COUNT() FROM Child_Obj__c WHERE Lookup_Name__c in: listParents AND Status__c='B']; 

List<Parent_Obj__c> par = new List<Parent_Obj__c>();

for(Parent_Obj__c parent:p1){
	parent.Total_Count__c = p1.size();
	parent.A_Count__c= acount;
	parent.B_Count__c = bcount;
	parent.Total_Count__c = parent. A_Count__c + parent. B_Count__c;
	par.add(parent);
}
update par;
}

 

sinsin

Thanks Varun.

 

I need to apply some conditions in the trigger. Like I want this trigger to fire only when there is Status change of the Child__c object.

Like once the status of the Trigger get changed from A to B or B to A then only it get fire.

 

I am thinking to get this done using trigger.new and trigger.old but while applying so I think the SOQL statement will come inside the FOR loop.

 

It would be really helpful if you can help. Thanks !! 

gbu.varungbu.varun

Hi, 

Just add trigger.oldmap.get(acc.id).Status<> trigger.newmap.get(acc.id).Status) to compare it. 

 

trigger CountTrigger on Child_Obj__c (before update) {
	Set<Id> parentIds = new Set<Id>();
	List<Parent_Obj__c> parents = new list<Parent_Obj__c>();
	if (Trigger.isDelete){
	 for(Child_Obj__c child:Trigger.oldMap.values()){
		parentIds.add(child.Lookup_Name__c);
	   }
	}
	else{
		for(Child_Obj__c child:Trigger.new){
		   if(trigger.oldmap.get(Child_Obj__c.id).FieldName <> trigger.newmap.get(acc.id).FieldName)
			parentIds.add(child.Lookup_Name__c);
		}
	}

	Parent_Obj__c p1= [SELECT Id, Total_Count__c, A_Count__c,B_Count__c, FROM Parent_Obj__c WHERE Parent_Obj__c.Id in: parentIds limit 1];

	Integer acount = [SELECT COUNT() FROM Child_Obj__c WHERE Lookup_Name__c in: parentIds AND Status__c='A'];
	Integer bcount = [SELECT COUNT() FROM Child_Obj__c WHERE Lookup_Name__c in: listParents AND Status__c='B']; 

	List<Parent_Obj__c> par = new List<Parent_Obj__c>();

	for(Parent_Obj__c parent:p1){
		parent.Total_Count__c = p1.size();
		parent.A_Count__c= acount;
		parent.B_Count__c = bcount;
		parent.Total_Count__c = parent. A_Count__c + parent. B_Count__c;
		par.add(parent);
	}
	update par;
}

 

 

Hit soultion accpted if it works.

 

This was selected as the best answer
sinsin

Hi,

 

Thanks alot for the help.

Just want to have ne more concern here. I had made this trigger to  fire for any change in Parent_Obj__c. Like both the chaild and parent are related. So in the child_obj__c if I edit the parent_obj__c which is lookup relationship in child_obj__c. I want that for the new Parent_obj__c the child will be get added and reflect into the Parent's page Count field and for the old Parent_obj__c the count field should be reflected accordingly (subtracted by 1).

 

But when I am doing that I am able to do either one like either I am able to show the correct count either for the new Parent_obj__c or for the old Parent_obj__c.

 here is the snippet I am adding to get the new parent_obj__c updated.

 

==============

 

if (Trigger.isUpdate){

for (Child_Obj__c child:Trigger.new){
        if(trigger.oldMap.get(child.Id).Parent_Name__c <> trigger.newMap.get(child.Id).Parent_Name__c){
            
            parentIds.add(child.Parent_Name__c);
        }
    }

=============================================

Parent_Name__c is the lookup relation name in the Child_obj__c for Parent_Obj__c.

 

When I am doing that I am just getting the new Parent_Obj__c updated with the correct Count field.

I also tried traversing the Trigger.old. in the same block like

 

 

if (Trigger.isUpdate){

for (Child_Obj__c child:Trigger.new){
        if(trigger.oldMap.get(child.Id).Parent_Name__c <> trigger.newMap.get(child.Id).Parent_Name__c){
            
            parentIds.add(child.Parent_Name__c);
        }
    }

 for (Child_Obj__c child:Trigger.old){
        if(trigger.oldMap.get(child.Id).Parent_Name__c <> trigger.newMap.get(child.Id).Parent_Name__c)
        parentIds.add(child.Parent_Name__c);
    }

}

 

but this time I am getting the total old Chilld Count for both the parents.

 

Please help.