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
Jamison BrownJamison Brown 

Activity Count Trigger

I am trying to get a field populated with the number of activities within an account. Based on what I have seen, it would need to be separated for tasks and events. We dont really use events, so I just need to set it up for tasks. I found some existing code and edited it, but cant get it to work. What am I doing wrong? I am a novice, so please be kind.

 

/* Provide summary of Number of Tasks on Account record */

trigger TaskSumTrigger on Task (after delete, after insert, after undelete,
after update) {

Task[] cons;
if (Trigger.isDelete)
cons = Trigger.old;
else
cons = Trigger.new;

// get list of accounts
Set<ID> acctIds = new Set<ID>();
for (Task con : cons) {
acctIds.add(con.AccountId);
}

Map<ID, Task> tasksForAccounts = new Map<ID, Account>([select Id
,AccountId
from Task
where AccountId in :acctIds]);

Map<ID, Account> acctsToUpdate = new Map<ID, Account>([select Id
,Number_of_Activities__c
from Account
where Id in :acctIds]);

for (Account acct : acctsToUpdate.values()) {
Set<ID> conIds = new Set<ID>();
for (Task con : tasksForAccounts.values()) {
if (con.AccountId == acct.Id)
conIds.add(con.Id);
}
if (acct.Number_of_Activities__c != conIds.size())
acct.Number_of_Activities__c = conIds.size();
}

update acctsToUpdate.values();

}

Jerun JoseJerun Jose

I couldnt see anything wrong with your code. I think it should be working. Could you tell us what exactly is happening?

I just added a final filter for better performance, but that shouldnt change anything really.

 

/* Provide summary of Number of Tasks on Account record */

trigger TaskSumTrigger on Task (after delete, after insert, after undelete, after update) {

	Task[] cons;
	if(Trigger.isDelete)
		cons = Trigger.old;
	else
		cons = Trigger.new;

	// get list of accounts
	Set<ID> acctIds = new Set<ID>();
	for (Task con : cons) {
		acctIds.add(con.AccountId);
	}

	Map<ID, Task> tasksForAccounts = new Map<ID, Account>([select Id, AccountId	from Task where AccountId in :acctIds]);

	Map<ID, Account> acctsToUpdate = new Map<ID, Account>([select Id, Number_of_Activities__c from Account where Id in :acctIds]);

	for (Account acct : acctsToUpdate.values()) {
		Set<ID> conIds = new Set<ID>();
		for (Task con : tasksForAccounts.values()) {
			if (con.AccountId == acct.Id)
				conIds.add(con.Id);
		}
		if (acct.Number_of_Activities__c != conIds.size())
			acct.Number_of_Activities__c = conIds.size();
	}

	if(!acctsToUpdate.isEmpty())
		update acctsToUpdate.values();
}

 P.S. What you are trying to do is to simulate a rollup summary field. There are many other posts on this forum regarding this which have better code quality and are better optimized. Probably you could use one of them.

 

Jamison BrownJamison Brown

When I try to add the trigger, I get the following error message:

 

Error: Compile Error: Invalid initial type LIST<Task> for MAP<Id,Account> at line 15 column 34

 

I am certainly not opposed to looking elsewhere for better code. I am a novice, so I do not know where to go from here. If you can point me in the direction of some of the posts you are talking about, that would be very helpful.

Jerun JoseJerun Jose

It looks like you have updated your code. Could you post the new code where you are facing the error?

davidjgriffdavidjgriff
The error is pretty explicit. Look closely at line 15 and you'll see you have conflicting object types in your Map...