Select to view content in your preferred language

Is it possible to pass dataset name from variable?

823
9
Jump to solution
07-18-2023 03:26 PM
RhettZufelt
MVP Notable Contributor

I am trying to use a variable as the dataset name in FeatureSetByName, but not having any luck.

Like so:

 

 var table = 'TestTableName'   
    
var tbl = FeatureSetByName($datastore,table, ['Year','AsBuilt'], False)   

 

But, arcade doesn't like it.  I've tried several ways with no luck.  Hoping someone here know if this is possible, and how to do it.

Thanks,

R_

0 Kudos
3 Solutions

Accepted Solutions
MikeMillerGIS
Esri Frequent Contributor

That function requires a string literal, as ArcGIS needs to monikerize the class name when publishing to a feature service.  

I typically use a switchyard function like the below:

function get_features_switch_yard(class_name, fields, include_geometry) {
    var class_name = Split(class_name, '.')[-1];
    var feature_set = null;
    if (class_name == 'PipelineDevice') {
        feature_set = FeatureSetByName($datastore, 'PipelineDevice', fields, include_geometry);
    } else if (class_name == 'PipelineJunction') {
        feature_set = FeatureSetByName($datastore, 'PipelineJunction', fields, include_geometry);
    } else if (class_name == 'PipelineAssembly') {
        feature_set = FeatureSetByName($datastore, 'PipelineAssembly', fields, include_geometry);
    } else if (class_name == 'PipelineLine') {
        feature_set = FeatureSetByName($datastore, 'PipelineLine', fields, include_geometry);
    } else if (class_name == 'StructureJunction') {
        feature_set = FeatureSetByName($datastore, 'StructureJunction', fields, include_geometry);
    } else if (class_name == 'StructureLine') {
        feature_set = FeatureSetByName($datastore, 'StructureLine', fields, include_geometry);
    } else if (class_name == 'StructureBoundary') {
        feature_set = FeatureSetByName($datastore, 'StructureBoundary', fields, include_geometry);
    } else if (class_name == 'Rules') {
        feature_set = FeatureSetByName($datastore, 'UN_5_Rules', fields, false);
    } else {
        feature_set = FeatureSetByName($datastore, 'StructureBoundary', fields, include_geometry);
    }
    return feature_set;
}

View solution in original post

0 Kudos
Jake_S
by Esri Contributor
Esri Contributor

@RhettZufelt what you're looking for is a build a dynamic query. In this post Building a dynamically FeatureSetByName query Hussein points out:


"FeatureSetByName does not support dynamic class names. The name of the class has to be available during static analysis of the script so we can build out the relations in the catalog. (E.g. if you copy Class A which has an attribute rule that reads from Class B we also copy Class B too)"

Not the best resolution but its confirmed by the Esri team

~ Jake

View solution in original post

0 Kudos
MikeMillerGIS
Esri Frequent Contributor

You can make this work, just need to build it like this

function get_fs(cls_name)
{
  return Decode(cls_name,
      "sdInlet",FeatureSetByName($datastore, "sdInlet", ['*'], true),
      "sdOutlet",FeatureSetByName($datastore, "sdOutlet", ['*'], true),
      "sdCleanOut",FeatureSetByName($datastore, "sdCleanOut", ['*'], true),
      "sdManhole",FeatureSetByName($datastore, "sdManhole", ['*'], true),
      null)
}
var fclist = ['sdInlet','sdOutlet','sdCleanOut',"sdManhole"];
for (var p in fclist){
    var point_fs = get_fs(fclist[p]);
    //Do stuff with it
}

 

View solution in original post

9 Replies
alex_mapintel
New Contributor III

I wonder if you need to wrap it as text using Text() :

var table = 'TestTableName'   
    
var tbl = FeatureSetByName($datastore,Text(table), ['Year','AsBuilt'], False) 

 

0 Kudos
RhettZufelt
MVP Notable Contributor

Thanks for the suggestion, I had not tried that.  But no, it validates, but give the ever so informative 999999 error when you try to save.

R_

0 Kudos
MikeMillerGIS
Esri Frequent Contributor

That function requires a string literal, as ArcGIS needs to monikerize the class name when publishing to a feature service.  

