[SalesForce] How to query the data that has been modified in last 30 MINUTES

we have Order__c as custom object. we are trying to extract the past 30 minutes record.
where LastModifiedDate=TODAY and HOUR_IN_DAY(LastModifiedDate) > 0.5 is not working

We tried SOQL :

 SELECT count() FROM Order__c  where LastModifiedDate=TODAY and HOUR_IN_DAY(LastModifiedDate) > 0.5

HOW DO I FETCH PAST 30 minute records only using Where clause filter?

Best Answer

The easiest way might be to make the following Minutes_Since_Modified__c formula:

(NOW() - LastModifiedDate) * 24 * 60

Then to query for it:

SELECT Id FROM Account WHERE Minutes_Since_Modified__c <= 30

If you can use Apex, then use a Datetime instance.

Datetime halfHourBack = Datetime.now().addMinutes(-30);
List<MyObject__c> records = [
    SELECT count() FROM MyObject__c
    WHERE LastModifiedDate >= :halfHourBack
];

If not, you can query the literal value as long as you can construct it using ISO 8601. For example at 12:30 PM it would look like:

List<MyObject__c> records = [
    SELECT count() FROM MyObject__c
    WHERE LastModifiedDate >= 2016-08-09T12:00:00Z
];
Related Topic