5

Is there any way we can get ListView Filter criteria using API or APEX or SOQL.

2 Answers 2

7

You can sort of do this via API and apex code, depending on what exactly you're looking for.

private static String filterCriteria(String authToken, String objType, String listView){
  system.debug('ObjType: '+objType+'-- listView: '+listView);

    Id listViewId = [SELECT id FROM ListView WHERE developerName = :listView AND sObjectType=:objType].id;
    String ep = URL.getSalesforceBaseUrl().toExternalForm() + '/services/data/v43.0/sobjects/'+objType+'/listviews/'+listViewId+'/describe';
    HttpRequest req = new HttpRequest();
    req.setMethod('GET');
    req.setHeader('Authorization','Bearer '+authToken);
    req.setEndpoint(ep);
    HttpResponse res = new Http().send(req);
    system.debug('Res body: '+res.getBody());
    queryWrapper qw = (queryWrapper)json.deserialize(res.getBody(), queryWrapper.class);

    return (qw.query.containsIgnoreCase('WHERE')?qw.query.substringAfter('WHERE'):'No filter criteria for this list view';

}

private class queryWrapper{
    String query{get;set;}

}

objType and listView are the API name of the object and the list view, respectively. Example output:

WHERE Id IN (SELECT ContactId FROM CampaignMember WHERE CampaignId = '70134000001NYuE') ORDER BY Name ASC NULLS FIRST, Id ASC NULLS FIRST

I had to use oauth to get a token because this is used in a lightning component, but if you're not using it from a custom component you can skip fetching the access token and replace:

req.setHeader('Authorization','Bearer '+authToken);

With:

req.setHeader('Authorization','Bearer '+Userinfo.getSessionId());

If filter scope is important to you, you'll want to adjust the .containsIgnoreCase and .substringAfter values to account for that. Since not all listview queries have 'USING SCOPE', I think you'd want something like:

(qw.query.containsIgnoreCase('USING SCOPE')?qw.query.substringAfter('USING SCOPE'):(qw.query.containsIgnoreCase('WHERE')?qw.query.substringAfter('WHERE'):'No filter criteria for this list view';

Also, your endpoint will clearly be different if you have not activated my domain.

5

You can access the ListViewFilter via the Metadata API ListView. Unfortunately ListView isn't currently one of the Supported Metadata Types in the Apex Metadata API.

The Apex Wrapper Salesforce Metadata API would be a good starting point as it has ListViewFilter.

Otherwise, consider voting for Provide programmatic access to ListView Filters and raising the use case in the Apex Metadata API group.

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .