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
Pooja Singh 21Pooja Singh 21 

Batch job to delete all tasks related to Case with record type X.

Hi,

I need help writing a batch job which can help deleting all tasks related to cases with record type 'X'. Can anyone share a dummy code for same?

Thanks,
Pooja
sandeep sankhlasandeep sankhla
Hi Pooja,

Please use below code for reference
 
global class TaskBatch  implements Database.Batchable<Sobject> 
{
    
    
    /*  Start : Constructor */
    public TaskBatch( )  
    {
        strQuery = ''; 
        
    }
	/* End : Constructor*/
	
	
    /*
        @MethodName : Start 
        @param      : Database.BatchableContext BC
        @Description: 
    */
    global Database.QueryLocator start(Database.BatchableContext bc) 
    {
    	      	
    	/* Main batch Query */
    	strQuery = 'SELECT ID FROM Case WHERE recordtype Id is 'yourRecordtype';
        return Database.getQueryLocator(strQuery);
    }
    
    /*
        @MethodName : Execute 
        @param      : Database.BatchableContext BC, List<Meeting_Log__c> lstSobjects
        @Description: Processes the Meeting records recieved from the Start method in batches
    */
    
    global void execute(Database.BatchableContext BC, List<SObject> lstSobjects )
    {
    	here collect all case ids and get all related tasks  and delete them
    }
    
     
    
    /*
        @MethodName : finish 
        @param      : Database.BatchableContext BC
        @Description: Performs the post-execution steps
    */
    global void finish(Database.BatchableContext BC)
    {
    	
        
    }
        
}

Please implement this and let me know if it helps you...
P.S. If my answer helps you to solve your problem please mark it as best answer. It will help other to find best answer.

Thanks,
Sandeep
Salesforce Certified Developer 
 
Pooja Singh 21Pooja Singh 21
Hi Sandeep,

Thanks for your reply.

I am stuck only in the execute method. I have got all the cases with record type X now in a list. Can you please give me a proper code to extract all the tasks related to those cases and delete them. This will help a lot.

Thanks,
Pooja
sandeep sankhlasandeep sankhla
Hi you can simply query on Task like below...
 
Select t.WhoId, t.WhatId From Task where whoId in SetCaseIds

then you can collect all tasks and delset them

please check and let me kbow if it helps ..
sandeep sankhlasandeep sankhla
for(Task objTask : [Select WhoId, WhatId From Task where WhoId IN : setCaseIds])
{
	lastTaskTodeleted.add(objTask);
}


delete lastTaskTodeleted;
sandeep sankhlasandeep sankhla
Hi Pooja,

Please try above code snippt and let me knwo if it works you....also let me know if you need any clarfiction for above code..

thanks
doravmondoravmon
First you need a query of all the tasks:
Task[] task = [select Id, RecordType... from Task where...]
then:

List<Task> deleteList = new List<Task>()
for (Task t in task)
{
    if(t,recordType=='X')
        deleteList.add(t);
}

Last, delete those records

if(deleteList.size>0)
{   delete deleteList;
    deleteList.clear()
}

hope this help~
Pooja Singh 21Pooja Singh 21
Hi Sandeep, 

I am working on the code. There may be a possibility to add couple of more requirements in it. Once it works, I will let you know.

 
Pooja Singh 21Pooja Singh 21
Hi Doravmon,

I do not think that there is any Record Type field for Task object so this approach is not suitable.