QueryTask and coded domain values

7871
13
Jump to solution
02-25-2015 02:50 PM
WilliamMeredith
New Contributor

Is it possible to return coded domain values using QueryTask in the JavaScript API?  FindTask seems to do it ok, no luck with QueryTask.

Tags (1)
0 Kudos
1 Solution

Accepted Solutions
RobertScheitlin__GISP
MVP Emeritus

William,

  Query Task does not do this for you. You as the developer have to get the coded values in code.

Here is a snippet function that I use in some widgets.

     _getCodedValue: function (layer, fieldName, fieldValue, typeID) {
        var result;
        var codedValueDomain;
        if (typeID) {
          var featureType = this._getFeatureType(layer, typeID);
          if (featureType) {
            codedValueDomain = featureType.domains[fieldName];
          }
        } else {
          var field = this._getField(layer, fieldName);
          if (field) {
            codedValueDomain = field.domain;
          }
        }
        if (codedValueDomain) {
          if(codedValueDomain.type === 'codedValue'){
            for (var cv = 0; cv < codedValueDomain.codedValues.length; cv++) {
              var codedValue = codedValueDomain.codedValues[cv];
              if (fieldValue === codedValue.code) {
                result = codedValue;
                break;
              }
            }
          }
        }
        return result;
      }

,

View solution in original post

13 Replies
RobertScheitlin__GISP
MVP Emeritus

William,

  Query Task does not do this for you. You as the developer have to get the coded values in code.

Here is a snippet function that I use in some widgets.

     _getCodedValue: function (layer, fieldName, fieldValue, typeID) {
        var result;
        var codedValueDomain;
        if (typeID) {
          var featureType = this._getFeatureType(layer, typeID);
          if (featureType) {
            codedValueDomain = featureType.domains[fieldName];
          }
        } else {
          var field = this._getField(layer, fieldName);
          if (field) {
            codedValueDomain = field.domain;
          }
        }
        if (codedValueDomain) {
          if(codedValueDomain.type === 'codedValue'){
            for (var cv = 0; cv < codedValueDomain.codedValues.length; cv++) {
              var codedValue = codedValueDomain.codedValues[cv];
              if (fieldValue === codedValue.code) {
                result = codedValue;
                break;
              }
            }
          }
        }
        return result;
      }

,

WilliamMeredith
New Contributor

Thanks Robert.  Works for my needs.

Billy

0 Kudos
RoxanneWilson
New Contributor III

Robert,

I was going to try using this code snippet in my existing code.  As I've got zero coding experience and this is my first web map, I'm not sure what text do I change in your snippet and to what to make it work with my data.  I have a field called Make, that has a domain applied to it called CCTVmakes.  It's setup so 1=Make X and 2= Make Y, etc...

Thanks.

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Roxanne,

Are you developing a JS app or something in WAB?

If it is for Native JS API app then you just need to change the function signature to this:

function _getCodedValue(layer, fieldName, fieldValue, typeID) {

Then in your code use the function by providing the appropriate function parameters

var codedValue = _getCodedValue(yourlayerobject, "your field name", theCurrentFieldValue, theTypeIDifyourfieldusesasubtype);

RoxanneWilson
New Contributor III

It is for a native JS API and not WAB.  But you've lost me on the explanation part.

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Roxanne,

I am not sure I can break it down any further. The code provided is a basic JS function that you call from your code providing all the required parameters of the function.

0 Kudos
RoxanneWilson
New Contributor III

When I have this typed, I get the error that getCodedValue is undefined.

function_getCodedValue (layer, fieldname, fieldValue);

var codedValue = _getCodedValue (VideoCamera, Make);

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Roxanne,

  OK, here is the issue.

This is the whole function:

function _getCodedValue(layer, fieldName, fieldValue, typeID) {

        var result;

        var codedValueDomain;

        if (typeID) {

          var featureType = this._getFeatureType(layer, typeID);

          if (featureType) {

            codedValueDomain = featureType.domains[fieldName];

          }

        } else {

          var field = this._getField(layer, fieldName);

          if (field) {

            codedValueDomain = field.domain;

          }

        }

        if (codedValueDomain) {

          if(codedValueDomain.type === 'codedValue'){

            for (var cv = 0; cv < codedValueDomain.codedValues.length; cv++) {

              var codedValue = codedValueDomain.codedValues[cv];

              if (fieldValue === codedValue.code) {

                result = codedValue;

                break;

              }

            }

          }

        }

        return result;

      }

and this is how you call it:

var codedValue = _getCodedValue (VideoCamera, "The field name as a string", "this is the value you have for that fields attribute");

0 Kudos
RoxanneWilson
New Contributor III

Could I simplify that and just create a function that says something like if Make=1, return Axis.  else if make =2, return Vicon?

I attempted, but my syntax isn't right.  And I've never dealt with function or if/else statements.

function MakeValue

  if (Make =1){

  return "Axis";

} else {

  if (Make =2){

  return "Vicon";

};

0 Kudos