Okay so I have this code written for a data expression for an indicator element on my dashboard. The issue is that every time I try to run it, I get this error: "Test execution error: Execution error - Function not found. Verify test data." I'm not sure what function it is referring to or what I'm doing wrong.
Any help would be appreciated.
// Define the feature set
var Dates = FeatureSetByPortalItem(
Portal('https://regionofpeel.maps.arcgis.com'),
'5d0f25a7a3314686b526bab55653befb',
0
);
// Function to check if a string represents a valid date
function IsDate(dateString) {
// Check if the date string is not null, undefined, or an empty string
if (dateString == null || dateString == "") {
return false;
}
// Attempt to create a Date object from the string using DateTime function
var dateObject = Date(dateString);
// Check if the created Date object is a valid date
return !IsEmpty(dateObject);
}
var Dict = {
'fields': [
{'name': 'WellAbandonmentDate', 'type': 'esriFieldTypeDate'},
{'name': 'DateReceived', 'type': 'esriFieldTypeDate'},
{'name': 'WaitingTime', 'type': 'esriFieldTypeDouble'}, // New field for waiting time
],
'geometryType': '',
'features': []
};
var i = 0;
var totalWaitingTime = 0;
var maxWaitingTime = -Infinity;
var minWaitingTime = Infinity;
// Loop through records in the feature set
for (var record in Dates) {
var wellAbandonmentDate = record['Date_abandonment'];
var dateReceived = record['date_recieved'];
// Check if both dates are valid
if (IsDate(wellAbandonmentDate) && IsDate(dateReceived)) {
// Calculate waiting time in days using the DateDiff function
var waitingTime = DateDiff(dateReceived, wellAbandonmentDate, 'days');
// Update total, max, and min waiting times
totalWaitingTime += waitingTime;
maxWaitingTime = Max(maxWaitingTime, waitingTime);
minWaitingTime = Min(minWaitingTime, waitingTime);
// Add feature to Dict
Dict.features.push({
'attributes': {
'WellAbandonmentDate': wellAbandonmentDate,
'DateReceived': dateReceived,
'WaitingTime': waitingTime
}
});
i++;
}
}
// Calculate average waiting time
var averageWaitingTime;
if (i > 0) {
averageWaitingTime = totalWaitingTime / i;
} else {
averageWaitingTime = null;
}
// Add average, max, and min waiting times to Dict fields
Dict.fields.push({'name': 'AverageWaitingTime', 'type': 'esriFieldTypeDouble'});
Dict.fields.push({'name': 'MaxWaitingTime', 'type': 'esriFieldTypeDouble'});
Dict.fields.push({'name': 'MinWaitingTime', 'type': 'esriFieldTypeDouble'});
// Loop through features
for (var j in Dict.features) {
var featureData = Dict.features[j];
// Add calculated values to the features
featureData.attributes.AverageWaitingTime = averageWaitingTime;
featureData.attributes.MaxWaitingTime = maxWaitingTime;
featureData.attributes.MinWaitingTime = minWaitingTime;
}
return Dict;