0

I have a requirement where I have written batch class to delete old records(Have to house keep old records for many objects as we don't use them). Just to test it I have created it in my Dev org for contact object. But challenge I am facing while I am moving the change to one of my sandbox instance is , I am no able to pass the date in custom label. Values in custom label is taking as string but not the date. I need to delete the records created last 3 months or 6 months and date has to be given in a custom label not in actual batch class as per my client requirement. Just for testing I am deleting records created last 3 days back, as below code but it throws " 18:54:32:065 FATAL_ERROR System.TypeException: Invalid date/time: LAST_N_DAYS:3 "

I have given LAST_N_DAYS:3 as value in custom label. And batch class is as below.

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

    // instance member to retain state across transactions
        global Integer contactsProcessed= 0;
        global Database.QueryLocator start(Database.BatchableContext bc) {
        string stringDate = System.Label.SetConfigurableDate;
        Datetime myDate = DateTime.parse(stringDate);
        system.debug('custom label date value is  --->'+myDate );
        string contactsToDelete = 'Select ID,LastName,FirstName,Name from Contact where CreatedDate =: myDate ';
        system.debug('Contacts to be deleted are --->'+contactsToDelete);
        return Database.getQueryLocator(contactsToDelete);
    }
    global void execute(Database.BatchableContext bc, List<Contact> contactList){
             system.debug('Contacts to be deleted are --->'+contactList);
             list<contact> contacts= new list<contact>();
             for(Contact contact:contactList){

             contacts.add(contact);
             contactsProcessed = contactsProcessed+ 1;

             }
             delete contactList;
        }

    global void finish(Database.BatchableContext bc){
        System.debug(contactsProcessed+ ' contact records processed. Hurray !!');
        AsyncApexJob job = [SELECT Id, Status, NumberOfErrors, 
            JobItemsProcessed,
            TotalJobItems, CreatedBy.Email
            FROM AsyncApexJob
            WHERE Id = :bc.getJobId()];
        // call some utility to send email
         EmailUtils.sendMessage(job, contactsProcessed);
    }    
}

Can anyone please help me here.

0

There is a predefined function valueOf from Date class.

Date myDate = Date.valueOf(System.Label.SetConfigurableDate;);

Also CreatedDate is a datetime field so make sure you convert the date to datetime. This can be done by utilizing the method newInstance() from DateTime class.

createdDateTime = DateTime.newInstance (myDate.year(), myDate.month(), myDate.day())
2
  • Thank you for your response Rohit but still it does not work for me. It throws runtime error "19:43:05:070 FATAL_ERROR System.TypeException: Invalid date: LAST_N_DAYS:3" Here is me class edited: string stringDate = System.Label.SetConfigurableDate; Date myDate = Date.valueOf(System.Label.SetConfigurableDate); string contactsToDelete = 'Select ID,LastName,FirstName,Name from Contact where CreatedDate =: DateTime.newInstance (myDate.year(), myDate.month(), myDate.day()) '; – Maddy Jan 13 '20 at 14:14
  • @sfdcfox pointed out in his answer. You have not binded the variable correctly in your dynamic SOQL. Follow his suggestion and it should resolve your query. – Rohit Mourya Jan 13 '20 at 14:18
0

You wouldn't be able to use a "binding" like that, but since you have a String for you SOQL:

string contactsToDelete = 'Select ID,LastName,FirstName,Name from Contact where CreatedDate = '+myDate;

This should give you the correct SOQL for this to work.

8
  • Still same error: FATAL_ERROR System.TypeException: Invalid date: LAST_N_DAYS:3 string contactsToDelete = 'Select ID,LastName,FirstName,Name from Contact where CreatedDate = '+myDate; – Maddy Jan 13 '20 at 14:28
  • @Maddy The problem is DateTime.parse. You can't parse LAST_N_DAYS:3 as a DateTime. Just use the label directly. – sfdcfox Jan 13 '20 at 17:46
  • I have used below query, still I am getting 00:20:11:030 FATAL_ERROR System.TypeException: Invalid date: LAST_N_DAYS:3 error. string contactsToDelete = 'Select ID,LastName,FirstName,Name from Contact where CreatedDate =: System.Label.SetConfigurableDate'; Can we use LAST_N_DAYS:3 in custom label as date value ? It says invalid date.. Even below query is not working.. string contactsToDelete = 'Select ID,LastName,FirstName,Name from Contact where CreatedDate =: LAST_N_DAYS:3'; – Maddy Jan 13 '20 at 18:57
  • @Maddy You don't use : at the beginning. Either of the following solutions are acceptable: string contactsToDelete = 'Select ID,LastName,FirstName,Name from Contact where CreatedDate = '+System.Label.SetConfigurableDate; or string contactsToDelete = 'Select ID,LastName,FirstName,Name from Contact where CreatedDate = LAST_N_DAYS:3 – sfdcfox Jan 13 '20 at 19:10
  • Both queries are not working for me. My client has suggested to use custom label so that they will update date as configurable value instead of it to be a code change. It says below error: 09:39:30:029 FATAL_ERROR System.TypeException: Invalid date: LAST_N_DAYS:4 Below query works in workbench n fetches 4 records: SELECT CreatedDate,Description,FirstName,Id FROM Contact WHERE CreatedDate = LAST_N_DAYS:4 – Maddy Jan 14 '20 at 4:15

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.