function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Melissa CareyMelissa Carey 

Need help writing a trigger!

We have a field on the Account level called "Monthly Fee".  I'd like to create a new field on the parent level so we can see a roll-up summary of the children accounts.  Please help me with writing the trigger! 

Cory CowgillCory Cowgill

If this is a Rollup-Summary Field and the relationship is Master-Detail, than you don't need to do a Trigger to do simple Roll Ups.

 

If the relationship is Master-Detail, you can handle this when you create the Field.

 

Create the field on Account as a type Rollup-Summary and tell it what you want to child field you want to summarize.

 

 

Melissa CareyMelissa Carey

The relationship is not Master-Detail.  I was told I need to use a trigger.

GobbledigookGobbledigook

I'm not completely 100% sure of what you're looking for, but if you have a parent-child relationship, you can use __r.  So let's say you have a Parent__c and a Child__c custom object.  On the parent you want to fill the Report__c field, and you want to call the Summary__c field from the child. 

 

So you have something like: Parent__c.Report__c = Parent__c.Child__r.Summary__c 

 

So, crudely written: 

 

trigger DoThis on Parent__c(before update)

{

for(Parent__c p: trigger.new)

{

   p.Report__c = p.Child__r.Summary__c;

}

}

 

It might not be exact but it's a start.