You need to sign in to do that
Don't have an account?
Sharad Jain 9
Issue with Aggregate Results
I have to count all the tasks or activities for every account record and update a custom account field with number. I have 20 thousand accounts and I have to do it with script not with triggers. I am facing problem while iterating aggregate results. Can anyone help me on this .. Below is my code snippet
Set<Id> Accid=new Set<Id>();
List<Integer> check=new List<Integer>();
for(Account Myacc:[SELECT Id,name FROM Account])
{
Accid.add(Myacc.Id);
}
for(List<AggregateResult> lstAR:[Select Account.name, count(Id) Cnt from Task where Activity_Type__c='member support' and Type_of_Member_Support__c ='Flash consulting' and Account.name=:Accid and AccountId!=null group by Account.name]){
check.add(lstAR);
Integer intdata =(integer)check[0].get('Cnt');
Account acc= new Account();
acc.Member_Support_Delivered__c=intdata;
}
Set<Id> Accid=new Set<Id>();
List<Integer> check=new List<Integer>();
for(Account Myacc:[SELECT Id,name FROM Account])
{
Accid.add(Myacc.Id);
}
for(List<AggregateResult> lstAR:[Select Account.name, count(Id) Cnt from Task where Activity_Type__c='member support' and Type_of_Member_Support__c ='Flash consulting' and Account.name=:Accid and AccountId!=null group by Account.name]){
check.add(lstAR);
Integer intdata =(integer)check[0].get('Cnt');
Account acc= new Account();
acc.Member_Support_Delivered__c=intdata;
}
Try below code :
Hope this helps you!
If my answer helps resolve your query, please mark it as the 'Best Answer' & upvote it to benefit others.
Thanks
Varaprasad
@For SFDC Support: varaprasad4sfdc@gmail.com
Blog: http://salesforceprasad.blogspot.com/
Salesforce latest interview questions :
https://www.youtube.com/channel/UCOcam_Hb4KjeBdYJlJWV_ZA?sub_confirmation=1
But I have to add one more filter in which I have to Pull all activities(Tasks) that were created between Account custom field (membership expiration date) and 365 days before that date(Account>Membership expirationdate-365days) . Below filter should apply on tasks while pulling data.
consider only When Task.Activity_Type_C = Member Support AND task.Type_of_Membersupport_C = Flash Consulting. update this count on Account.Membership Support Delivered field.
Instead of using filter here
List<AggregateResult> lstAR = [SELECT Count(Id) cnt, WhatId aid FROM Task
WHERE Activity_Type__c='member support' and Type_of_Member_Support__c ='Flash consulting' and whatId IN :accIds GROUP BY WhatId];
we have to use filter here i guess
Set<Id> accIds=new Set<Id>();
for(Account Myacc:[SELECT Id,name FROM Account])
{
accIds.add(Myacc.Id);
}
Hope this helps you!
If my answer helps resolve your query, please mark it as the 'Best Answer' & upvote it to benefit others.
Thanks
Varaprasad
@For SFDC Support: varaprasad4sfdc@gmail.com
Blog: http://salesforceprasad.blogspot.com/
Salesforce latest interview questions :
https://www.youtube.com/channel/UCOcam_Hb4KjeBdYJlJWV_ZA?sub_confirmation=1
I am not able to create a soql for this