I want to retrieve the first x records in my dataset. Logically, I created an Auto Number field, but it's acting like a string. Example:

[SELECT id__c FROM Person__c WHERE id__c < '4']

incorrectly returns:

{1, 10, 2}


{1, 2, 3}

I have made a squirrelly work-around using IN ('1', '2', ..., x) and use a for-loop to build the list. Is there a best practice or at cleaner method?

(Note: Tried ORDER BY + LIMIT x, but ordering is obeying string sorting rules. Considering adding a trigger to convert the AutoNumber to an int or manage my own auto number as Integer)

1 Answer 1


Auto-number fields are strings, as they can contain letters, too. To make things easier, consider changing the "Display" to something like 0000000000, which will generate numbers like 0000000001, 0000000002, 0000000003, etc. You can use between one and ten 0s, so choose whichever you think will be most appropriate. Otherwise, if you do need them to be numbers, you could indeed do this in a trigger or even a flow. Or, as a more direct route, you can probably just ORDER BY CreatedDate ASC to get the first of the created records. This is also convenient because date fields are also indexed.

You must log in to answer this question.

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