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
sonisha ssonisha s 

Trigger to delete a task associated with an account before deleting the account

FARSANA PSFARSANA PS
Hai Sonisha,

Try this...
 
trigger taskDeletionTrigger on Account (before delete) { 
    list<task> taskList=[select WhoId, Id from Task where WhoId IN:Trigger.oldMap.keyset()];
    if(tasklist.size()>0)
        delete tasklist;
}

Hope this helps..
Deepali KulshresthaDeepali Kulshrestha
Hi sonisha,

I have gone through your problem please refer bellow code:-  


Apex Class:-


public class DeleteAccount {
    public static void testd(List<Account> lacc){
        List<Task> ltask=[select WhoId,WhatId,Id,AccountId from Task where AccountId IN:lacc];
        
        if(ltask.size()>0){
            delete ltask;
        }
        
    }

}



Trigger

trigger TestAccount on Account (before delete) {
    if(Trigger.isBefore && Trigger.isDelete){
        DeleteAccount.testd(Trigger.old);
    }

}



I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha
www.kdeepali.com