+ Start a Discussion
GRStevenBrookesGRStevenBrookes 

Help with Trigger - Simple Roll-Up

Evening All,

 

Having a few problems....I effectivly have 2 objects, one called CallScripter_Data__c and another Service_Data__c - the latter being a child of the first via a MDR.

 

Effectivly, records are added to the Service Data object and I want some simple calculations to then appear on the related CallScripter object.

 

Here is my code - which works, but only posts the values for the last record that is ended, rather than summing up all of the records that fit the criteria - like a Roll-Up-Summary - can anyone help with this?

 

trigger CS_Data_Jan12 on Service_Data__c (before insert) {

     Set<Id> esIds=new Set<Id>();
     for (Service_Data__c es : trigger.new)
     {
        esIds.add(es.CallScripter_Data__c);
     }

     List<CallScripter_Data__c> sis=[select id, January12_Total_Calls__c,January12_Total_Minutes__c, (select id, Calls__c,Minutes__c from Service_Data__r) from CallScripter_Data__c where id in :esIds];
    
     for(CallScripter_Data__c si : sis) {
      if(si.Service_Data__r.size()>0) {	  
		  si.January12_Total_Calls__c = si.Service_Data__r[0].Calls__c;
		  si.January12_Total_Minutes__c = si.Service_Data__r[0].Minutes__c;
		  	 }	
  }
  update sis;
}

 

Navatar_DbSupNavatar_DbSup

Hi,


You can do this through roll-up summery as you have written that in this case you have Master Detail Relations between the object. So try to create a Rolllup Summery Field which will calculate the sum of all Service_Data__c record at the parent object CallScripter_Data__c . No need of writing a trigger for this. Please make these fields January12_Total_Calls__c, January12_Total_Minutes__c as Rollupsummary field.

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved.