• Mollie Bodensteiner 6
  • NEWBIE
  • 10 Points
  • Member since 2016

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 3
    Replies
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. 
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 have a trigger to rollup the open tasks at lead and contact level. The trigger occasionally throws exception - caused by: System.QueryException: Aggregate query has too many rows for direct assignment, use FOR loop
Please suggest how to fix this issue

trigger CalculateOpenTasks on Task (after delete, after insert, after undelete, 
after update) {
// Declare the variables
public set<Id> LeadIDs = new Set<Id>();
public list<Lead> LeadsToUpdate = new List<Lead>();
public set<Id> ContactIDs = new Set<Id>();
public list<Contact> ContactsToUpdate = new List<Contact>();

// 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')&&t.IsClosed==False && t.Marketing__c==True)
            LeadIDs.add(t.WhoId);
            if(string.valueOf(t.WhoId).startsWith('003')&&t.IsClosed==False &&  t.Marketing__c==True)
            ContactIDs.add(t.WhoId);
        }
    }
}

if(Trigger.isDelete || Trigger.isUpdate){
    for(Task t: Trigger.old){
        if(t.WhoId<>null){
            if(string.valueOf(t.WhoId).startsWith('00Q')&&t.IsClosed==False &&  t.Marketing__c==True)
            LeadIDs.add(t.WhoId);
            if(string.valueOf(t.WhoId).startsWith('003')&&t.IsClosed==False &&  t.Marketing__c==True)
            ContactIDs.add(t.WhoId);
        }
    }
}

// Update the Leads

if(LeadIDs.size()>0){
for(Lead l: [Select l.Id, l.Open_Tasks__c,
(Select Id From Tasks where IsClosed = False AND Marketing__c=True)
From Lead l where Id in :LeadIDs])
LeadsToUpdate.add(new Lead(Id=l.Id, Open_Tasks__c = l.Tasks.size()));
update LeadsToUpdate;
}

// Update the Contacts

if(ContactIDs.size()>0){
for(Contact c: [Select c.Id, c.Open_Tasks__c,
(Select Id From Tasks where IsClosed = False AND Marketing__c=True )
From Contact c where Id in :ContactIDs])
ContactsToUpdate.add(new Contact(Id=c.Id, Open_Tasks__c = c.Tasks.size()));
update ContactsToUpdate;
}

}