returnDistinctValues not working in my query

4056
14
08-26-2015 09:33 AM
TracySchloss
Frequent Contributor

I have a queryTask based on the URL of my featureLayer.  This layer has 508 features in this layer.  My maximum record count on my service is much higher than that, to accommodate another layer with more features in it.  I'm using AGS 10.2.2 and Advanced Query Support says 'true'.

I'm using this queryTask as the source for a dropdown list, so I need to get just the distinct values from my AgencyAcronym field.  My queryTask executes, but instead of the 18 distinct values for this field, I'm still getting 508 features returned.  I'm having to run these through another function to remove the duplicates and I don't think I should have to do this.  I've tried a where clause of '1=1', because I want to check all records, but that doesn't make a difference, I still get all 508 features.

I have my queryTask set up as:

 //populates the pick list searching state facilities by agency       
     populateBuildingList: function(){
       app.buildingAgencyList.length = 0;
       var queryTask2 = new QueryTask(app.buildingLayer.url);
       var query = new Query();
       query.where = "AgencyAcronym is not null";
       query.outFields = ["AgencyAcronym"];
       query.orderByFields = ["AgencyAcronym"];
       query.returnGeometry = false;
       query.returnDistinctValues = true;
         on(queryTask2, 'error', function(err){
           console.log('error populating building list: ' + err.error)
         });
       queryTask2.execute(query, lang.hitch(this, updateBGridHandler));
      function updateBGridHandler(results){
       console.log('results of updateBGridHandler :' + results.features.length + " records" );  //all 508 records returned. 
        arrayUtils.forEach(results.features, function(feature){
          var agencyName = feature.attributes.AgencyAcronym;
           agencyName = agencyName.replace(/^\s+|\s+$/g,'');
          app.buildingAgencyList.push(agencyName);
        });
        app.buildingAgencyList.sort();
        var sortedList = common.sortAndRemoveDuplicates(app.buildingAgencyList);  //a function I'm having to use because the values aren't unique
        var data = arrayUtils.map(sortedList, function(itm, idx){
          return {
            "id": idx,
            "name": itm,
            "value": itm
          }
        })
        var currentMemory = new Memory({
          data: data,
          idProperty: 'id'
        });
        app.agencyDropdown.set("store", currentMemory);  //a grid created earlier
        app.agencyDropdown.sort('name');
       } 
      }  
0 Kudos
14 Replies
TracySchloss
Frequent Contributor

I believe this is a bug.  When I change my code to use a non-secured service, both orderByFields and returnDistictValues works just fine.  With secure services, running through a proxy etc, neither one of these work.

Kelly Hutchinsate this?

0 Kudos
KellyHutchins
Esri Frequent Contributor

Tracy Schloss​ it looks like you are still using the workaround of setPreRequestCallback. That workaround is no longer required at version 3.11 of the api and later. Can you try using the query's returnDistinctValues and see if that resolves the issue.

Query | API Reference | ArcGIS API for JavaScript

0 Kudos
TracySchloss
Frequent Contributor

I had a version that didn't have it, and that didn't help.

There must be something in particular about this service.  I tried another service that I've had for a while, making a secure version of it.  I was able to generate a list of distinct values without any problem from it.  It is based on a SQL server geodatabase, where my initial service is a file geodatabase.  I haven't read any limitations about that, except from an older thread that may or may not have any bearing.

My problem service doesn't return distinct values through the REST endpoint either.  Of course what you can do with REST and what's in the API aren't always identical.

In the end, I just went back to my function that removed duplicates, so at least I can move forward.

0 Kudos
KellyHutchins
Esri Frequent Contributor

Tracy perhaps the 'problem' service doesn't support querying using things like Distinct. You can test this by checking the layer's advancedQueryCapabilities property.

0 Kudos
TracySchloss
Frequent Contributor

I'd just been going by what I could see from the REST service, which said

Supports Advanced Queries: true

I tried var test = app.buildingLayer.advancedQueryCapabilities; and looked at that object.  It is

0 Kudos