I'm trying to identify task records in a production org. The problem is that the data volume is too large to create a report or one-off SOQL Task records. Our task records are huge and are in desperate cleanup, hence us needing to identify certain information before proceeding with mass deletions. There are 1907144 task records (3.6 GB) and 40% of our data usage.

So I thought I would try a batch SOQL to buy more processing time. However I'm getting the following error:

First error: [REQUEST_RUNNING_TOO_LONG] Your request was running for too long, and has been stopped.

I've tried multiple methods of speeding up the query by adding or removing filter criteria, but it always seems to timeout in Production. Putting a limit on the SOQL doesn't seem to help either. Is there something I can change in the batch class to increase the timeout limit or could I add or remove parts of my filter criteria? Note: the Subject LIKE '%Opportunity has been%' is really what I am trying to identify and needs to be in the query somewhere.

Here's my relevant code (edited per Eric's comment below, but it still failed :(

global class batchableSOQL implements Database.Batchable<sObject>, Database.Stateful{

    global List<String> taskRecordIds = new List<String>();
    global String s;
    global List<String> stringLines = new List<String>();

    global Database.QueryLocator start(Database.BatchableContext BC){
        //String twoYearsAgo =  DateTime.now().AddDays(-730).format('yyyy-MM-dd\'T\'hh:mm:ss\'z\'');
        String query = 'select id, subject, type, ActivityDate, WhatId, What.name, CreatedDate from Task WHERE contains_Opportunity_has_been__c  = true  AND Type = \'System Generated Email\' AND Isdeleted = false AND CreatedDate > 2015-05-22T00:00:00Z';
    System.Debug('queryis= '+query);
    return Database.getQueryLocator(query);

    global void execute(Database.BatchableContext BC, List<Task> scope){
        //add the found records to a csv

    global void finish(Database.BatchableContext BC){
        //notify admin of records found with csv attachment

  • 1
    Fyi there's an addYears method... – Adrian Larson Dec 20 '16 at 17:07
  • 1
    The major issue is in these two filters only: LIKE \'%Opportunity has been%\' AND Subject LIKE \'%A Related%\' . Try if you can change into some thing else. – Ashwani Dec 20 '16 at 17:16
  • 4
    One thing I have done when I needed to filter based on "LIKE" for a one off process is create a formula field checkbox to be true if the LIKE condition is met. Then in the query use that checkbox = true. In reality it is still a full table scan IIRC but seems to get one over the hump in situations like these. no guarantee it will work in your specific case though but may be worth a try... – Eric Dec 20 '16 at 17:27
  • 1
    I have also seen where batch queries will fail and the error gets thrown in SF own query locator class and not the batch. In those cases I ended up querying the entire table without filters and processed them in the execute method filtering them there. I am confident that will work if you can make it fit into your design – Eric Dec 20 '16 at 18:30
  • 1
    How about dataloader ? If your intention is to cleanup..do you process any logic in execute ? – bharath Dec 20 '16 at 20:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.