function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Sohel AminSohel Amin 

Count contact records within a listView

Hi Guys,

I'm using salesforce api to get all contacts from a list view.
I can also use limit & offset aswell but my problem is I could not found a way to count all contacts within a list view.

I use the following endpoint services/data/v36.0/sobjects/Contact/listviews/ 

Is there any way to count the result so I can pass the limit & offset for next query ?

Thanks
Sohel Amin
sandhya reddy 10sandhya reddy 10
Hi Sohel,

you can use aggregrate functions for this as below code
 
AggregateResult[] arList = [select count(id) from Contact];
System.debug(LoggingLevel.INFO, arList[0].get('expr0'));

Please see below link to have more information

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_SOQL_agg_fns.htm

Please let us know if this helps you.

Thanks and Regards
sandhya
Sohel AminSohel Amin
Thanks for the reply Sandhya

Let me clear more, I'm trying to get all contacts from a list view I can do it but I can't count the result so I can do paging query.
https://ap2.salesforce.com/services/data/v36.0/sobjects/Contact/listviews/<LISTVIEWID>/results

If you know the SOQL for that then please let me know.
sandhya reddy 10sandhya reddy 10
Hi Sohel,

After i do some google search i found same thread answered by people.please see this you can get some idea

https://success.salesforce.com/answers?id=90630000000DJDRAA4

Please let us know if this helps you.
Thanks and Regards
sandhya
maninder singh 50maninder singh 50
to get the total records in list view we need to make 2 calls.
1. Get the query : Use 
/services/data/v46.0/sobjects/<objectapiname>/listviews/<listviewid>/describe
to get the query used to filter the list. Extract the WHERE clause from this.

2. Run the query to get count of records: Create a SOQL by concatenating , 'SELECT COUNT(Id) FROM ' +<WHERE clause from Step 1>
/services/data/v46.0/query?q=<SOQL created>