Test execution error: Execution error - Function not found. Verify test data.

411
7
Jump to solution
11-24-2023 11:11 AM
Jabousaw
New Contributor II

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;

 

 

 

0 Kudos
1 Solution

Accepted Solutions
KenBuja
MVP Esteemed Contributor

You can use this code in each of your indicators

// 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 i = 0;
var totalWaitingTime = 0;
var maxWaitingTime = -Infinity;
var minWaitingTime = Infinity;

var features = [];

// 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
        var feat = {
            'attributes': {
                'WellAbandonmentDate': wellAbandonmentDate,
                'DateReceived': dateReceived,
                'WaitingTime': waitingTime
            }
        );
        Push(features, feat)
        i++;
    }
}

// Calculate average waiting time
var averageWaitingTime;
if (i > 0) {
    averageWaitingTime = totalWaitingTime / i;
} else {
    averageWaitingTime = null;
}
// Loop through features
for (var j in features) {
    var featureData = features[j];
    // Add calculated values to the features
    featureData.attributes.AverageWaitingTime = averageWaitingTime;
    featureData.attributes.MaxWaitingTime = maxWaitingTime;
    featureData.attributes.MinWaitingTime = minWaitingTime;
}

var Dict = { 
    'fields': [ 
        {'name': 'AverageWaitingTime', 'type': 'esriFieldTypeDouble'},
        {'name': 'MaxWaitingTime', 'type': 'esriFieldTypeDouble'},
        {'name': 'MinWaitingTime', 'type': 'esriFieldTypeDouble'}
    ],   
    'geometryType': '',    
    'features': [{
            'attributes': {
                'AverageWaitingTime': averageWaitingTime,
                'MaxWaitingTime': maxWaitingTime,
                'MinWaitingTime': minWaitingTime
            }
     }]
};
return FeatureSet((Dict))

 

View solution in original post

0 Kudos
7 Replies
DavidPike
MVP Frequent Contributor

I'm guessing this an Enterprise Portal dashboard?  It's likely things such as DateDiff() aren't present in your Arcade version.

0 Kudos
Jabousaw
New Contributor II

It pops up as one of the functions I can use and I rewrote the code to manually calculate the difference in time using conversions and it gave me the same error. 

0 Kudos
KenBuja
MVP Esteemed Contributor

You're using Push incorrectly in several places. I've restructured your code a little bit and it returns the Dict.

// 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 i = 0;
var totalWaitingTime = 0;
var maxWaitingTime = -Infinity;
var minWaitingTime = Infinity;

var features = [];

// 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
        var feat = {
            'attributes': {
                'WellAbandonmentDate': wellAbandonmentDate,
                'DateReceived': dateReceived,
                'WaitingTime': waitingTime
            }
        );
        Push(features, feat)
        i++;
    }
}

// Calculate average waiting time
var averageWaitingTime;
if (i > 0) {
    averageWaitingTime = totalWaitingTime / i;
} else {
    averageWaitingTime = null;
}

var Dict = { 
    'fields': [ 
        {'name': 'WellAbandonmentDate', 'type': 'esriFieldTypeDate'},   
        {'name': 'DateReceived', 'type': 'esriFieldTypeDate'},
        {'name': 'WaitingTime', 'type': 'esriFieldTypeDouble'},  // New field for waiting time
        {'name': 'AverageWaitingTime', 'type': 'esriFieldTypeDouble'},
        {'name': 'MaxWaitingTime', 'type': 'esriFieldTypeDouble'},
        {'name': 'MinWaitingTime', 'type': 'esriFieldTypeDouble'}
    ],   
    'geometryType': '',    
    'features': features
};

// 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;

However, is your intent to just have one record for the maximum, minimum, and average waiting times for your indicator? All of the records in your dataset will have the same values for these three times.

 

 

 

0 Kudos
Jabousaw
New Contributor II

I'll have 3 separate indictors, one displaying maximum, minimum, and average waiting times, respectively since I don't think I can have 1 indicator showcases all 3 values. It's also easier to look at when having 3 separate indicators. Also, the code you provided ran but I don't think it outputted the correct information, as it just outputted a bunch of text and no numbers. 

Jabousaw_1-1700858958676.png

 

Jabousaw_0-1700858902297.png

Jabousaw_2-1700858976410.png

 

0 Kudos
KenBuja
MVP Esteemed Contributor

You can use this code in each of your indicators

// 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 i = 0;
var totalWaitingTime = 0;
var maxWaitingTime = -Infinity;
var minWaitingTime = Infinity;

var features = [];

// 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
        var feat = {
            'attributes': {
                'WellAbandonmentDate': wellAbandonmentDate,
                'DateReceived': dateReceived,
                'WaitingTime': waitingTime
            }
        );
        Push(features, feat)
        i++;
    }
}

// Calculate average waiting time
var averageWaitingTime;
if (i > 0) {
    averageWaitingTime = totalWaitingTime / i;
} else {
    averageWaitingTime = null;
}
// Loop through features
for (var j in features) {
    var featureData = features[j];
    // Add calculated values to the features
    featureData.attributes.AverageWaitingTime = averageWaitingTime;
    featureData.attributes.MaxWaitingTime = maxWaitingTime;
    featureData.attributes.MinWaitingTime = minWaitingTime;
}

var Dict = { 
    'fields': [ 
        {'name': 'AverageWaitingTime', 'type': 'esriFieldTypeDouble'},
        {'name': 'MaxWaitingTime', 'type': 'esriFieldTypeDouble'},
        {'name': 'MinWaitingTime', 'type': 'esriFieldTypeDouble'}
    ],   
    'geometryType': '',    
    'features': [{
            'attributes': {
                'AverageWaitingTime': averageWaitingTime,
                'MaxWaitingTime': maxWaitingTime,
                'MinWaitingTime': minWaitingTime
            }
     }]
};
return FeatureSet((Dict))

 

0 Kudos
Jabousaw
New Contributor II

Thank you, the code worked! However, it is giving me negative time values for average and minimum waiting times. I've tried adding conditions to the code to fix that but now I'm getting 0 for both the minimum and average waiting times. Do you have any suggestions on what could fix this?

0 Kudos
KenBuja
MVP Esteemed Contributor

Switch the order of the dates in the DateDiff function in line 37.