You need to sign in to do that
Don't have an account?
Mollie Bodensteiner 6
Aggregate Query Has Too Many Rows for Direct assignment, Use For Loop | Counting Tasks
Hello,
I am stuggling with this trigger to count the number of open tasks on a lead. I am receiving the following error:
UpdateContactClosedTasks: execution of AfterDelete caused by: System.QueryException: Aggregate query has too many rows for direct assignment, use FOR loop: External entry point
Here is the trigger:
trigger UpdateLeadOpenTasks on Task (after insert, after undelete,
after update, after delete) {
// Declare the variables
public set<Id> LeadIDs = new Set<Id>();
public list<Lead> LeadsToUpdate = new List<Lead>();
// Build the list of Leads and Contacts to update
if(Trigger.isInsert || Trigger.isUnDelete || Trigger.isUpdate){
for(Task t: Trigger.new){
if(t.WhoId<>null){
if(string.valueOf(t.WhoId).startsWith('00Q'))
LeadIDs.add(t.WhoId);
}
}
}
if(Trigger.isDelete || Trigger.isUpdate){
for(Task t: Trigger.old){
if(t.WhoId<>null){
if(string.valueOf(t.WhoId).startsWith('00Q'))
LeadIDs.add(t.WhoId);
}
}
}
// Update the Leads
if(LeadIDs.size()>0){
for(Lead l: [Select l.Id, l.Open_Activities__c,
(Select Id From Tasks where IsClosed = False)
From Lead l where Id in :LeadIDs])
LeadsToUpdate.add(new Lead(Id=l.Id, Open_Activities__c = l.Tasks.size()));
update LeadsToUpdate;
}
if(LeadstoUpdate != null && !LeadsToUpdate.isEmpty())
Database.update(LeadsToUpdate);
}
Any help would be greatly appreciated.
I am stuggling with this trigger to count the number of open tasks on a lead. I am receiving the following error:
UpdateContactClosedTasks: execution of AfterDelete caused by: System.QueryException: Aggregate query has too many rows for direct assignment, use FOR loop: External entry point
Here is the trigger:
trigger UpdateLeadOpenTasks on Task (after insert, after undelete,
after update, after delete) {
// Declare the variables
public set<Id> LeadIDs = new Set<Id>();
public list<Lead> LeadsToUpdate = new List<Lead>();
// Build the list of Leads and Contacts to update
if(Trigger.isInsert || Trigger.isUnDelete || Trigger.isUpdate){
for(Task t: Trigger.new){
if(t.WhoId<>null){
if(string.valueOf(t.WhoId).startsWith('00Q'))
LeadIDs.add(t.WhoId);
}
}
}
if(Trigger.isDelete || Trigger.isUpdate){
for(Task t: Trigger.old){
if(t.WhoId<>null){
if(string.valueOf(t.WhoId).startsWith('00Q'))
LeadIDs.add(t.WhoId);
}
}
}
// Update the Leads
if(LeadIDs.size()>0){
for(Lead l: [Select l.Id, l.Open_Activities__c,
(Select Id From Tasks where IsClosed = False)
From Lead l where Id in :LeadIDs])
LeadsToUpdate.add(new Lead(Id=l.Id, Open_Activities__c = l.Tasks.size()));
update LeadsToUpdate;
}
if(LeadstoUpdate != null && !LeadsToUpdate.isEmpty())
Database.update(LeadsToUpdate);
}
Any help would be greatly appreciated.
Sorry for late response.
That was the simple logic trigger to count the child records.
As per your requirement please update the your trigger with below trigger.
Please let me know if this help and choose the best answer.
Thanks
Shivdeep
All Answers
please try the below code... Please let me know, if this help you..!!!
I am getting an Error: Complie Error: unexpected token: else at line 28. Also does this take into account that the task has to be open?
Thoughts?
Sorry for late response.
Yesterday I didn't check it, I wrote this on notepad and paste on the code section. there is some error while saving.
The error is :-
1- replace " string.valueof(t.whoid).startwith('OOQ') " with " string.valueof(t.whoid).startswith('00Q') " on line no. 8,16,23,31.
2- on line no. 38 replace "leadsIds" with " leadIds " .
There is some points for task (activities), I think you know about that :
- You can not delete the open activity, you have to change the status to 'Completed' and then from activity history you can delete the activities.
if you do the same then this trigger will run fine.please let me know if this help.
thanks
Shivdeep
It is working, except it doesn't decrease the open task count when the task is closed. I updated it to only count open tasks, but am just struggling with decreasing the value of the count once the task is closed.
Thanks so much.
Sorry for late response.
That was the simple logic trigger to count the child records.
As per your requirement please update the your trigger with below trigger.
Please let me know if this help and choose the best answer.
Thanks
Shivdeep