0

I would like to access the records related to contact predefined list views (like birthdays this week, contacts added this week), and custom views created by the user, using REST API.

The SOQL query I tried is

https://ap.salesforce.com/services/data/v43.0/sobjects/Contact/listviews/{id}/results

This does provide the results but it doesn't give the value of the fields I want.

I want to be able to do something like

select firstname,email from Contact

but fetch the records associated with the custom view selected by the user.

Edit:

I created a few custom views and tried getting their query using describe, as suggested by @David Reed. But the query I'm getting for all user created custom views are the same.

SELECT Name, Account.Name, Phone, Email, Title, Owner.Alias, Id, CreatedDate, LastModifiedDate, SystemModstamp, Account.Id, AccountId, Owner.Id, OwnerId FROM Contact USING SCOPE mine ORDER BY Name ASC NULLS FIRST, Id ASC NULLS FIRST
1

Multi-Call Approach

If you would like to use SOQL rather than SOSL, you can pull the results of the list view using the Results endpoint

/services/data/v43.0/sobjects/Contact/listviews/{id}/results

and then extract the object Ids from the results. The Ids are returned even if the Id column isn't part of the list view.

"columns": [ { "fieldNameOrPath" : "Id", "value" : "0033600001a28XXXXX" } ]

Once you've got a collection of Ids, you can make another API call to perform whatever SOQL query you wish, adding on a WHERE clause referencing the collection of Ids you've accumulated.

Note that when running queries via the API the limit of 4,000 characters in the WHERE clause applies, so you might have to batch your Ids and make multiple queries to stay under the limit. Depending on exactly how you format your query, the maximum records you can pull by Id is in the low 200 range.

SOQL Mutation Approach

You can get the actual SOQL for a list view from its Describe endpoint:

/services/data/v43.0/sobjects/Contact/listviews/{id}/describe

It's in the "query" key. The presence of a "soqlCompatible" key on the response body for getting all available List Views suggests there are some that don't have SOQL associated with them, but I don't know which ones those might be - all the Contact list views in my developer edition have SOQL.

For example, a "My Contacts" List View with a filter for FirstName starts with "K" returns this SOQL:

SELECT Name, Account.Name, Phone, Email, Title, Owner.Alias, Id, CreatedDate, LastModifiedDate, SystemModstamp, Account.Id, AccountId, Account.RecordTypeId, Owner.Id, OwnerId 
FROM Contact 
USING SCOPE mine 
WHERE FirstName like 'K%' 
ORDER BY Name ASC NULLS FIRST, Id ASC NULLS FIRST

Once you've got the query, you can strip out or extend its SELECT clause to add the fields that you want in your results and execute it normally.

  • Thanks for the reply. I checked the query key and it looks like it might work! I'll update after I try it out. And also, can the same list of records be achieved using SOSL? I was sure it was only for search. – Dareka Dec 12 '18 at 6:21
  • SOSL is for full-text search. It just happens to have a clause limiting to a list view. – David Reed Dec 12 '18 at 12:17
  • Hey! I was trying out the mutation approach you provided, and I noticed that the query it provides doesn't have listview anywhere in the criteria. It seems to return all the leads or contacts. – Dareka Dec 14 '18 at 7:33
  • I checked it with custom views. The queries appear to be the same. – Dareka Dec 14 '18 at 7:42
  • @Dareka Would you mind putting an example or two in the body of your question? I created a custom List View and was able to get unique SOQL back from the API. – David Reed Dec 14 '18 at 12:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.