Select to view content in your preferred language

Arcade Data Expression Not Creating FeatureSet

206
7
4 weeks ago
Labels (1)
HaydenHarrison1
Occasional Contributor

Hello, 

I am trying to create a data expression that would query a feature layer containing work orders (WOs) and determine how many "outstanding" WOs (aka older than 7 days) we've had over the last 5 years. I am able to loop through the dataset and get a count of all outstanding work orders but can't get it to create a featureset to use in a chart widget on a dashboard. The output is a blank featureset. Version context: enterprise portal is on 11.1. Any input is greatly appreciated.  

 

var workorders = FeatureSetByPortalItem(
    portal,
    'itemid',
    0,
    ['InitiateDate', 'ActualFinishDate', 'Priority', 'WorkOrderID', 'OBJECTID'],
    true
);
 
var filteredFeatures = [];

 
// Loop through work orders
for (var wo in workorders) {
    var startDate = wo["InitiateDate"];
    var endDate = wo["ActualFinishDate"];
 
    
    // Ensure both dates are not empty or null
    if (!IsEmpty(startDate) && !IsEmpty(endDate)) {
        // Calculate days to complete
        var daysToComplete = DateDiff(endDate, startDate, 'days');
 
        // Only include work orders that took more than 7 days
        if (daysToComplete > 7) {
            // Add the feature to the filtered list
            Push(filteredFeatures, {
                attributes: {
                    WorkOrderID: Text(wo["WorkOrderID"]),
                    InitiateDate: Date(startDate),
                    ActualFinishDate: Date(endDate),
                    DaysToComplete: daysToComplete
                }
            });
        }
    }
}
 
var finaltable = {
    fields: [
        {name: 'WorkOrderID', type: 'esriFieldTypeString'},
        {name: 'InitiateDate', type: 'esriFieldTypeDate'},
        {name: 'ActualFinishDate', type: 'esriFieldTypeDate'},
        {name: 'DaysToComplete', type: 'esriFieldTypeInteger'}
    ],
    geometryType: '',
    features: [
        {attributes: {
            WorkOrderID: Text(wo["WorkOrderID"]),
            InitiateDate: (startDate),
            ActualFinishDate: (endDate),
            DaysToComplete: (daysToComplete)
        }
        }
        ]
}
 
return FeatureSet(Text(finaltable))

 

 

0 Kudos
7 Replies
KenBuja
MVP Esteemed Contributor

You're building the array of filterFeatures, but you don't add that to finalTable.

var finaltable = {
  fields: [
    { name: "WorkOrderID", type: "esriFieldTypeString" },
    { name: "InitiateDate", type: "esriFieldTypeDate" },
    { name: "ActualFinishDate", type: "esriFieldTypeDate" },
    { name: "DaysToComplete", type: "esriFieldTypeInteger" }
  ],
  geometryType: "",
  features: filteredFeatures
};

return FeatureSet(finaltable);
0 Kudos
HaydenHarrison1
Occasional Contributor

Hi Ken, 

 

Thanks for responding. When I edited the code to your above suggestion, I got an invalid parameter error. ScreenshotParameterError.png

0 Kudos
KenBuja
MVP Esteemed Contributor

I ran your code using my own data and discovered that you have to convert daysToComplete to an integer (line 34). Otherwise, it ran correctly (with some field name changes and a different minimum days)

var workorders = Top(
  FeatureSetByPortalItem(
    Portal("https://noaa.maps.arcgis.com/"),
    "b6b9dd55450a4f64bcafb3cd6e005a5c",
    0,
    ["stormdate", "surveydate", "efscale", "OBJECTID"],
    true
  ),
  10
);

var filteredFeatures = [];

// Loop through work orders
for (var wo in workorders) {
  var startDate = wo["stormdate"];
  var endDate = wo["surveydate"];

  // Ensure both dates are not empty or null
  if (!IsEmpty(startDate) && !IsEmpty(endDate)) {
    // Calculate days to complete
    var daysToComplete = DateDiff(endDate, startDate, "days");
    // Only include work orders that took more than 7 days
    if (daysToComplete > 2) {
      // Add the feature to the filtered list
      Push(
        filteredFeatures,
        {
          attributes:
            {
              WorkOrderID: Text(wo["efscale"]),
              InitiateDate: Date(startDate),
              ActualFinishDate: Date(endDate),
              DaysToComplete: Floor(daysToComplete)
            }
        }
      );
    }
  }
}

