You need to sign in to do that
Don't have an account?
Olga Kim 5
A field on a parent Object that shows AVERAGE amount from child object records?
I have a requirement to create a custom field called Average_Utility__c on a parent object - Property__c that will show average sum of the child object records - Utility__c.
I tried to create a trigger and a class that uses aggregate AVG function to fetch the records of amount_paid field from Utility__c.
But my problem is I don't know how to make it to recalculate Average_Utility__c amount every time when new utility record is created.
This is my code:
public class AvgUtility {
public static void avgUtility (list<property__c> propList)
{
// list <property__c> propList=new List<property__c>();
list<property__c> PropertyList=[select id,name, Average_Utility__c from property__c];
for(property__c prop :PropertyList)
{
if (prop.Average_Utility__c==null || prop.Average_Utility__c!=null)
{
AggregateResult[] groupedResults = [SELECT Property__r.ID, AVG(Amount_Paid__c) FROM Utility__c GROUP BY Property__r.ID];
for (AggregateResult ar : groupedResults){
prop.id =(id)ar.get ('Property__c');
prop.Average_Utility__c=(decimal)ar.get(('expr0'));
propList.add(prop);
}
}
}
insert propList;
}
}
Trigger Class
trigger AvgUtilityTrigger on Utility__c (after insert) {
for (Utility__c ut:trigger.new)
{
AvgUtility.avgUtility();
}
}
I will appreciate any advice!
I tried to create a trigger and a class that uses aggregate AVG function to fetch the records of amount_paid field from Utility__c.
But my problem is I don't know how to make it to recalculate Average_Utility__c amount every time when new utility record is created.
This is my code:
public class AvgUtility {
public static void avgUtility (list<property__c> propList)
{
// list <property__c> propList=new List<property__c>();
list<property__c> PropertyList=[select id,name, Average_Utility__c from property__c];
for(property__c prop :PropertyList)
{
if (prop.Average_Utility__c==null || prop.Average_Utility__c!=null)
{
AggregateResult[] groupedResults = [SELECT Property__r.ID, AVG(Amount_Paid__c) FROM Utility__c GROUP BY Property__r.ID];
for (AggregateResult ar : groupedResults){
prop.id =(id)ar.get ('Property__c');
prop.Average_Utility__c=(decimal)ar.get(('expr0'));
propList.add(prop);
}
}
}
insert propList;
}
}
Trigger Class
trigger AvgUtilityTrigger on Utility__c (after insert) {
for (Utility__c ut:trigger.new)
{
AvgUtility.avgUtility();
}
}
I will appreciate any advice!
Replace trigger code with below code:
Also made some changes in class code as :
Try these codes and let me know the results.
Thanks and Regards
Ayush
All Answers
Please try the below code for your class, And also run the Trigger on After delete and After update.
Please try the above code and let me know the results. If it solves your problem please close this thread with the best solution.
Thanks and Regards
Ayush Sharma
Could you, please, help me to write the code for after update and after insert trigger?
I just started to learn coding in Salesforce. I am not sure how the code should look like.
I really appreciate your help. Thanks again
Replace trigger code with below code:
Also made some changes in class code as :
Try these codes and let me know the results.
Thanks and Regards
Ayush