You need to sign in to do that
Don't have an account?
Shivani Jain
How to update a field on account after a task/event is deleted from Account using trigger.
I want to update a field on account based on the list of tasks that are remaining after deleting a task from Account. The value is being populated from Task or list of task which is remained on account after deleting a task. How to achieve it because trigger.new doesnt work on delete operation and trigger.old will include the value of that task also , which i want to delete. Please help.
Thanks in advance
Thanks in advance
You can do this by trigger on Task.
:- When you delete any task then a trigger will fire and will get the id for parent in your case It would be account.
:- In trigger if you get the id of account then you can update according to you requirement.
Click on Setup --> Under Customize--Acctivities -> Task Trigger here you can create trigger.
You can use trigger on after delete. Assuming the account field that you want to clear out is "Task_Thing__c".
The code should look something like this:
trigger Task on Task (after delete) {
List<Account> lstAccsToupdate = new List<Account>();
Set<Id> setAccountIds = new Set<Id>();
if (Trigger.isDelete){
for (Task t : trigger.Old) {
String whatId;
if(t.WhatId != null) whatId = t.WhatId;
if (whatId.startsWith('001')) {
setAccountIds.add(whatId);
}
}
for (Account a : [select Id, Task_Thing__c from Account where Task_Thing__c !=null and Id in :setAccountIds]) {
a.Task_Thing__c = null;
lstAccsToupdate.add(a);
}
if (!lstAccsToupdate.isEmpty()) update lstAccsToupdate;
}
}