var finaltable = {
  fields: [
    {
      name: "WorkOrderID",
      alias: "Work Order ID",
      type: "esriFieldTypeString"
    },
    { name: "InitiateDate", alias: "Initiate Date", type: "esriFieldTypeDate" },
    {
      name: "ActualFinishDate",
      alias: "Actual Finish Date",
      type: "esriFieldTypeDate"
    },
    {
      name: "DaysToComplete",
      alias: "Days To Complete",
      type: "esriFieldTypeInteger"
    }
  ],
  geometryType: "",
  features: filteredFeatures
};

return FeatureSet(finaltable);

 

0 Kudos
HaydenHarrison1
Occasional Contributor

Changing the daystoComplete worked! I can now see the featureset. However, the date fields aren't populating. Thoughts? 

 

 

var workorders = FeatureSetByPortalItem(
    portal,
    'id',
    0,
    ['InitiateDate', 'ActualFinishDate', 'Priority', 'WorkOrderID', 'OBJECTID','DESCRIPTION'],
    false
);

// Debug: Check if workorders were retrieved
Console("Total Work Orders: " + Count(workorders));

var filteredFeatures = [];

// Loop through work orders
for (var wo in workorders) {
    var startDate = Date(wo["InitiateDate"]);
    var endDate = Date(wo["ActualFinishDate"]);

    // Debug: Log first work order details
    if (Count(filteredFeatures) == 0) {
        Console("Sample Work Order: " + Text(wo));
    }

    // Ensure both dates are valid
    if (!IsEmpty(startDate) && !IsEmpty(endDate)) {
        // Calculate days to complete
        var daysToComplete = DateDiff(endDate, startDate, "days");

        // Only include work orders that took more than 7 days
        if (daysToComplete > 7) {
            // Add the feature to the filtered list
            Push(filteredFeatures, {
                attributes: {
                    WorkOrderID: Text(wo["WorkOrderID"]),  // Ensure it's string
                    InitiateDate: Date(startDate),
                    ActualFinishDate: Date(endDate),
                    DaysToComplete: Floor(daysToComplete),
                    OBJECTID: (wo["OBJECTID"]),
                    Description: (wo["DESCRIPTION"])
                }
            });
        }
    }
}

// Debug: Check how many were filtered
Console("Filtered Work Orders: " + Count(filteredFeatures));

return FeatureSet(Text({
    fields: [
        { name: "WorkOrderID", alias: "Work Order ID", type: "esriFieldTypeString" },
        { name: "startDate", alias: "Initiate Date", type: "esriFieldTypeDate" },
        { name: "endDate", alias: "Actual Finish Date", type: "esriFieldTypeDate" },
        { name: "daysToComplete", alias: "Days To Complete", type: "esriFieldTypeInteger" },
        { name: "Description", alias: "Description", type: "esriFieldTypeString" },
        { name: "OBJECTID", alias: "OBJECTID", type: "esriFieldTypeOID" },
    ],
    geometryType: "",
    features: filteredFeatures
}))

 

0 Kudos
KenBuja
MVP Esteemed Contributor

The field names in the dictionary ("startDate" and "endDate") are different than the attributes in the array ("InitiateDate" and "ActualFinishDate")

 

0 Kudos
HaydenHarrison1
Occasional Contributor

Unfortunately the feature set will not populate unless I use "startDate" and "endDate". 

0 Kudos
KenBuja
MVP Esteemed Contributor

What I mean is that the attribute keys have to be the same as the Dictionary field names to be mapped properly

attributes: {
  WorkOrderID: Text(wo["WorkOrderID"]),  // Ensure it's string
  InitiateDate: Date(startDate),
  ActualFinishDate: Date(endDate),
  DaysToComplete: Floor(daysToComplete),
  OBJECTID: (wo["OBJECTID"]),
  Description: (wo["DESCRIPTION"])
}
--------------------
fields: [
  { name: "WorkOrderID", alias: "Work Order ID", type: "esriFieldTypeString" },
  { name: "InitiateDate", alias: "Initiate Date", type: "esriFieldTypeDate" },
  { name: "ActualFinishDate", alias: "Actual Finish Date", type: "esriFieldTypeDate" },
  { name: "daysToComplete", alias: "Days To Complete", type: "esriFieldTypeInteger" },
  { name: "Description", alias: "Description", type: "esriFieldTypeString" },
  { name: "OBJECTID", alias: "OBJECTID", type: "esriFieldTypeOID" },
],
0 Kudos