1

Here is my batch apex .it is not deleting records

global class BatchProcessorder implements Database.Batchable<sObject>{
    String query;
    DateTime lastTwoDays = DateTime.now().addDays(-30);

    global Database.querylocator start(Database.BatchableContext BC){
        Query = 'SELECT ID FROM order_batch__c where CreatedDate >:lastTwoDays'; 
        return Database.getQueryLocator(query);
    }
    global void execute(Database.BatchableContext BC, List<order_batch__c> scope){
       List<order_batch__c> oppList = new List<order_batch__c>();
       for(order_batch__c a : scope){
       if(a.Save_order__c=false) 
           oppList.add(a);
       }
       delete oppList ;
    }

    global void finish(Database.BatchableContext BC){
    }
}

I have scheduled two to three time with changes .still the same it is happening

Teja
  • 1,422
  • 4
  • 27
  • 58

2 Answers2

6

The reason you are not deleting any records is because your if statement is incorrect. It's assigning false to the Save_Order__c field, and then returning false to the if keyword

You should be using == in the below code:

   if(a.Save_order__c=false) 
       oppList.add(a);

The code below will work correctly:

   if (a.Save_order__c == false) 
       oppList.add(a);

Have a look at How do I start to debug my own Apex code? to get more information on debugging issues like this.

BarCotter
  • 12,331
  • 4
  • 37
  • 58
  • 1
    That's one reason I prefer if(!a.Save_Order__c) instead of if(a.Save_Order__c==false)-- it's less likely to cause problems if you have a typo. Also, it's worth explaining that the reason why if(a.Save_Order__c=false) doesn't work is because it's assigning false to the Save_Order__c field, and then returning false to the if keyword, and thus skips the body of the if block. – sfdcfox Oct 29 '14 at 15:25
  • @sfdcfox always adding value. I've updated my answer to include why the if statement wasn't working. Thanks for the input. – BarCotter Oct 29 '14 at 15:49
-1

Please check if the query you have used returns records. Also check if any of the records meets the criteria specified in the execute method.

SFDC Learner
  • 317
  • 10
  • 29