Shaun Luttin
Shaun Luttin

Reputation: 141512

Determine the number of records in a salesforce table without knowing the table's field names

I'm using the Salesforce REST API to determine the number of records in each Salesforce table. Ideally, I would be able to a query like this on each table:

SELECT COUNT(*) FROM <variable_table_name>

                  --or--

SELECT COUNT() FROM <variable_table_name>

Unfortunately, SOQL does not support * as select all. Running COUNT() without a field doesn't work either. How can we determine how many rows each table contains without knowing the name of at least one field?

My last resort is a two step process:

  1. Use the SObject Describe resource to retrieve a field name
  2. use COUNT with that field name

That means two network calls for each table rather than just one. I'd prefer not to do that.

Upvotes: 0

Views: 1105

Answers (1)

Shaun Luttin
Shaun Luttin

Reputation: 141512

The answer is to use COUNT() without any fields. Thank you Salman Syed. The problem for me was that the web response for COUNT(Id) differs from the web response for COUNT().

COUNT(Id) WebResponse Content

{
    "totalSize":1,
    "done":true,
    "records":[{
         "attributes":{"type":"AggregateResult"},
         "expr0":260
    }]
}

COUNT() WebResponse Content

{
    "totalSize":260,
    "done":true,
    "records":[]
}

When using COUNT(), I needed to look at totalSize property rather than the records.expr0 one.

Upvotes: 2

Related Questions