I saw a post similar to this about using the decode expression to populate a popup on AGOL. I have a little twist. I want the decode expression to select a different record depending on the value of a certain attribute and then return that record's attribute values.
So what I want to get is a line in the popup that says Secondary Contact and fills in the inspector name and phone number by the Decode determining which ID or OBJECTID record to use from the INSP_AREA of the polygon selected.
Within the record being selected by the ID or OBJECTID field to return the Inspector Name and Phone Number.
I'm a bit of a novice so not sure what I'm doing wrong.
I tried a few iterations of this and got to: Error on line 11. Cannot assign to const value.
var north = 1;
var alt_north = 3;
var south = 2;
var alt_south = 322;
var selection = $feature.INSP_AREA;
var row=Decode(selection, "Northwest", north, "Northeast", north, "Hospital", alt_north, "Power Plant", alt_north, "Airport", alt_south, south);
var contact = "Secondary Contact " + $feature.INSPECTOR_NAME + " phone " + $feature.PHONE;
return When(row < 3, ($feature.ID = row; contact), row >= 3,($feature.OBJECTID = row; contact), "Sorry");
And I tried this: Error on line 11. Cannot assign to const value.
var north = 1;
var alt_north = 3;
var south = 2;
var alt_south = 322;
var selection = $feature.INSP_AREA;
var row=Decode(selection, 'Northwest', north, 'Northeast', north, 'Hospital', alt_north, 'Power Plant', alt_north, 'Airport', alt_south, south);
var contact = "Secondary Contact " + $feature.INSPECTOR_NAME + " phone " + $feature.PHONE;
iif(row < 3, $feature.ID = row, $feature.OBJECTID = row);
return "Secondary Contact " + $feature.INSPECTOR_NAME + " phone " + $feature.PHONE;
You can use the $layer profile variable to get another record in your dataset with the Filter and First functions. The return uses a template literal.
var sql
if (row < 3) {
sql = "ID = @row"
} else {
sql = "OBJECTID = @row"
}
var feat = First(Filter($layer, sql))
return `Secondary Contact ${feat.INSPECTOR_NAME} phone ${feat.PHONE}`;
Thanks for your help Ken. I learned a few things just from your reply but I'm still having a problem with the output. Here's everything again as I have it in there. The Error says Object not found $layer. I did not call out $layer as a variable because from what I've been looking at that should be known.
Just so you know I am doing this in ArcPro 2.9.6 as a label to test it before I put it into a webmap as a popup.
var north = 1;
var alt_north = 3;
var south = 2;
var alt_south = 322;
var selection = $feature.INSP_AREA;
var row=Decode(selection, 'Northwest', north, 'Northeast', north, 'Hospital', alt_north, 'Power Plant', alt_north, 'Airport', alt_south, south);
//return `selection row`
var sql
if (row < 3) {
sql = "ID = @row"
} else {
sql = "OBJECTID = @row"
}
var feat = First(Filter($layer, sql))
return `Secondary Contact ${feat.INSPECTOR_NAME} phone ${feat.PHONE}`;
I'm not sure why it's not working in your version of ArcGIS Pro. The $layer variable was introduced in Arcade v1.5 and your version uses v1.17. A simple test works correctly in Pro 3.4.2 on my data (returning the FIELD_ID for the first record on any feature with an OBJECTID greater than 10)
var row = $feature.OBJECTID
var sql = "OBJECTID = @row"
if (row > 10) sql = "OBJECTID = 1"
return First(Filter($layer, sql)).FIELD_ID
In any case, it should work correctly in the popup in your webmap
Ken,
The $layer does not work for me. It says it is unrecognized. I tried it in the webmap even though it didn't work in ArcPro, but got the same result. In Pro I only had the one feature in the map. In the webmap I was selected on the layer and using the popup tool for it to add the expression.
I was able to make it work by changing the variables to the text I wanted for each area dependent on which polygon was selected. Everything worked down to the $layer. That last statement before the return just didn't. I'll just have to change the variables when an inspector name changes.
Thanks for your help! I really appreciate it!