You need to sign in to do that
Don't have an account?
Nicholas Sewitz
Last Activity Date By Account Owner
Hi I am trying to populate a custom field on the Account object with the most recent Task Activity Date where the Task Owner = Account Owner. This is the trigger I have so far, I can't figure out how to add the t.ownerid = a.ownerid filter.
trigger LastActivityByOwner on task(after insert) { map<id,Account> accounts = new map<id,account>(); for(task record:trigger.new) { if(record.ownerid.getsobjecttype()==user.sobjecttype) { if(record.ownerid!=null) { accounts.put(record.accountid,new account(id=record.accountid,Last_Activity_By_Owner__c=record.ActivityDate)); } } } update accounts.values(); }
Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger LastActivityByOwner caused an unexpected exception, contact your administrator: LastActivityByOwner: execution of AfterInsert caused by: System.FinalException: Record is read-only: Trigger.LastActivityByOwner: line 11, column 1
I would go in a different direction...
First, you need to make sure that the Task record being processed is linked to an Account. If it's not then you don't need to process it any further. If it is then you need to perform a query of the Account object in order to grab the OwnerId for the Account. Once you have all of the Account.OwnerId values you may then iterate over all of the Tasks again to see if the Task owner is the same as the related Account owner. Then, you will construct a list of Account records to be updated with the Last_Activity_By_Owner__c value based upon the Task that was inserted. Last, you will update the Account records.
I highly recommend that you perform the Account updates asynchronously because you don't really need to hold up the Task trigger processing to accomplish your goal. This may need to be tweaked a little but this should be a good start for the trigger:
And here is the class called by the trigger that will perform the asynchronous Account updates.
Let me know if you have questions. And sorry for the verbose code. But I thought it would be more helpful.
-greg
is throwing an error "Illegal assignement from List to Map"
-greg
Task.ActivityDate is a Date type field. My assumption then is that all of the Tasks processed by the trigger will have the same value for this field so we don't need to compare the values for each Task in order to determine a winning value to use on the Account. If this assumption is incorrect then the logic I am suggesting will need to be modified. Otherwise, this is what you need to do.
Slightly modify the for loop logic in the trigger as follows: Let me know how everything goes…
-greg
Class:
Please try this
I guess this might work
trigger updateAccounts on task(after insert)
{
Set<Id> accountIds=new Set<Id>();
for(Task tas:Trigger.new)
{
if(tas.AccountId !=null)
{
accountIds.add(tas.AccountId);
}
}
if(!accountIds.isEmpty())
{
Map<Id,Account> mapAccount=new Map<Id,Account>([Select Id,OwnerId from Account where ID IN : accountIds]);
if(!mapAccount.isEmpty())
{
List<Account> lstAccount=new List<Account>();
for(Task tas:Trigger.new)
{
if(tas.AccountId !=null)
{
if(mapAccount.containsKey(tas.AccountId))
{
if(mapAccount.get(tas.AccountId).OwnerId == tas.OwnerId)
{
Account acc=new Account(Id=tas.AccountId,Last_Activity_By_Owner__c =tas.ActivityDate);
lstAccount.add(acc);
}
}
}
}
if(!lstAccount.isEmpty())
{
update lstAccount;
}
}
}
}
Please try this code