Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
ShowAll Questionssorted byDate Posted
lohith m

# rollup trigger

Hi All I have a scenario here plese help me i am learner in salesforce please help me with this topic .... thanks in advance......

Create a Custom Formula Field Age in Contact (value needs to be calculated in Years Based on Date of Birth)
Then in account create 4 fields  Below 18, 18-30,31-59,60+ (Number Type)
Then create a Trigger and rollup the counts accordingly from Contact to Account.
For example
if there are 20 people under 18 then In Account Below 18 should show 20.
if there are 50 people under 18 -30 then In Account 18-30 should show 50…..
Best Answer chosen by lohith m
Jason Beal
You can create a formula field named age on the Contact object. Here is a formula to calculate age.
`IF(ISNULL(Birthdate), null, FLOOR((TODAY() - Birthdate + 1) / 365.2425))`
Create the following trigger on the Contact object. Modify the age groups to give you exactly what you need. You'll need to add the custom fields for the age groups to Contact.
```trigger ContactAge on Contact (after insert, after undelete, after update, after delete) {
Contact[] contacts;
if (Trigger.isDelete)
contacts = Trigger.old;
else
contacts = Trigger.new;

ID[] accountIds = new ID[]{};
for (Contact contact : contacts)

Contact[] accountContacts = [select Id from Contact where AccountId in :accountIds];
Account[] accounts = [select Id, AgeGroup1__c,AgeGroup2__c from Account where Id in :accountIds];

for (Account account : accounts) {
Integer ageGroup1 = 0;
Integer ageGroup2 = 0;
for (Contact contact : accountContacts) {
if (contact.AccountId == account.Id){
if (contact.age < 18)
ageGroup1++;
if (contact.age >= 18 & contact.age < 30)
ageGroup2++;
}
}
account.AgeGroup1__c = ageGroup1;
account.AgeGroup2__c = ageGroup2;
}

update accounts;
}```

James Loghry
Since it sounds like you're fairly new to Triggers, have you checked developer.salesforce.com/trailhead for a primer on Apex Triggers?

I would start there, as this is a pretty straight forward trigger to write.

However, if you're looking for a easier approach, you might want to look at installing the "Declaritive Rollup Summary Tool" which can create rollup summary functionality even for lookup relationships. It also supports filtering criteria for your Contact DoB criteria.  For more on the tool see here: http://andyinthecloud.com/2014/02/09/new-release-spring14-declarative-rollup-summary-tool/
Jason Beal
You can create a formula field named age on the Contact object. Here is a formula to calculate age.
`IF(ISNULL(Birthdate), null, FLOOR((TODAY() - Birthdate + 1) / 365.2425))`
Create the following trigger on the Contact object. Modify the age groups to give you exactly what you need. You'll need to add the custom fields for the age groups to Contact.
```trigger ContactAge on Contact (after insert, after undelete, after update, after delete) {
Contact[] contacts;
if (Trigger.isDelete)
contacts = Trigger.old;
else
contacts = Trigger.new;

ID[] accountIds = new ID[]{};
for (Contact contact : contacts)

Contact[] accountContacts = [select Id from Contact where AccountId in :accountIds];
Account[] accounts = [select Id, AgeGroup1__c,AgeGroup2__c from Account where Id in :accountIds];

for (Account account : accounts) {
Integer ageGroup1 = 0;
Integer ageGroup2 = 0;
for (Contact contact : accountContacts) {
if (contact.AccountId == account.Id){
if (contact.age < 18)
ageGroup1++;
if (contact.age >= 18 & contact.age < 30)
ageGroup2++;
}
}
account.AgeGroup1__c = ageGroup1;
account.AgeGroup2__c = ageGroup2;
}

update accounts;
}```

This was selected as the best answer
lohith m
Thank  you Jason Beal