When posting code, use the Insert/edit code sample tool instead of attaching it as a text file.
Give this script a try. It puts null values for the plot attributes in each feature for the grades layer and only fills them in when there is a matching plots feature.
A couple of additional notes about this.
This uses the Arcade variable substitution in the Filter function in line 38, which means you don't have to worry about whether a value has quotes or not.
Aslo, calling the Filter function many times in a loop is very expensive and will slow your script down. Using @jcarlson's Memorize function will drastically reduce the execution time for the script. In my test script using a plots layer of 36 features and a grades feature of 51 features, this reduced the execution time from about 8 seconds to half a second.
function Memorize(fs) {
var temp_dict = {
fields: Schema(fs)["fields"],
geometryType: "",
features: []
};
for (var f in fs) {
var attrs = {};
for (var attr in f) {
attrs[attr] = Iif(TypeOf(f[attr]) == "Date", Number(f[attr]), f[attr]);
}
Push(temp_dict["features"], { attributes: attrs });
}
return FeatureSet(temp_dict);
}
var portal = Portal("https://xyz.maps.arcgis.com/");
var plots = Memorize(FeatureSetByPortalItem(portal, "xyzx", 0, ["*"], false));
var grades = FeatureSetByPortalItem(portal, "xyzx", 0, ["*"], false);
// Create empty features array
var features = [];
// Populate Feature Array
for (var t in grades) {
var tableID = t["BlockCode"];
var feat = {
attributes:
{
BlockCode: tableID,
FinalGrade: t["FinalGrade"],
SelectedEURs: t["SelectedEURs"],
last_name: t["last_name"],
UCode: null,
Vineyard_Name: null
}
};
for (var p in Filter(plots, "UCode = @tableID")) {
feat.attributes.UCode = p["UCode"];
feat.attributes.Vineyard_Name = p["Vineyard_Name"];
}
Push(features, feat);
}
var joinedDict = {
fields: [
{ name: "BlockCode", type: "esriFieldTypeString" },
{ name: "FinalGrade", type: "esriFieldTypeString" },
{ name: "SelectedEURs", type: "esriFieldTypeString" },
{ name: "last_name", type: "esriFieldTypeString" },
{ name: "UCode", type: "esriFieldTypeString" },
{ name: "Vineyard_Name", type: "esriFieldTypeString" }
],
geometryType: "",
features: features
};
// Return dictionary cast as a feature set
return FeatureSet(joinedDict);