+ Start a Discussion
santhosh konathala 17santhosh konathala 17 

Can anybody help how to delete records older than 30 days in an object.The below code is not fetching records.Please help me..

global class TechMDeletion implements Database.Batchable<sObject>
{   
    
    
  
    global Database.QueryLocator start(Database.BatchableContext bc)
    {
        date d=system.today().adddays(-30);        
        return Database.getQueryLocator([Select id from TechM__c where CreatedDate =:d]);


  
    } 
    
    
    
    global void execute(Database.BatchableContext BC, list<sObject> scope)
    {     
    system.debug('records fetched are'+scope);
        
 
        
            delete scope;   
            DataBase.emptyRecycleBin(scope); 
    }
    
    
    global void finish(Database.BatchableContext BC) 
    {                 
        
        
       
    }
}
Best Answer chosen by santhosh konathala 17
Santosh Reddy9989Santosh Reddy9989
Hi,
In query editor u have to use below formate,

Select id from TechM__c where CreatedDate <= 2016-11-15T00:00:00Z
                           
Select id from TechM__c where DAY_ONLY(CreatedDate) <= 2016-11-15

Select id from TechM__c where Created
Date <=  YESTERDAY
                           
Select id from TechM__c where CreatedDate =  TODAY

All Answers

Santosh Reddy9989Santosh Reddy9989
Hi santosh, use below start function 

 

  global Database.QueryLocator start(Database.BatchableContext bc)
    {
        date d=system.today().adddays(-30);        
        return Database.getQueryLocator( 'Select id from TechM__c where CreatedDate <=:d' );
    } 
santhosh konathala 17santhosh konathala 17
Hi Santoshreddy,

The below query not fetching records in query editor saying that unknown parsing query error....could you help on this
Santosh Reddy9989Santosh Reddy9989
Hi,
In query editor u have to use below formate,

Select id from TechM__c where CreatedDate <= 2016-11-15T00:00:00Z
                           
Select id from TechM__c where DAY_ONLY(CreatedDate) <= 2016-11-15

Select id from TechM__c where Created
Date <=  YESTERDAY
                           
Select id from TechM__c where CreatedDate =  TODAY
This was selected as the best answer
santhosh konathala 17santhosh konathala 17
Thanks Santosh,

your query saved me...