You need to sign in to do that
Don't have an account?
Chris Feldhacker II
Empty QueryLocator?
What is the best way to return a QueryLocator object that returns 0 records?
I have a Batch class that normally need to return >50K records, so I'm using the start() method that returns a QueryLocator. However, if certain preconditions are not met, then the Batch class should not process any records -- i.e., I want the start method to return an empty result.
Options (that don't work):
1) Having start return null doesn't work as Apex throws an UnexpectedException.
2) Database.getQueryLocator(new List<SObject>()) doesn't work, as Apex requires the parameter to be a SOQL query.
3) Constructing a Database.QueryLocator object directly seems impossible.
Options (that work):
4) Execute Database.getQueryLocator() with a "bogus" SOQL query that is guaranteed to return 0 results (like, WHERE lastmodified date is in the future or some non-nullable field is null).
5) Other ideas?
I have a Batch class that normally need to return >50K records, so I'm using the start() method that returns a QueryLocator. However, if certain preconditions are not met, then the Batch class should not process any records -- i.e., I want the start method to return an empty result.
Options (that don't work):
1) Having start return null doesn't work as Apex throws an UnexpectedException.
2) Database.getQueryLocator(new List<SObject>()) doesn't work, as Apex requires the parameter to be a SOQL query.
3) Constructing a Database.QueryLocator object directly seems impossible.
Options (that work):
4) Execute Database.getQueryLocator() with a "bogus" SOQL query that is guaranteed to return 0 results (like, WHERE lastmodified date is in the future or some non-nullable field is null).
5) Other ideas?
- System.debug('TestBatch.start - committing suicide');
- System.abortJob(context.getJobId());
- System.debug('TestBatch.start - failed');
Line 3 is still executed even though the job is (supposed to be) aborted.1. Use SELECT Id FROM Account WHERE Id = null so that the result will be zero instead of returning null.
or
2. Create a schedulable class, do the precondition in it and then call the batch class from the schedulable class.
--
Magulan Duraipandian
www.infallibletechie.com
SELECT Id FROM Account LIMIT 0
Works like a charm!