I am writing an Batch apex where I need to query the records which are created today(System date).
I created one field name Created_Date_Formatted__c
which just gives me only date from CreateDate
field.
In my start(Database.BatchableContext BC) method, I am passing my query as string
query ='SELECT id,name FROM sObject__c WHERE Created_Date_Formatted__c = '+ String.valueOf(System.now());
I have records which has created_Date_format__c as today, When I am querying the records in query editor without any where clause it gives me all the records
But when I copy the date(red marked) and put the where clauses it does not work
I have tried putting date in different format but it doesn't work. I would really appreciate if anyone can help me. Please let me know if anything else is also required from my side. I will update the thread.
Thanks
Best Answer
Don't use a formula like this. You'll end up needing a full table scan every time you run this query, which will easily time out on larger tables. Instead, you can literally just say "today" in your query:
"TODAY" automatically selects all records created from midnight local time (according to the user's time zone). If you want records from the last 24 hours, you can calculate that, too:
When you absolutely need to filter on a formula that returns text, you still need to quote the value: