• sin
  • NEWBIE
  • 0 Points
  • Member since 2013

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 2
    Replies

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;

}

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

 

  • September 26, 2013
  • Like
  • 0

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;

}

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

 

  • September 26, 2013
  • Like
  • 0