Bhushan Lodha
Bhushan Lodha

Reputation: 6862

SOQL query to retrieve last row of table

What is the SOQL query to retrieve last row of table?

Upvotes: 0

Views: 8684

Answers (2)

PartOfTheOhana
PartOfTheOhana

Reputation: 685

I don't think there's a concept of last in databases. I think a table is a set of records, and as a result the records are unique (hello primary key!) and there is no CONSISTENT ordering like an array (or a salesforce list)

With that warning, There's a better way to do this. do a count() to get the number of records on the object. You'd do it like this: rn = SELECT COUNT(Id) FROM Account

then you want that nth record. to select it you use: SELECT * FROM Account Where rn = ..... <- how to indicate the nth row, i'm not sure how to do in soql.

Upvotes: 0

Matt K
Matt K

Reputation: 7337

SOQL is a bit different than SQL. TOP 1 is not valid in SOQL (Salesforce Object Query Language), you would need to use LIMIT 1.

Check the documentation: http://www.salesforce.com/us/developer/docs/api/index_CSH.htm#sforce_api_calls_soql_select.htm

You could try this, too: SELECT Id From ObjectName__c ORDER BY Id DESC LIMIT 1

Upvotes: 10

Related Questions