Select to view content in your preferred language

Arcade script to split multi pick field values

974
3
Jump to solution
03-01-2023 04:42 PM
AndrewWallick
Occasional Contributor II

We have a working script to split apart values from a field that's the result of a multi-pick survey123 question.  The resulting table has three fields with a row for each answer, and we use the script in a Dashboard. We tried adjusting this script to work in a different dashboard with a different feature service, but it's only returning empty fields.

They seem to be doing the same things, and I believe the punctuation is all the same as well. The main difference I can see is the non-working script is trying to use a date field where the working script is using a second string field. So maybe this could be causing a problem. But if so, I'm not sure how to fix it.

Anyone have any idea?

Working Script:

 

// Write an expression that returns a FeatureSet.
// Documentation: https://arcg.is/3c419TD
// Samples: https://arcg.is/38SEWWz

// Reference layer using the FeatureSetByPortalItem() method.
var vportal = Portal('https://www.arcgis.com')
var fs = FeatureSetByPortalItem(
    vportal,
    '3b33e05c548a4688ba8852a1053f580b',
    0,
    ['severe_hazard_items_avail','City'],
    false
);
 
// Create empty array for features and feat object
var features = [];
var feat;

// Split comma separated hazard types and store in dictionary.
for (var vfeature in fs) {
    var split_array  =  Split(vfeature["severe_hazard_items_avail"], ',')
    var count_arr = Count(split_array)
    for(var i = 0; i < count_arr; i++ ){
        feat = {
            'attributes': {
                'split_choices': Trim(split_array[i]),
                'City':vfeature.City
            }
        }
        Push(features, feat);
}}

// Empty dictionary to capture each hazard as separate rows.
var choicesDict = {
    'fields': [
        { 'name': 'split_choices', 'type': 'esriFieldTypeString'},
        { 'name': 'City', 'type': 'esriFieldTypeString'}],
    'geometryType': '',
    'features': features
};

// Convert dictionary to featureSet.
var fs_dict = FeatureSet(Text(choicesDict));
return fs_dict

 

 
 
Not working script:

 

// Reference layer using the FeatureSetByPortalItem() method.
var vportal = Portal('https://www.arcgis.com')
var fs = FeatureSetByPortalItem(
vportal,
'd9ee252d54894d7980539078681775ee',
0,
['training_types', '_date'],
false
);

// Create empty array for features and feat object
var features = [];
var feat;

// Split comma separated hazard types and store in dictionary.
for (var vfeature in fs) {
var split_array = Split(vfeature["training_types"], ',')
var count_arr = Count(split_array)
for(var i = 0; i < count_arr; i++ ){
feat = {
'attributes': {
'split_choices': Trim(split_array[i]),
'_date':vfeature._date
}
}
Push(features, feat);
}}

// Empty dictionary to capture each choice as separate rows.
var choicesDict = {
'fields': [
{ 'name': 'split_choices', 'type': 'esriFieldTypeString'},
{ 'name': 'date', 'type': 'esriFieldTypeDate'}],
'geometryType': '',
'features': features
};

// Convert dictionary to featureSet.
var fs_dict = FeatureSet(Text(choicesDict));
return fs_dict

 

0 Kudos
1 Solution

Accepted Solutions
JohannesLindner
MVP Frequent Contributor

To post code:

JohannesLindner_0-1677736512957.png

JohannesLindner_1-1677736529803.png

 

Sadly, fields with type esriFieldTypeDate don't expect dates, but numbers. So you have to convert first:

feat = {
    'attributes': {
        'split_choices': Trim(split_array[i]),
        '_date': Number(vfeature._date)
        }
    }

 

 

This is a behavior that confuses a lot of users. I'm trying to get it fixed, please give your kudo to this idea:

Arcade: Allow Date() values in date fields (esriFi... - Esri Community


Have a great day!
Johannes

View solution in original post

3 Replies
JohannesLindner
MVP Frequent Contributor

To post code:

JohannesLindner_0-1677736512957.png

JohannesLindner_1-1677736529803.png

 

Sadly, fields with type esriFieldTypeDate don't expect dates, but numbers. So you have to convert first:

feat = {
    'attributes': {
        'split_choices': Trim(split_array[i]),
        '_date': Number(vfeature._date)
        }
    }

 

 

This is a behavior that confuses a lot of users. I'm trying to get it fixed, please give your kudo to this idea:

Arcade: Allow Date() values in date fields (esriFi... - Esri Community


Have a great day!
Johannes
AndrewWallick
Occasional Contributor II

Thank you so much, thank worked!

0 Kudos
DavidNyenhuis1
Esri Contributor

With this week's update to Arcade and ArcGIS Dashboards, this issue is fixed. Dates in feature sets now just work. You no longer have to wrap dates with Number() if you pass the dictionary into the FeatureSet() function (which as of this release accepts a dictionary as opposed to only text based JSON).

Instead of:

return FeatureSet(Text(dict))

Do this:

return FeatureSet(dict)

Learn more on this blog post

 

NOTE: For Enterprise users, this update is targeted for 11.2