[SalesForce] How to compare Datetime field with SystemTime.now() in SOQL query

I have a 2 datetime field(Start_Time__c and End_Time__c) and I need to compare them with current time.
I am writing the following SOQL Query :

Select id from objTime where Start_Time__c >= SystemTime.now() AND End_Time__c

But it is unable to fetch to the comparison of date time, do we have any solution for resolving the same ?

Best Answer

You can use static or dynamic SOQL here. It would be as below

Static soql:

DateTime currentTime = System.now();
List<Account> accounts  = [SELECT Id From Acccount WHERE Start_Time__c >= :currentTime AND End_Time__c <= TODAY ];

Dynamic soql:

DateTime currentTime = System.now();

List<Account> accounts = Database.query('SELECT Id From Acccount WHERE Start_Time__c >= :currentTime AND End_Time__c <= TODAY ');

I tried this on account , you can use this on any sobject. As a common practice if this helps mark this as solution.

Related Topic