returnDistinctValues does not always work

4902
10
12-22-2014 01:04 PM
EdwardSohn2
New Contributor III

Using:

var queryTask = new QueryTask(layer.layerObject.url + "?returnDistinctValues=true");

But sometimes this works and sometimes it does not.

Are there any server settings for each layer (featurelayer) that needs to be specified precisely to make the returnDistinctValues=true work?

Using v. 3.10.

0 Kudos
10 Replies
ReneRubalcava
Frequent Contributor

Looking at the docs, you need to specify the field for distinctValues in the outFields parameter and need server 10.11 minimum. supportsAdvancedQueries of the service also needs to be true, but I think true is the default setting when a service is published.

Does it work consistently when you perform the query via the REST HTML page of the sevice?

0 Kudos
EdwardSohn2
New Contributor III

Seems like the layers for which it does not work are feature collections and those for which it works are feature services... (at least for two of the layers for which it did not work...)  Do they really need to be feature services?  Is it possible to make it work for feature collections?

0 Kudos
JohnGrayson
Esri Regular Contributor

From the API help; when dealing with FeatureCollection based FeatureLayers: Does not support queries that need to be performed on the server, e.g. queries with a where clause or non-extent based spatial queries.

FeatureLayer | API Reference | ArcGIS API for JavaScript

0 Kudos
EdwardSohn2
New Contributor III

The layers apparently are both feature services and they both have supportsAdvancedQueries set to true.  But the returnDistinctValues=true are still not working...  (and yes the field is being sent as outFields parameter in the queries)...

0 Kudos
JohnGrayson
Esri Regular Contributor

Edward, do you have a jsfiddle or jsbin showing the issue?  If not, do you have a rest url endpoint and query parameters that we can try it out? 

EdwardSohn2
New Contributor III

Oh btw, the server url seem to be different:

Those that work are of type:

"https://utility.arcgis.com/usrsvcs/...?returnDistinctValues=true"

Those that don't work all seem to be of type:

"https://services2.arcgis.com/...?returnDistinctValues=true"

i.e. starting with https://services2.arcgis.com/

Here is one example which doesn't seem to work:

"https://services2.arcgis.com/OzJIHUWI4lRJCw0e/arcgis/rest/services/BusStop/FeatureServer/0?returnDis..."

0 Kudos
JohnGrayson
Esri Regular Contributor

Edward,

returnDistinctValues is a property on the JS API Query object, not a parameter of the url of the service, so please don't include it when creating the Query instance. The services you mention are secure services so make sure you include a reference to esri/IdentityManager so the appropriate credentials are obtained and used by the JS API before you make the request.

It would help if you shared some of the JS code so we can figure out what is going on.

John

0 Kudos
EdwardSohn2
New Contributor III

Currently we are using 3.10. returnDistinctValues is not defined until 3.11.  Sending as query parameter does work... but only for utility.arcgis.com/usrsvcs...

doesn't work for services2.arcgis.com urls...

I tried upgrading to 3.11 and using

                var queryTask = new QueryTask(layer.layerObject.url);

                var esriQuery = new Query();

                esriQuery.returnDistinctValues = true;

                esriQuery.outFields = [attribute];

                esriQuery.where = typeof layer.layerObject.defaultDefinitionExpression === "undefined" || layer.layerObject.defaultDefinitionExpression === null ? "1=1" : layer.layerObject.defaultDefinitionExpression;

Got same results:

Worked for utility.arcgis.com/usrsvcs...

doesn't work for services2.arcgis.com urls...

0 Kudos
ReneRubalcava
Frequent Contributor

If you are using 3.10, you'll need to hack it a little bit using setRequestPreCallback as described in this thread.

0 Kudos