You need to sign in to do that
Don't have an account?

Retrieving activity records whose RelatedTo is of type Account
I'm trying to get the activity records whose RelatedTo is of type Account in order to update Total_Touches field on Account. Somehow it is not getting the Activity records and I'm not getting any error. Below is my code, Any advice please?
public static set<Id> filterTaskOrEventRecords(List<sObject> lstActivities,map<Id,sObject> mapOldActivities)
{
set<Id> setAccountIds = new set<Id>();
for(sObject objActivity : lstActivities)
{
Boolean isRelatedToCompany = objActivity.get('AccountId') != null ? (((Id)objActivity.get('AccountId')).getsObjectType() == Account.sObjectType ? true : false) : false;
// Filtering records in insert,update and delete case.
if((trigger.isInsert) || (trigger.isDelete) || trigger.isUpdate)
{
if(isRelatedToCompany)
setAccountIds.add((Id)objActivity.get('AccountId'));
if( trigger.isUpdate && ((Id)mapOldActivities.get(objActivity.Id).get('AccountId')).getsObjectType() == Account.sObjectType)
setAccountIds.add((Id)mapOldActivities.get(objActivity.Id).get('AccountId'));
}
}
return setAccountIds;
}
public static set<Id> filterTaskOrEventRecords(List<sObject> lstActivities,map<Id,sObject> mapOldActivities)
{
set<Id> setAccountIds = new set<Id>();
for(sObject objActivity : lstActivities)
{
Boolean isRelatedToCompany = objActivity.get('AccountId') != null ? (((Id)objActivity.get('AccountId')).getsObjectType() == Account.sObjectType ? true : false) : false;
// Filtering records in insert,update and delete case.
if((trigger.isInsert) || (trigger.isDelete) || trigger.isUpdate)
{
if(isRelatedToCompany)
setAccountIds.add((Id)objActivity.get('AccountId'));
if( trigger.isUpdate && ((Id)mapOldActivities.get(objActivity.Id).get('AccountId')).getsObjectType() == Account.sObjectType)
setAccountIds.add((Id)mapOldActivities.get(objActivity.Id).get('AccountId'));
}
}
return setAccountIds;
}
P.S. Please use the code blocks when pasting code. It is easier to read and edit.
Thanks for your reply. I have replacaed the field AccountID to WhatID but when I create a related task to an Account, the task gets created on both Accounts but the Total Activities does not get updated right away on the related Account until I create another then it updates. Any ideas please?
Below is my task trigger
Task trigger handler
Thanks for your help!
Your trigger looks good to me. What I would recommend is to try adding debugg statements to verify if you are getting the expected result. I tried executing a simplified version of the code and it gave me positive results.
The code I used is below. The debug statement output was
12:25:28:279 USER_DEBUG [35]|DEBUG|$$$$$$$$$$$$$$ {00190000006ainBAAQ}
In the populateTouchInfoOnAccount method debug the value of setAcctIdsToUpdate. If the values are as per your expectation, you will need to debug the update logic in the SL_UtilActivityHandler.updateActivityInfoOnAccount(setAcctIdsToUpdate); method instead.