4

I am trying to use the REST api to access Opportunities in SalesForce. I seem to be able to do this quite easily with a call to services/data/v26.0/sobjects/Opportunity/ and enumerating over the recentItems member of the returned object. What I'd like to be able to do is two fold, first get the list of filters that can be applied to Opportunities, eg "All Opportunities", "Active Opportunities" or whatever a user defines. The datatype description can be found here (http://na14.salesforce.com/help/doc/en/customviews.htm). Is this information accessible via the REST API (preferable) or the SOAP API or do I have to do something else to get the information?

Code sample:

var instance = "https://na14.salesforce.com/";
var accessToken = "*RemovedForSecurity*";

var url = new UriBuilder(instance + "services/data/v26.0/sobjects/Opportunity/").Uri;
using (var httpClient = new HttpClient())
{
    httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Authorization", "Bearer " + accessToken);
    using (var stream = await httpClient.GetStreamAsync(url))
    {
        var sobjects = new JsonSerializer().Deserialize<ExpandoObject>(new JsonTextReader(new StreamReader(stream)));
        foreach(var sobject in sobjects)
        {
            Console.WriteLine(sobject);
        }
    }
}
2

The answer you're looking for here found here: rest - How to retrieve list views of any salesforce object using API?. In short, the answer is, you'll have to use the Metadata API to get a full list of publicly accessible list views (those not set to "Only me"), or you'll have to construct some Apex Code that utilizes the StandardSetController to get a list of list view ID values and names (via getListViewOptions()). There's no convenient API that exposes all views available to a user directly. The most robust option, assuming you need to know the actual filters from the list view would be to use the Metadata API. You'll need a way to read and write ZIP files and XML files.

  • What field from the metadata api on an object gets me the listviews the object is a member of? – Pete Garafano Jul 23 '13 at 22:07
  • It's a sub-object of CustomObject. You should be able to access the ListView by querying the CustomObject(s) you're interested in. – sfdcfox Jul 23 '13 at 22:11
  • @sfdcfox awesome :) I was combining the same post with salesforce.com/us/developer/docs/apexcode/Content/… to test what i posted below :) – Rao Jul 23 '13 at 22:13
2

I implemented based on https://stackoverflow.com/questions/13392577/how-to-retrieve-list-views-of-any-salesforce-object-using-api

@RestResource(urlMapping='/v1.0/list_views')
global with sharing class My_RestResource {

    @HttpGet
    global static List<string> doGet() {
    List<string> selectoptions = new list<string>();
    ApexPages.StandardSetController setCtrl = new ApexPages.StandardSetController(Database.getqueryLocator([Select Id,Name FROM opportunity limit 1]));
    system.debug('*****'+ setCtrl.getListViewOptions());
    for(selectoption s: setCtrl.getListViewOptions()){
    selectoptions.add( s.getLabel());
    }
    return selectoptions ;
    }

}

enter image description here Hope this helps

  • Nice example. That's exactly what I meant about the StandardSetController. The only downside to this approach is you don't know what the filters are, only the label and an ID. This can make it hard to construct queries for those lists without using... more REST interfaces that set the filters and return the list of values. – sfdcfox Jul 23 '13 at 22:15
  • Is there a place where I can find what return type is supported by http* methods when I tried to return list<selectoption> it was throwing an Invalid type for Http* method: LIST.System.SelectOption. Is this expected or a bug? – Rao Jul 23 '13 at 22:19
  • 1
    If you can't return it directly, try making a custom class that contains the value and label attributes, then return that. public class ListViewOption { public ListViewOption(String value, String label) { this.value = value; this.label = label; } public string value, label; } then return ListViewOption[] as the data type. You can populate with for(SelectOption option:setCtrl.getListViewOptions()) { results.add(new ListViewOption(option.getValue(), option.getLabel()); } – sfdcfox Jul 25 '13 at 18:12
  • That seems to be another alternative, but why is list<selectoption> not accepted as a return type, Is there a documentation that says what the accepted types are ? – Rao Jul 25 '13 at 18:37

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.