Find your content:

Search form

You are here

Database.getQueryLocator sorting


Does Database.getQueryLocator respect order by clause while running a batch? I am using following code

Database.getQueryLocator([select id,name from account order by industry asc])

Will this batch first process records where industry is null?

Attribution to: doga

Possible Suggestion/Solution #1

SOQL returns null values first per the salesforce documentation, unless you specify NULLS LATST in your query. To avoid confusion you can chose to expressly say NULLS FIRST if that is what you want.

Regardless of nulls first or nulls last they will be included unless they are expressly excluded by your WHERE clause.

Attribution to: ca_peterson

Possible Suggestion/Solution #2

I believe the answer is yes it does, and yes it will.

This is the results from the query in Dev console:

enter image description here

Which I appreciate doesn't answer your question, but I know that Database.getQueryLocator does certainly appear to respect sort order from having used it in the past in other projects - e.g. for sorting Opportunity Line Items by AccountID and Quantity.

Attribution to: Phil Hawthorn
This content is remixed from stackoverflow or stackexchange. Please visit

My Block Status

My Block Content