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
Erin Ryan 62Erin Ryan 62 

TaskDeleteBatch code modification

I need to tweak the code to any Task that contains the word Pardot will be deleted. I'm not a developer so any assistance will be appreciated. Thanks so much. 

Apex Class
public class TaskDeleteBatch implements Database.Batchable<sObject>{
    public Database.QueryLocator start(Database.BatchableContext bc){
      String query ='select id from task where createdby.Name =\'B2BMA Integration\' AND Subject LIKE \'Pardot List Email%\'';
        return Database.getQueryLocator(query);
    }
    public void execute(Database.BatchableContext bc, list<Task> scope){
        Database.delete(scope);  }
     public void finish(Database.BatchableContext bc)   }}

TaskDeleteBatchTest
@isTest public class TaskDeleteBatchTest {
    @isTest static void testMethod1(){
       Lead l = new lead();
        l.LastName = 'Test';
        l.Company = 'Test Company';
        l.How_did_lead_hear_about_us__c='Email Campaign';
        Insert l;
        
        Profile p = [SELECT Id FROM Profile WHERE Name='B2BMA Integration User']; 

        User u = new User(Alias = 'standt', Email='standarduser@leopardsolutions.com', 
                          EmailEncodingKey='UTF-8', LastName='B2BMA Integration', LanguageLocaleKey='en_US', 
                          LocaleSidKey='en_US', ProfileId = p.Id, 
                          TimeZoneSidKey='America/Los_Angeles', UserName='b2bmaintegration@leopardsolutions.com');
        
        system.runAs(u){
            Task t = new Task();
            t.Subject = 'Pardot List Email';
            t.whoId = l.Id;
            Insert t;   
            
            system.debug([select id,createdBy.Name from task][0].createdBy.Name);
        }
        
        Test.startTest();
          Database.executeBatch(new TaskDeleteBatch());
    Test.stopTest();

    }
}
@anilbathula@@anilbathula@
Hi Erin,

Try this code:
 
public class TaskDeleteBatch implements Database.Batchable<sObject>{
    public Database.QueryLocator start(Database.BatchableContext bc){
      string usrname='B2BMA Integration';
      String query ='select id from task where createdby.Name =: usrname AND Subject LIKE \'Pardot List Email%\'';
        return Database.getQueryLocator(query);
    }
    public void execute(Database.BatchableContext bc, list<Task> scope){
        Database.delete(scope); 
    }
   public void finish(Database.BatchableContext bc) {
   }
}
Thanks
Anil.B
 
Erin Ryan 62Erin Ryan 62
Thanks, I'll test the code and keep you posted. 
Erin Ryan 62Erin Ryan 62
User-added image  The modification isn't deleting all of the tasks?
Erin Ryan 62Erin Ryan 62
Hi Anil , this is the modified criteria. Sorry if I wasn't clear. 
Object > Tasks
Subject starts with > Pardot
Created By > Any User
Created Date > all dates