function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
iSqFt_ADiSqFt_AD 

Activity Roll Up Trigger throwing System.LimitException error - How to fix??

I found this trigger and made a couple changes to it in attempts to get a simple roll up count of activities in the Activity History of Accounts. 

 

trigger Activity_Count_2 on Task (after insert, after update, after delete, after undelete) {
List<Account> uplist=new List<Account>();
List<Account> lelist=[select id,Activity_Count_2__c from Account];
List<AggregateResult> st=new List<AggregateResult>();
st=[select count(Id),whoid from task where id=:Trigger.new and status='Completed' group by whoid];
for(Integer i=0;i<st.size();i++)
{
for(Account a:lelist)
{
if(a.id==st[i].get('whoid'))
{
a.Activity_Count_2__c=(Integer)st[i].get('expr0')+a.Activity_Count_2__c;
uplist.add(a);
}
}
}
update uplist;
}

 

Unfortunately, when I went to log an activity at the Account I received this error:

 

"Apex trigger Activity_Count_2 caused an unexpected exception, contact your administrator: Activity_Count_2: System.LimitException: Too many query rows: 50001"

 

 

What am I missing here?

Best Answer chosen by Admin (Salesforce Developers) 
Naidu PothiniNaidu Pothini
trigger Activity_Count_2 on Task (after insert, after update, after delete, after undelete) 
{
	List<Id> taskIds = new List<Id>();
	List<Id> accIds = new List<Id>();
	Map<Id,Account> accMap = new Map<Id,Account>();
	List<Account> updateList = new List<Account>();  // edited 
	
	for(Task t : Trigger.new)
	{
		taskIds.add(t.Id);
		String str = t.whatId;
		if(str.startsWith('001'))     // Task belongs to Account
		{
			accIds.add(t.whatId);
		}
	}
	
	if(accIds.size() > 0)
	{
		for(Account ac : [SELECT Id, Activity_Count_2__c FROM Account WHERE Id in :accIds])
		{
			accMap.put(ac.Id,ac);
		}

		for(AggregateResult ar : [SELECT Count(Id) c, WhatId w FROM Task WHERE whatId IN :accIds GROUP BY WhatId])
		{
			Account acc = new Account();
			
			string str = string.valueOf(ar.get('w'));
			
			acc = accMap.get(str);   // Edited   //
			acc.Activity_Count_2 = Integer.valueOf(ar.get('c'));   // Edited
			
			updateList.add(acc);
		}
	}
	
	if(updateList.size() > 0)
	{
		update updateList;
	}
}

 try this

All Answers

Naidu PothiniNaidu Pothini
trigger Activity_Count_2 on Task (after insert, after update, after delete, after undelete) 
{
	List<Id> taskIds = new List<Id>();
	List<Id> accIds = new List<Id>();
	Map<Id,Integer> acMap = new Map<Id,Integer>();
	List<Account> newList = new List<Account>();
	
	for(Task t : Trigger.new)
	{
		taskIds.add(t.Id);
		String str = t.whatId;
		if(str.startsWith('001'))     // Task belongs to Account
		{
			accIds.add(t.whatId);
		}
	}
	
	if(accIds.size() > 0)
	{
		List<Account> accList = [SELECT Id, Activity_Count_2__c FROM Account WHERE Id in :accIds];

		for(AggregateResult ar : [SELECT Count(Id) c, WhatId w FROM Task WHERE whatId IN :accIds GROUP BY WhatId])
		{
			acMap.put(ar.get('w'), ar.get('c'));  // edited
		}
		
		for(Account acc : accList)
		{
			acc.Activity_Count_2 = acMap.get(acc.Id);
			newList.add(acc);
		}
	}
	
	if(newList.size() > 0)
	{
		insert newList;
	}
}

 Try this.

iSqFt_ADiSqFt_AD

Error: Compile Error: unexpected token: '{' at line 13 column 8

Naidu PothiniNaidu Pothini

I have edited my previous post.

 

check it.

iSqFt_ADiSqFt_AD

Hi Naidu, 

 

I thought I knew how to fix this but no matter the changes I make I keep getting this error:

 

Error: Compile Error: Variable does not exist: WhatId at line 24 column 23

 

It looks like you called it out correctly, but what am I missing?


Thank you as always for your assistance.

Naidu PothiniNaidu Pothini

Please try the code again. i have edited it.

 

iSqFt_ADiSqFt_AD

Boo!

 

