Q:

Sum of field in related object

I have an eshot object which is related to Lead.
For each lead myLead
I would like to sum into myLead.clicks
the integer eshot.numclicks__c
where eshot.lead__c matches the current lead.
My SOQL is weak. How could I do this?

A:

You can get your triggers and backfill batch all without writing any code yourself if you install declarative-lookup-rollup-summaries. Just create a Lookup Rollup Summary as follows:

Parent Object: Lead
Child Object: EShot__c
Relationship Field: Lead__c
Field to Aggregate: NumClicks__c
Aggregate Operation: Sum
Aggregate Result Field: Clicks__c

