Is it possible to return coded domain values using QueryTask in the JavaScript API? FindTask seems to do it ok, no luck with QueryTask.
Solved! Go to Solution.
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; }
,
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; }
,
Thanks Robert. Works for my needs.
Billy
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.
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);
It is for a native JS API and not WAB. But you've lost me on the explanation part.
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.
When I have this typed, I get the error that getCodedValue is undefined.
function_getCodedValue (layer, fieldname, fieldValue);
var codedValue = _getCodedValue (VideoCamera, Make);
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");
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";
};