+ Start a Discussion
vinni.focevinni.foce 

How to count the child field records?

Hi,

 

I have two objects Test1,Test2.(Lookup relation)

The two objects have amount fields..

I want to count the child object record filed into parent...

 

Please help me....

Best Answer chosen by Admin (Salesforce Developers) 
Yoganand GadekarYoganand Gadekar

for bulkifying please try following code. You will need to replace childrelationship name(in bold) with the actual childrelationship name. You can find it if you click your field RollUpParent__c in your child objects fields in set up (you can find child relationship there)

 

trigger RollUpTrigger on RollUpchild__c (after insert,after update)
{
   
  Set<id> RollChilSet  = new Set<id>();
  List<RollUpParent__c> updateList = New List<RollUpParent__c>();
  for(RollUpchild__c rollUpchild :trigger.new){
      RollChilSet.add(rollUpchild.RollUpParent__c) 
  }

   for(RollUpParent__c rolPar:[select id,TotalValue__c,(select id,Number__c,RollUpParent__c from  chilRelationshipName ) from RollUpParent__c where id    IN:RollChilSet]) {
  integer totalval = 0; 
 
    for(RollUpchild__c rol : rolPAr.chilRelationshipName ){
    totalval = totalval  + rol.Number__c;
   
     }  

    rolPa.TotalValue__c = totalval ;
    updateList.add(rolPar);

    }

   update updateList;

 }

 

Please mark this as answer if this helps.

_Yoganand Gadekar.

All Answers

kiranmutturukiranmutturu

for this you need to write the trigger to get the fields of the child ...for any efficent way to get this done check here

vinni.focevinni.foce

it's working fine....

but bulk inserts it's not working.....

Any idea....

please help me...

trigger RollUpTrigger on RollUpchild__c (after insert,after update)
{
    integer totalvalue = 0;
    for(RollUpchild__c rollUpchild:Trigger.new)
    {
        list<RollUpchild__c> rollUpCal = [select id,Number__c,RollUpParent__c from RollUpchild__c where RollUpParent__c=:rollUpchild.RollUpParent__c];
        for(integer i = 0;i<rollUpCal.size();i++)
        {
            totalvalue  = totalvalue + integer.valueof(rollUpCal[i].Number__c);
        }
       RollUpParent__c rollUpparentCal = [select id from RollUpParent__c where id=:rollUpchild.RollUpParent__c];
       rollUpparentCal.TotalValue__c = totalvalue;
       update rollUpparentCal;
    }
}

kiranmutturukiranmutturu

simply you know the reason..one thing that you need to work with is collections....so makye use of collections to prepare the data and avoid queris and dml statements inside for loop......

Mohith Kumar ShrivastavaMohith Kumar Shrivastava

http://blog.jeffdouglas.com/2011/08/23/salesforce-trigger-when-rollups-summaries-not-possible/

 

This link is good and jeff has nice example .You may like to refer the same 

Yoganand GadekarYoganand Gadekar

for bulkifying please try following code. You will need to replace childrelationship name(in bold) with the actual childrelationship name. You can find it if you click your field RollUpParent__c in your child objects fields in set up (you can find child relationship there)

 

trigger RollUpTrigger on RollUpchild__c (after insert,after update)
{
   
  Set<id> RollChilSet  = new Set<id>();
  List<RollUpParent__c> updateList = New List<RollUpParent__c>();
  for(RollUpchild__c rollUpchild :trigger.new){
      RollChilSet.add(rollUpchild.RollUpParent__c) 
  }

   for(RollUpParent__c rolPar:[select id,TotalValue__c,(select id,Number__c,RollUpParent__c from  chilRelationshipName ) from RollUpParent__c where id    IN:RollChilSet]) {
  integer totalval = 0; 
 
    for(RollUpchild__c rol : rolPAr.chilRelationshipName ){
    totalval = totalval  + rol.Number__c;
   
     }  

    rolPa.TotalValue__c = totalval ;
    updateList.add(rolPar);

    }

   update updateList;

 }

 

Please mark this as answer if this helps.

_Yoganand Gadekar.

This was selected as the best answer
vinni.focevinni.foce
Hi Yoganand... Thanks for your replay..... can you please explain this query... for(RollUpParent__c rolPar:[select id,TotalValue__c,(select id,Number__c,RollUpParent__c from CHILRELATIONSHIPNAME ) from RollUpParent__c where id IN:RollChilSet])
vinni.focevinni.foce

compiling error...

Please help me for that....

 

Error: Compile Error: Didn't understand relationship 'RollUpChild' in FROM part of query call. If you are attempting to use a custom relationship, be sure to append the '__r' after the custom relationship name. Please reference your WSDL or the describe call for the appropriate names. at line 9 column 30

 

 

trigger RollUpTrigger on RollUpChild__c (after insert,after update)
{
  Set<id> RollChilSet  = new Set<id>();
  List<RollUpParent__c> updateList = New List<RollUpParent__c>();
  for(RollUpChild__c rollUpchild :trigger.new)
  {
      RollChilSet.add(rollUpchild.RollUpParent__c);
  }
  for(RollUpParent__c rolPar:[select id,TotalValue__c,(select id,Number__c,RollUpParent__c from RollUpChild )from RollUpParent__c where id IN:RollUpChild])
  {
  }
}

vinni.focevinni.foce

compiling error...

Please help me for that....

 

 

  

Error: Compile Error: Didn't understand relationship 'RollUpChild' in FROM part of query call. If you are attempting to use a custom relationship, be sure to append the '__r' after the custom relationship name. Please reference your WSDL or the describe call for the appropriate names. at line 9 column 30 

 

trigger RollUpTrigger on RollUpChild__c (after insert,after update)
{
  Set<id> RollChilSet  = new Set<id>();
  List<RollUpParent__c> updateList = New List<RollUpParent__c>();
  for(RollUpChild__c rollUpchild :trigger.new)
  {
      RollChilSet.add(rollUpchild.RollUpParent__c);
  }
  for(RollUpParent__c rolPar:[select id,TotalValue__c,(select id,Number__c,RollUpParent__c from RollUpChild )from RollUpParent__c where id IN:RollChilSet])
  {
  }
}

Yoganand GadekarYoganand Gadekar

Hey you can try this modified trigger,

just make sure you have child relatuionship name as RollUpchilds.(check this in set field after you click your field, you can see chil relationship name) u only need to write __r.Tthis should work.

Query with child relationship name is a Relationship query wherein u are able to iterate for all the child records for each of the parent records. __r should be apended when relation is custom. 

 

trigger RollUpTrigger on RollUpchild__c (after insert,after update)
{
   
  Set<id> RollChilSet  = new Set<id>();
  List<RollUpParent__c> updateList = New List<RollUpParent__c>();
  for(RollUpchild__c rollUpchild :trigger.new){
      RollChilSet.add(rollUpchild.RollUpParent__c); 
  }

   for(RollUpParent__c rolPar:[select id,TotalValue__c,(select id,Number__c,RollUpParent__c from  RollUpchilds__r) from RollUpParent__c where id    IN:RollChilSet]) {
  decimal totalval = 0; 
 
    for(RollUpchild__c rol : rolPAr.RollUpchilds__r){
    totalval = totalval  + rol.Number__c;
   
     } 

    rolPar.TotalValue__c = totalval ;
    updateList.add(rolPar);

    }

   update updateList;

 }