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
Shivani JainShivani 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
AshlekhAshlekh
Hi,

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.

Boom B OpFocusBoom B OpFocus
Shivani,

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;
}
}