Play Games

Search This Blog

Wednesday, July 26, 2017

How to fetch records order by a field where empty(null) values to be displayed last using SOQL in Salesforce

Problem: I want to fetch all contacts and I need to display contacts which are associated with Account first and then remaining last.
Solution: We can use NULLS FIRST or NULLS LAST keywords along with order by clause in soql while retrieving.
Example:
When you run the below query in Developer Console
SELECT Name,Account.id FROM Contact ORDER BY Account.name ASC NULLS LAST


When you run the below query in Developer Console
SELECT Name,Account.id FROM Contact ORDER BY Account.name ASC NULLS FIRST


No comments:

Post a Comment