+ Start a Discussion
Dan_try_zeroDan_try_zero 

Trigger update (sum)

Hi

 

I have two objects A and B 

B has MasterDetail relationship with A

how can I  write a trigger to insert a record In object B on update of field in a record of  object A, i need to make the sum of some fields in each object created in A

 

 

 

Thanks in advance

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

Just a hint. Hope this helps.

Try following code : This will function like roll up summary. Code demonstrates for one field.

 

trigger Update_Masterfield on DetailObject__c (after update,After insert){

 

   set<ID> MasterObjectIdSet = New Set<ID>();

   for(DetailObject__c detailList: trigger.new){

        MasterObjectIdSet.add(detailList.MasterFieldInDetail__c);

   }

   Map<id,integer> sumMAp = New Map<id,integer>();

   for(MasterObject__c mastList : [select id masterField__c,(select id detailField__c from childRelationshipname)from  masterObject__c where id In: MasterObjectIdSet]){

Integer sum=0;

  

   for(DetailObject__c detList:childrelationshipname){

         sum = sum + detlist.DetailField__c;

    }

    sumMAp.put(masList.id,sum);

}

Set<masterObject__c> UpdateMasterSet =New Set<masterObject__c>();

for(masterObject__c maList:[select id,masterField__c from masterObject__c where id In:sumMAp.keySet()]){

     maList.masterfield__c = SumMap.get(maList.id);

    UpdateMasterSet.add(maList);

}

update UpdateMasterSet;

}

All Answers

Yoganand GadekarYoganand Gadekar

if you are looking for updating detail objects field into master object then u have an option of using roll up summary field.

U need to create a field with data type roll up summary in master object wherein u will need to specify which field from detail should be summed up/averaged etc.

mark this as an answer if this works for you.

 

 

Dan_try_zeroDan_try_zero

i know, but roll-up is limitated at 10 for objext, i need to do 20+ roll up in that case, for update all this fields, any suggestion for this question?

Yoganand GadekarYoganand Gadekar

Just a hint. Hope this helps.

Try following code : This will function like roll up summary. Code demonstrates for one field.

 

trigger Update_Masterfield on DetailObject__c (after update,After insert){

 

   set<ID> MasterObjectIdSet = New Set<ID>();

   for(DetailObject__c detailList: trigger.new){

        MasterObjectIdSet.add(detailList.MasterFieldInDetail__c);

   }

   Map<id,integer> sumMAp = New Map<id,integer>();

   for(MasterObject__c mastList : [select id masterField__c,(select id detailField__c from childRelationshipname)from  masterObject__c where id In: MasterObjectIdSet]){

Integer sum=0;

  

   for(DetailObject__c detList:childrelationshipname){

         sum = sum + detlist.DetailField__c;

    }

    sumMAp.put(masList.id,sum);

}

Set<masterObject__c> UpdateMasterSet =New Set<masterObject__c>();

for(masterObject__c maList:[select id,masterField__c from masterObject__c where id In:sumMAp.keySet()]){

     maList.masterfield__c = SumMap.get(maList.id);

    UpdateMasterSet.add(maList);

}

update UpdateMasterSet;

}

This was selected as the best answer
Dan_try_zeroDan_try_zero

thanks i'll try it ;)