0

I am beginner in LWC. I just created a simple LWC component which is retrieving records from Contact object. I have written a method in apex class which helps to get 15 records from Contact object.

Then through imperative way of calling in LWC, I am trying to print those contact records.

I also created a new contact with all the fields I mentioned in SOQL query. But this contact is not getting printed.Some random 15 records of contact object is getting displayed.

My question is whether SOQL query returns contact records randomly or in the order of recently created?

0

1 Answer 1

1

In your case, it's actually neither.

If you don't have any filters in your query (i.e. stuff in the WHERE clause, like WHERE CreatedBy.Name = 'Some User'), then I believe the results you'll get are non-deterministic.

This is different from being random, because there is some process by which the records are selected (selecting at random doesn't strike me as a fast operation, which could be a problem at Salesforce's scale). We just aren't privy to the details.

Further, while the order in which the records are returned are also non-deterministic (unless you use ORDER BY), I do recall a change several years ago that made it so that if you repeat a query multiple times (in a single transaction) you get the results in the same order each time.

If you want a particular set of results, you need to use filters and/or ORDER BY. Adding ORDER BY CreatedDate DESC would get you the records in most-recently created order (the DESC there is necessary, because the default sort order is ASC (ascending). ASC would give you the oldest records instead of the newest).

1
  • Thanks Derek for answering my question. :)
    – Aishwarya
    Commented Nov 5, 2022 at 18:16

Not the answer you're looking for? Browse other questions tagged .