You need to sign in to do that
Don't have an account?
Niraj Kumar 9
trigger to count the task of Account ,contact, opportunity and update in the Account count field
Hi Guys,
I need a trigger to count the task of Account, Contact, opportunity and update in the Account count field with sum of Account task, Contact task, Opportunity task.
EX - Create a Account, create 2 task on Account, create a contact related to this account and create 1 task, create a opportunity related to this account create 2 task. In Account Count Field it shoud be 5 taks.
I need a trigger to count the task of Account, Contact, opportunity and update in the Account count field with sum of Account task, Contact task, Opportunity task.
EX - Create a Account, create 2 task on Account, create a contact related to this account and create 1 task, create a opportunity related to this account create 2 task. In Account Count Field it shoud be 5 taks.
I have tested the above code and it is working. All the tasks related to accounts, contacts and cases will be linked to account automatically by salesforce.
Thanks,
Srinivas
- Please mark as solution if your problem is resolved.
You can create trigger on task. Following are the steps need to be done in trigger
In trigger:(After Insert/update/Delete of task)
1. Create below variables
Set<Id> setAccountId;
Set<Id> setContactId;
Set<Id> setOpportunityId;
map<Id, Integer> mapAccoutIdwithTaskCount.
2. iterate over Task
if(task is related to Account)
setAccountId.add(AccountId);
else if(task is related to Opportunity )
setOpportunityId.add(OpportunityId);
else if(task is related to Contact )
setContactId.add(ContactId);
3. Query Account with task
for(Account objAccount: [Select Id, (Select Id From Tasks) From Account])
{
if(!objAccount.Tasks.isEmpty())
{
mapAccoutIdwithTaskCount.put(objAccount.Id,objAccount.Tasks.size())
}
else
{
mapAccoutIdwithTaskCount.put(objAccount.Id,0)
}
}
4. Query Account with task
for(Contact objContact: [Select Id, (Select Id From Tasks) From Contact])
{
if(objContact.AccountId != null)
{
Integer intCount = 0;
if(mapAccoutIdwithTaskCount.containsKey(objContact.AccountId))
intCount = mapAccoutIdwithTaskCount.get(objContact.AccountId);
if(!objContact.Tasks.isEmpty() )
{
mapAccoutIdwithTaskCount.put(objContact.AccountId,intCount + objContact.Tasks.size())
}
else
{
mapAccoutIdwithTaskCount.put(objContact.AccountId,intCount)
}
}
}
5. *repeat the 4th step for opportunity*
6. List<Account> lstAccountToUpdate = new list<Account>();
for(Id idAccount: mapAccoutIdwithTaskCount.keySet())
{
Account objAccount = new Account(Id = idAccount, Count = mapAccoutIdwithTaskCount.get(idAccount) );
lstAccountToUpdate.add(objAccount);
}
if(!lstAccountToUpdate.isEmpty())
update lstAccountToUpdate;
Thanks,
Vedashri
Hey Niraj,
This is standard Behaviour . You can check out below url for better understanding
https://help.salesforce.com/apex/HTViewSolution?id=000003747&language=en_US
Its not working in our sandbox. Can u please modify the code?
Thanks.
Your code is working fine in other org but here (in our org) need to be call future method and pass the query in future method and update the count Because I am getting error like ::::
Thanks.
line 9, column 1 means soql query
Thanks.
Here is code with your step. Error::::
Apex trigger NewAccountCountTask caused an unexpected exception, contact your administrator: NewAccountCountTask: execution of AfterInsert caused by: System.SObjectException: SObject row was retrieved via SOQL without querying the requested field: Contact.AccountId: Trigger.NewAccountCountTask: line 42, column 1 getting error while creating task on Account, contact, opportunity.
Please help me .
Sorry for late reply. In Line number 37 you have queried Contact but not added AccountId field in it. But AccountId you are using in Line number 42 that is you are gettig this issue.
Please chnage Line 37 fo loop fro below code If you got solution please mark best answer. Or let me know if you need more information.
Thanks,
Vedashri