Edit: I got more written, code example is below. I can see, using a console command, that the new order_by field is populated the way I want, and all the other fields have their data in them, but when I hit 'Done' on the data expression, the Dashboard tells me there's no data from the expression. What am I doing wrong that there's no data when I can see it from a Console command just before the Return at the end?
Hello!
I've been trying to figure out how to use Arcade to create a field in a Dashboard, and then use that field to sort a Serial Chart, but I'm new to Arcade and am having trouble figuring it out. I know some Python (self-taught), but my couple days with Arcade haven't yielded much fruit yet.
I can access my portal item, and get to the schema, but I'm not understanding how to create a new field in the table, and then populate that field based on a value in another field.
// Connect to portal item, the specific table I want to add a field to
// in this case. Portal and item ID replaced with x's
var fs = FeatureSetByPortalItem(
Portal('xxx'),
'xxx',
1,
['*'],
false
)
// get schema for the table.
// What is ['fields'] doing here exactly?
var out_fields = Schema(fs)['fields']
// add a new field to the schema for the value to sort by.
// I can see this adding a field to the schema using a console command.
Push(
out_fields,
{name: 'order_by', alias: 'Order', type: 'esriFieldTypeString'}
)
// Use updated schema to create a new dict
var out_dict = {
fields: out_fields,
geometryType: '',
features: []
}
// Iterate over features and populate order_by field
for (var f in fs){
// Create dict from existing attributes
var atts = Dictionary(Text(f))['attributes']
// The decode mapping
var d = Decode(
f['The_Field_name'],
"INS", "A",
"RAN", "B",
"TXT", "C",
"FOR", "D",
"EXP", "E",
"PUR", "F",
"HIT", "G",
"FAL", "H",
"REW", "I",
"CAL", "J",
""
)
// Setting the field order_by to the decode value
atts['order_by'] = d
// This makes the features of out_dict, which were empty when we
// created it above, to the value of the atts dict I think?
Push(
out_dict['features'],
atts)
}
// Running a console command on out_dict at this point shows all my fields
// and that they have date in every field, and I can see order_by is
// populated, but when I hit Done and run it, I get No Data.
Return FeatureSet(Text(out_dict))
This is where I get lost.
How do I access the values in any particular field in the table I'm calling to evaluate them, and then how do I write whatever corresponding value I want to the new field created?
I was able to do it successfully in our Portal Map Viewer, but I'm struggling to figure out how to do it in the Dashboard itself.
Any help would be greatly appreciated! And if I need to clarify anything, please let me know.
EDIT: See top of post, I've gotten more code written, but can't figure out why it's empty when I hit done.
Thanks,
Ethan