You need to sign in to do that
Don't have an account?

Trigger to fetch records in one quarter
Hi,
Please help me with this requirement
I have 'score' field on objectA and this object has lookup relationship to account.
I want to take average of 'score' field on all objectA records created in current quarter and stamp that value on account.
Thanks in advance
Please help me with this requirement
I have 'score' field on objectA and this object has lookup relationship to account.
I want to take average of 'score' field on all objectA records created in current quarter and stamp that value on account.
Thanks in advance
http://www.sundoginteractive.com/sunblog/posts/rollup-summary-with-a-lookup-field-salesforce
<pre>
for ( AggregateResult result :
[ SELECT AVG(Score__c) average, Account__c
FROM ObjectA__c
WHERE ( CreatedDate = THIS_QUARTER
AND Account__c IN Trigger.new
)
GROUP BY Account__c
]
)
{
Trigger.newMap.get( (Id) result.Account__c ).averageScore__c = (Decimal) result.average;
}
</pre>
This code is not tested. I just typed it in here. Let me know if you have any issues with it.
I tried this code
trigger update_fb on feedback__c(after insert,after update)
{
list<aggregateresult> test;
for(feedback__c fb:trigger.new)
{
test=[select avg(Average_Score__c)aver from feedback__c where createddate=this_quarter and account__c=:fb.account__c];
system.debug(test);
string str='' +test[0].get('aver');
fb.account__r.average__c=decimal.valueOf(str);
system.debug(fb.account__r.average__c);
}
}
but i get this error
System.NullPointerException: Attempt to de-reference a null object: Trigger.update_fb: line 9, column 1
Please help me with this error, i do not know what exactly i am doing wrong
Have you tried the code that I posted? It won't have this problem, and it is bulk-safe, unlike your code. You should put queries inside for loops.
Note that my code will be in the Account before update trigger, not the Feedback trigger. You can also put code in the Feedback after trigger - something like this:
<pre>
Map<Id,Account> accountsToUpdate = new Map<Id,Account>();
for ( Feedback__c feedback : Trigger.isDelete ? Trigger.old : Trigger.new )
{
if ( !accountsToUpdate.containsKey( feedback.Account__c ) )
{
accountsToUpdate.put( feedback.Account__c, new Account( Id = feedback.Account__c ) );
}
}
update accountsToUpdate.values();
</pre>
This will cause the related accounts to update themselves (in the Account trigger) any time a Feedback item is inserted, updated, deleted, or undeleted (assuming you trigger on all these events).
Let me know if this solves the problem.