You need to sign in to do that
Don't have an account?
Ordering SOQL by CreatedDate
Hi,
This may seem like a simple question but one I thought I'd ask to confirm all the same.
- if 2 records are inserted to an object within the same second, if ordering the results of a SOQL query by createddate will Salesforce return the correct order in which they were inserted (if miliseconds can be the differentiator).
The reason I ask is I note the createddate field is of type datetime and it states on the following
http://www.salesforce.com/us/developer/docs/api/Content/primitive_data_types.htm#i1435039
Regular dateTime fields are full timestamps with a precision of one second. therefore I'm assuming it may not order the results correclty as per when they were inserted if they were created within the same second.
Thanks in advance for any further information on this.
Here's what I did -
1. Ran this code in the Anonymous code execution window -- this inserted two accounts, with "Account # 2" inserted after "Account # 1".
2. Then, I ran this query -
To my surprise, Salesforce did not order the rows correctly - Account # 1 showed higher than Account # 2, even though they were ordered in descending order of create date.
Leads me to believe that when two records are created in SF in the same second, the ordering is not predictable.
All Answers
Here's what I did -
1. Ran this code in the Anonymous code execution window -- this inserted two accounts, with "Account # 2" inserted after "Account # 1".
2. Then, I ran this query -
To my surprise, Salesforce did not order the rows correctly - Account # 1 showed higher than Account # 2, even though they were ordered in descending order of create date.
Leads me to believe that when two records are created in SF in the same second, the ordering is not predictable.
Hi Saurabh Dhoble,
Thanks for your help. This is very useful. What I think I will need to do is to create a custom field which I provide a more detailed time stamp via and then to order by this. If I store a time in an integer or decimal field created as current miliseconds.
If I then order by this do you think this would be correct?
Miliseconds example as per
http://www.ruddwire.com/handy-code/date-to-millisecond-calculators/
Thanks for your help.
It might, but I don't like playing around with timestamp fields - it's just unreliable to base decisions on that.
You might be better off by creating an auto-number field on your object. That way, each new object will be assigned a new value, and you can figure out the order of creation simply by ordering on this field.
Thanks for the help. Has been much appreciated as always!