Error: Compile Error: Incompatible key type Object for MAP<Id,Integer> at line 24 column 13

Naidu PothiniNaidu Pothini
trigger Activity_Count_2 on Task (after insert, after update, after delete, after undelete) 
{
	List<Id> taskIds = new List<Id>();
	List<Id> accIds = new List<Id>();
	Map<Id,Account> acMap = new Map<Id,Account>();
	List<Account> updateList = new List<Account>();  // edited 
	
	for(Task t : Trigger.new)
	{
		taskIds.add(t.Id);
		String str = t.whatId;
		if(str.startsWith('001'))     // Task belongs to Account
		{
			accIds.add(t.whatId);
		}
	}
	
	if(accIds.size() > 0)
	{
		for(Account ac : [SELECT Id, Activity_Count_2__c FROM Account WHERE Id in :accIds])
		{
			accMap.put(ac.Id,ac);
		}

		for(AggregateResult ar : [SELECT Count(Id) c, WhatId w FROM Task WHERE whatId IN :accIds GROUP BY WhatId])
		{
			Account acc = new Account();
			
			acc = accMap.get(ar.get('w'));
			acc.Activity_Count_2 = ar.get('c');
			
			updateList.add(acc);
		}
	}
	
	if(updateList.size() > 0)
	{
		update updateList;
	}
}

 Try this.

iSqFt_ADiSqFt_AD

You are quite patient and I thank you for your help. 

 

Still getting this error now - Error: Compile Error: Variable does not exist: updateList at line 38 column 16

Naidu PothiniNaidu Pothini

Try the above edited post.

iSqFt_ADiSqFt_AD

Error: Compile Error: Method does not exist or incorrect signature: accMap.put(Id, SOBJECT:Account) at line 22 column 13

 

I even tried to edit acMap to accMap but then I received this error: Error: Compile Error: Incompatible key type Object for MAP<Id,Account> at line 29 column 19

Naidu PothiniNaidu Pothini
trigger Activity_Count_2 on Task (after insert, after update, after delete, after undelete) 
{
	List<Id> taskIds = new List<Id>();
	List<Id> accIds = new List<Id>();
	Map<Id,Account> accMap = new Map<Id,Account>();
	List<Account> updateList = new List<Account>();  // edited 
	
	for(Task t : Trigger.new)
	{
		taskIds.add(t.Id);
		String str = t.whatId;
		if(str.startsWith('001'))     // Task belongs to Account
		{
			accIds.add(t.whatId);
		}
	}
	
	if(accIds.size() > 0)
	{
		for(Account ac : [SELECT Id, Activity_Count_2__c FROM Account WHERE Id in :accIds])
		{
			accMap.put(ac.Id,ac);
		}

		for(AggregateResult ar : [SELECT Count(Id) c, WhatId w FROM Task WHERE whatId IN :accIds GROUP BY WhatId])
		{
			Account acc = new Account();
			
			string str = string.valueOf(ar.get('w'));
			
			acc = accMap.get(str);   // Edited   //
			acc.Activity_Count_2 = Integer.valueOf(ar.get('c'));   // Edited
			
			updateList.add(acc);
		}
	}
	
	if(updateList.size() > 0)
	{
		update updateList;
	}
}

 try this

This was selected as the best answer
iSqFt_ADiSqFt_AD

Unfortunately its the same error - Error: Compile Error: Incompatible key type Object for MAP<Id,Account> at line 31 column 19

Naidu PothiniNaidu Pothini

I have edited the class. Try it.

iSqFt_ADiSqFt_AD

I originally got the error - Error: Compile Error: Invalid field Activity_Count_2 for SObject Account at line 32 column 13


Then I edited the line in the error to:

 

acc.Activity_Count_2__c = ar.get('c');

 but I get this error now: Error: Compile Error: Illegal assignment from Object to Decimal at line 32 column 13

 

I cannot figure out where you used a decimal wrong here.


Naidu PothiniNaidu Pothini

Edited the post. check it.

iSqFt_ADiSqFt_AD

It appears this was fixed correctly. Thank you Naidu!

sastry.soft1.3935945807637227E12sastry.soft1.3935945807637227E12
help me on this error   Error: Compile Error: Variable does not exist: trigger at line


in the query at not in condition

List<Customer__C> listPLAccMgrs = [Select id,Account_Manager__c,Account__c
                                                    FROM Customer_Prac__c
                                                    WHERE ACCOUNT__C
                                                    in:setAcctIds1 and id not in:trigger.newmap().keyset()];