3

I am running an hourly batch. This batch should pick all the records of an object say abc__c whose particular field Eligible__c is set to true in past one hour. I am trying to fetch these records by having a field maintained in the object that gets updated with the time stamp when the field is set to true using workflow field update. I am using the following query in my batch apex start() method.

DateTime curTime = datetime.now();
String queryToProcess = 'select id, name from abc__c where Eligible__c = true and MarkedEligibleTime__c <= :curTime';

Here I am comparing the field MarkedEligibleTime__c (that maintains time stamp) against the current time when batch is running and if it is less than the current time then I am picking the corresponding records. After batch is processed will ensure that MarkedEligibleTime__c is reset so that it doesn't get picked in next hourly batch. Is it the right approach to do or is there a better approach to achieve this ?

1

If you can wait until the Winter '14 rollout it looks like there is a new method which does might be helpful.

Database.getUpdated(String sobjectType, Datetime startDate, Datetime endDate)

Returns the list of individual records that have been updated for an sObject type within the specified start and end dates and times. The sObjectType argument is the sObject type name for which to get the updated records, such as account or merchandise__c. The startDate argument is the start date and time of the updated records time window. The endDate argument is the end date and time of the updated records time window. Results are returned for no more than 30 days previous to the day the call is executed.

---- edit ----

Having re-read your question, whether this is going to be useful depends on volumes of data I think. It would let you retrieve every record updated in the last hour without you needing to maintain a timestamp field. But you would still need to loop through everything to find the ones where just your field has changed, which could be a problem depending on your data volumes. So probably your approach is better, and I just got carried away with the new Database features!

  • 1
    That's an awesome feature to have, though. It's been in the API for ages. – sfdcfox Sep 11 '13 at 11:35

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.