Sort FeatureSet in Arcade using OrderBy

6667
2
Jump to solution
07-14-2020 12:04 AM
by Anonymous User
Not applicable

Hi All,

I am trying to sort the results of a table in arcade but having trouble hitting the table's fields. This table is related based on a pin, but not joined in any way to the feature set that the arcade expression is running on. It is arcade in ArcGIS Portal Popups.

Snippet below.

#assign table

var tbl = FeatureSetByName($map,"TABLE");

# assign pin

var pt = $feature['PIN'];

#create sql variable

var sql = "PIN = '" + pt + "'";

#run query on table

var query = Filter(tbl, sql);

var order = OrderBy(query, 'date DESC')

var result = ""
for (var field in order) {
   result += ....;

}

return result;

However when running this it tries to search the $feautre instead of the table resulting in field not found error.. Any asssitance would be appreacited. 

Tags (2)
0 Kudos
1 Solution

Accepted Solutions
XanderBakker
Esri Esteemed Contributor

Hi Cameron McArtney ,

Before looking at the code below, I have a question? What version of Enterprise do you have installed. This will define what functions are available to you. To be sure consult the version matrix: Version Matrix | ArcGIS for Developers 

I have included a couple of Console statements to write to the console to see if you have any results and what you get on the way. Can you try this and post back the information printed to the console?

// assign table
var tbl = FeatureSetByName($map,"TABLE");
Console(Count(tbl));

// assign pin
var pt = $feature['PIN'];
Console(pt)

// create sql variable
var sql = "PIN = @pt";
Console(sql);

// run query on table
var query = Filter(tbl, sql);

// it is good to check if you have any results
var cnt = Count(query);
Console(cnt);

var result = "There are " + cnt + " Result(s):";
if (cnt > 0) {
    // sort the featureset
    var order = OrderBy(query, 'date DESC');
    // construct the result
    for (var feat in order) {
       // change "YourFieldName" for your field name
       result += TextFormatting.NewLine + feat.YourFieldName; 
    }
}

return result;

‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

View solution in original post

2 Replies
XanderBakker
Esri Esteemed Contributor

Hi Cameron McArtney ,

Before looking at the code below, I have a question? What version of Enterprise do you have installed. This will define what functions are available to you. To be sure consult the version matrix: Version Matrix | ArcGIS for Developers 

I have included a couple of Console statements to write to the console to see if you have any results and what you get on the way. Can you try this and post back the information printed to the console?

// assign table
var tbl = FeatureSetByName($map,"TABLE");
Console(Count(tbl));

// assign pin
var pt = $feature['PIN'];
Console(pt)

// create sql variable
var sql = "PIN = @pt";
Console(sql);

// run query on table
var query = Filter(tbl, sql);

// it is good to check if you have any results
var cnt = Count(query);
Console(cnt);

var result = "There are " + cnt + " Result(s):";
if (cnt > 0) {
    // sort the featureset
    var order = OrderBy(query, 'date DESC');
    // construct the result
    for (var feat in order) {
       // change "YourFieldName" for your field name
       result += TextFormatting.NewLine + feat.YourFieldName; 
    }
}

return result;

‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
by Anonymous User
Not applicable

Hi Xander Bakker‌, 

You're spot on, it's version related, running 10.7.1, so ran a test in ArcGIS Online and it works.

I need to investigate further however if it is a bug or just missing in the version. As it fails as its trying to hit the $feature fields instead of the tables fields. Where as AGOL is able to hit the tables fields.

Cheers,