I typically use a switchyard function like the below:

function get_features_switch_yard(class_name, fields, include_geometry) {
    var class_name = Split(class_name, '.')[-1];
    var feature_set = null;
    if (class_name == 'PipelineDevice') {
        feature_set = FeatureSetByName($datastore, 'PipelineDevice', fields, include_geometry);
    } else if (class_name == 'PipelineJunction') {
        feature_set = FeatureSetByName($datastore, 'PipelineJunction', fields, include_geometry);
    } else if (class_name == 'PipelineAssembly') {
        feature_set = FeatureSetByName($datastore, 'PipelineAssembly', fields, include_geometry);
    } else if (class_name == 'PipelineLine') {
        feature_set = FeatureSetByName($datastore, 'PipelineLine', fields, include_geometry);
    } else if (class_name == 'StructureJunction') {
        feature_set = FeatureSetByName($datastore, 'StructureJunction', fields, include_geometry);
    } else if (class_name == 'StructureLine') {
        feature_set = FeatureSetByName($datastore, 'StructureLine', fields, include_geometry);
    } else if (class_name == 'StructureBoundary') {
        feature_set = FeatureSetByName($datastore, 'StructureBoundary', fields, include_geometry);
    } else if (class_name == 'Rules') {
        feature_set = FeatureSetByName($datastore, 'UN_5_Rules', fields, false);
    } else {
        feature_set = FeatureSetByName($datastore, 'StructureBoundary', fields, include_geometry);
    }
    return feature_set;
}
0 Kudos
RhettZufelt
MVP Notable Contributor

Thanks Mike,

This is very similar to the way I worked it out as well, except didn't wrap it in a function.

Want to choose a different 'input table' based on the user logged into Pro.  Was hoping there was a one liner to do this instead of a line for each 'option'.

R_

0 Kudos
RhettZufelt
MVP Notable Contributor

Is this still the case?  I wonder as in the AR editor in Pro:

 

var pfc = 'sdManhole'
var point_fs = FeatureSetByName($datastore, pfc , fields, true);

 

Is working perfectly, returning the values it should, BUT, will not let me save the attribute rule.

So, if FeatureSetByName needs a text literal (not template literal), then how is the above working as expected, just not able to save?

Any other ideas how to iterate through a list of layers like so?

 

var fclist = ['sdInlet','sdOutlet','sdCleanOut',"sdManhole"]
for (var p in fclist)
    var point_fs = FeatureSetByName($datastore, `${fclist[p]}`, fields, true);
    //Do stuff with it

 

Thanks again,

R_

@Jake_S 

0 Kudos
Jake_S
by Esri Contributor
Esri Contributor

@RhettZufelt what you're looking for is a build a dynamic query. In this post Building a dynamically FeatureSetByName query Hussein points out:


"FeatureSetByName does not support dynamic class names. The name of the class has to be available during static analysis of the script so we can build out the relations in the catalog. (E.g. if you copy Class A which has an attribute rule that reads from Class B we also copy Class B too)"

Not the best resolution but its confirmed by the Esri team

~ Jake

0 Kudos
RhettZufelt
MVP Notable Contributor

Thanks, that would explain why I couldn't figure it out 🙂

R_

0 Kudos
MikeMillerGIS
Esri Frequent Contributor

You can make this work, just need to build it like this

function get_fs(cls_name)
{
  return Decode(cls_name,
      "sdInlet",FeatureSetByName($datastore, "sdInlet", ['*'], true),
      "sdOutlet",FeatureSetByName($datastore, "sdOutlet", ['*'], true),
      "sdCleanOut",FeatureSetByName($datastore, "sdCleanOut", ['*'], true),
      "sdManhole",FeatureSetByName($datastore, "sdManhole", ['*'], true),
      null)
}
var fclist = ['sdInlet','sdOutlet','sdCleanOut',"sdManhole"];
for (var p in fclist){
    var point_fs = get_fs(fclist[p]);
    //Do stuff with it
}

 

RhettZufelt
MVP Notable Contributor

Thanks @MikeMillerGIS , exactly what I was looking for.

Also, great use for the Decode function.

R_

0 Kudos