You need to sign in to do that
Don't have an account?
Lakshmi S
Calculate average value on lookup relation ship?
Hi Dev's,
I have a two objects Account and Contact
In account there is a custom field - contactscore__c
In contact there is a custom field - score__c
So, i want to calculate average value of all related contacts 'score__c' and store the average value on account - 'contactscore__c' field;
How can we achieve this scenario using code?
Regards
Lakshmi.
Thanks in advance...
I have a two objects Account and Contact
In account there is a custom field - contactscore__c
In contact there is a custom field - score__c
So, i want to calculate average value of all related contacts 'score__c' and store the average value on account - 'contactscore__c' field;
How can we achieve this scenario using code?
Regards
Lakshmi.
Thanks in advance...
trigger AverageTrigger on Contact (after insert,after update,after delete,after undelete) {
Set<Id> accids = new Set<Id>();
Decimal contactscore;
Decimal noofcontacts;
if(trigger.isInsert || trigger.isUpDate || trigger.isUnDelete){
for(Contact con : trigger.new){
if(con.AccountId != null)
accids.add(con.AccountId);
}
}
if(trigger.isUpdate || trigger.isDelete){
for(Contact oldcon: trigger.old){
if(oldcon.AccountId != null)
accids.add(oldcon.AccountId);
}
}
if(accIds.size()>0){
List<Account> lst = [Select id,contactscore__c,no_of_Contacts__c,(Select id,score__c from Contacts) from Account where Id in : accIds];
list<account> updateacc=new list<account>();
list<account> updateacc2=new list<account>();
for(Account acc : lst){
List<AggregateResult> ar = [select avg(score__c)sc from contact where accountid = :acc.Id];
for(AggregateResult a : ar){
contactscore = (Decimal)a.get('sc');
}
acc.contactscore__c = contactscore;
acc.no_of_Contacts__c = acc.contacts.size();
updateacc.add(acc);
}
update updateacc;
}
}
All Answers
Here the code for Your scenario
trigger CountContactsnew on Contact (After Update,After Insert ) {
set<id> parentid = new set<id>();
decimal all_subtotal;
decimal all_subtotal1;
for(Contact child:trigger.new)
{
if(child.Age__c!= NULL){
parentid.add(child.AccountId);
}
}
list<Account> parent = [select Id,Contacts_sum__c,Contact_Rollup__c from Account WHERE Id =:parentid];
List<AggregateResult> pre_child_total = [select avg(Age__c),count(AccountId) from Contact where AccountId =:parentid];
for (AggregateResult ar:pre_child_total)
{
all_subtotal = (Decimal)ar.get('expr0');
all_subtotal1 = (Decimal)ar.get('expr1');
}
list<Account> parentupdate = new list<Account>();
for(Account pa:parent){
pa.Contacts_sum__c = all_subtotal;
pa.Contact_Rollup__c = all_subtotal1;
parentupdate.add(pa);
}
update parentupdate;
}
Thanks,
Naveen
Try with following code
Please let me know if this works for you.
Thanks,
Sukanya
Thanks for your reply.
your code not meets the criteria.
Thanks for your reply.
your code not satisfy my criteria.
trigger AverageTrigger on Contact (after insert,after update,after delete,after undelete) {
Set<Id> accids = new Set<Id>();
Decimal contactscore;
Decimal noofcontacts;
if(trigger.isInsert || trigger.isUpDate || trigger.isUnDelete){
for(Contact con : trigger.new){
if(con.AccountId != null)
accids.add(con.AccountId);
}
}
if(trigger.isUpdate || trigger.isDelete){
for(Contact oldcon: trigger.old){
if(oldcon.AccountId != null)
accids.add(oldcon.AccountId);
}
}
if(accIds.size()>0){
List<Account> lst = [Select id,contactscore__c,no_of_Contacts__c,(Select id,score__c from Contacts) from Account where Id in : accIds];
list<account> updateacc=new list<account>();
list<account> updateacc2=new list<account>();
for(Account acc : lst){
List<AggregateResult> ar = [select avg(score__c)sc from contact where accountid = :acc.Id];
for(AggregateResult a : ar){
contactscore = (Decimal)a.get('sc');
}
acc.contactscore__c = contactscore;
acc.no_of_Contacts__c = acc.contacts.size();
updateacc.add(acc);
}
update updateacc;
}
}