samirgambhir

Query problem

Discussion created by samirgambhir on Apr 4, 2013
Latest reply on Apr 5, 2013 by ottarv-geodata-no-esridist
Hi all,
I am running a queryTask and queryFeatures on a State and a sub-State feature layers respectively. The function is supposed to select features from the sub-State featureLayer which are above (or below) the State average (the input from the State feature Layer). I am being able to run the query on the sub-State layer, but I am not able to retrieve the State value. I'll appreciate it if someone can look into it. Here is the relevant code:
function queryParam(){
    var stateAvg;
    myURL = "subStateURL";
    queryTask = new esri.tasks.QueryTask(myURL);
    query = new esri.tasks.Query();
    query.returnGeometry = true;
    query.outFields = ["*"];
    query.where = "1=1";

    distFeatureLayer.setDefinitionExpression(someString);
    distFeatureLayer.queryFeatures(query, function(featureSet) {
        stateAvg=returnStateAverage(stateGeography, stateIndicator);
        for ( i = 0; i < featureSet.features.length; i++) {
            var indValue = featureSet.features[i].attributes[indicator];//'indicator' same as 'stateIndicator'
            if (userSelectedCriteria == "Above") {//user selects the criteria using a combobox
  if (indValue > stateAvg) {
                   selDist.push(featureSet.features[i].attributes.Dist_name);
      selStAbbr.push(featureSet.features[i].attributes.St_abbr);
  }
     } else if (userSelectedCriteria == "Below") {
  if (indValue < stateAvg) {
     selDist.push(featureSet.features[i].attributes.Dist_name);
     selStAbbr.push(featureSet.features[i].attributes.St_abbr);
  }
     }
        }
        strDist = "(Dist_name = '" + selDist[0] + "' AND St_abbr = '" + selStAbbr[0] + "')";
 if (selDist.length > 1) {
     for ( i = 1; i < selDist.length; i++) {
  strDist += "OR (Dist_name = '" + selDist[i] + "' AND St_abbr = '" + selStAbbr[i] + "')";
     }
 }
 distFeatureLayer.setDefinitionExpression(strDist);
        distFeatureLayer.selectFeatures(query, esri.layers.FeatureLayer.SELECTION_NEW);
 distFeatureLayer.setRenderer(renderer);
 distFeatureLayer.refresh();
 hide("loaderExecute");
    });
}

function returnStateAverage(geography, indicator){
    var stateVal;
    var myURL = "stateURL";
    queryTask = new esri.tasks.QueryTask(myURL);
    query = new esri.tasks.Query();
    query.returnGeometry = false;
    query.outFields = ["*"];
    query.where = "State_name='" + geography + "'";
    queryTask.execute(query, function(fSet) {
        var features = fSet.features;
 dojo.forEach(features, function(feature) {
     stateVal = feature.attributes[indicator];
     return stateVal;
 });
    });
}

Outcomes