JS API 4.16 Coded Domain Values with QueryTask

3827
18
Jump to solution
08-18-2020 12:27 PM
Claire_Peterson
Occasional Contributor

There are a lot of threads that talk about returning the coded domain values descriptions while using a queryTask but from the few examples I have seen using the 4.x API, I can't get anything to work. My code is below if anyone could help me. I haven't really found any examples along the same lines as to what I am trying to do.

//build query task
    var queryTask = new QueryTask({
      url: "https://services.arcgis.com/uHAHKfH1Z5ye1Oe0/arcgis/rest/services/misin_treatments_aquatic/FeatureServer/0"
      });

    //build query filter
    var query = new Query();
    query.returnGeometry = true;
    query.outSpatialReference = view.spatialReference; //added for testing
    query.outFields = ["*"];


    //pass the url parameters.
    var urlObject = urlUtils.urlToObject(window.location.href);
    if (urlObject.query)
    {
      if (urlObject.query.id)
      { OID = urlObject.query.id; }

    //set query based on the parameters

    var treatmentid = "OBJECTID = '" + OID + "'";
    query.where = treatmentid;
    }

  // execute query, place graphic on map and zoom to graphic
      queryTask.execute(query).then(function(result){
         console.log(result.features);
         view.graphics.removeAll();
         view.popup.close();

         var resultFeatures = result.features;
         resultFeatures.map(function(gra){
           gra.symbol = sym;
           view.graphics.add(gra);
           gra.popupTemplate = popupTemplate;

           // Provide graphic to a new instance of a Feature widget
         const feature = new Feature({
           container: "attributes",
           graphic: gra,
           map: view.map,
           spatialReference: view.spatialReference
         });

         var AOI = result.features;
         view.goTo(AOI);
       });
     });
0 Kudos
18 Replies
Claire_Peterson
Occasional Contributor

Aaron Booterbaugh‌ - still doesn't work 

using this service URL works just fine: https://services.arcgis.com/uHAHKfH1Z5ye1Oe0/ArcGIS/rest/services/MISIN_Browse_Data/FeatureServer/0 

but when I change it to this URL and modify the fieldNameTest variable , it does not:

https://services.arcgis.com/uHAHKfH1Z5ye1Oe0/arcgis/rest/services/misin_treatments_aquatic/FeatureSe... 

0 Kudos
AaronBooterbaugh
Occasional Contributor

Claire,

A subtype code value is not a string but an integer. Remove the quotes...

function onClick(event) {
        var fieldNameTest = "OB_DENSITY";
        var fieldDomainCode = "1";
        var testReturn = GetCodedValueAndAlias(featureLayer, fieldNameTest, fieldDomainCode);
        console.log("Field alias for field name " + fieldNameTest + " = " + testReturn[0]);
        console.log("Field daomain value for code value " + fieldDomainCode + " = " + testReturn[1]);
        fieldNameTest = "KINGDOM";
        fieldSubTypeCode = 1;
        testReturn = GetSubtypeCodeValuesAndAlias(featureLayer, fieldNameTest, fieldSubTypeCode);
        console.log("Field alias for field name " + fieldNameTest + " = " + testReturn[0]);
        console.log("Field subtype value for code value " + fieldSubTypeCode + " = " + testReturn[1]);
      }
Claire_Peterson
Occasional Contributor

Aaron Booterbaugh‌  That worked! Thank you so, so much.

Now I am wondering, seeing that the domain code is hard coded - how would it handle returning the domain value based on the feature that the query task brings back? It would change depending on which feature is selected.

0 Kudos
AaronBooterbaugh
Occasional Contributor

Claire,

I would not be able to say. I would need to understand the goal of what you are trying to do. Are you trying to change the values in a popup that shows a single feature? Or are you trying to change the values of an entire table view of values? The sample I gave just works on fields that you know have domains or Subtype Codes. It would fail on date type fields if passed in.

0 Kudos
Claire_Peterson
Occasional Contributor

Aaron Booterbaugh‌ I modified my last reply because I was able to get the function to execute as soon as the map loads.

The primary goal is we have the web app, a user can click on a feature and then clicks on a link within the feature's popup that would bring them to this map page - it only shows the selected feature and a few selected attributes in a popuptemplate off to the side. There are two fields that use domain values and those would change based on the feature that is returned by the queryTask. Below is an image of the finished map page and Chemical Used & Weather Conditions are the two fields that have coded domain values (chemical used as 40+ different domains).

0 Kudos
AaronBooterbaugh
Occasional Contributor

I would change the values in the result.features. Doing this will take some effort to understand how to do it. The result.features will have the all the field names and values. In the case you sent you would pass in the non alias name of the field for Chemical Used then the value 5 to get the real value then change the result.feature itself to reflect the new data you want. I hope that makes sense. If you use Chrome or Edge-C browser you can CTRL+SHIFT+I and go to the Source Tab to see the code. You can place stops in the code and then look at the variables that you then can change them. The result.features is a variable that you can look at and see how to change it. Below is an image of my browser I use Microsoft Edge Chromium version. Chrome can be used the same way.

0 Kudos
Claire_Peterson
Occasional Contributor

Aaron Booterbaugh‌ - thank you, I will take a look at this a bit more and see if I can figure out what to do. I appreciate the screenshot and all of your help with this.

Claire_Peterson
Occasional Contributor

Aaron Booterbaugh‌ - thanks again for your help with this. I ended up modifying my code a bit and changing it to a query on a feature layer versus a queryTask. This change returned the domain values in my popup!

0 Kudos
AaronBooterbaugh
Occasional Contributor

Cool

0 Kudos