<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Having syntax issues with 'When' function for recalculating ages in ArcGIS Online Questions</title>
    <link>https://community.esri.com/t5/arcgis-online-questions/having-syntax-issues-with-when-function-for/m-p/1202730#M47407</link>
    <description>&lt;P&gt;You're misunderstanding the When function.&lt;/P&gt;&lt;P&gt;&lt;A href="https://developers.arcgis.com/arcade/function-reference/logical_functions/#when" target="_blank" rel="noopener"&gt;When()&lt;/A&gt; doesn't work on feature sets. It takes multiple booleans and objects and returns the object following the first true boolean.&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;var x = 1
// this will return 25
return When(x == 0, "a", x == 1, 25, x == 2, "z", "default")&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Other things:&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;the dollar sign denotes global variables like $feature, $map, or $layer. You can't just put it in front of custom variables.&lt;/LI&gt;&lt;LI&gt;In most programming languages, "=" is assignment ("var x = 3"). To test for equal, use "==" ("x == 3" -&amp;gt; true)&lt;/LI&gt;&lt;LI&gt;FS is a FeatureSet, a collection of Seatures. You can't call a field on a FeatureSet, only on the separate Features.&lt;/LI&gt;&lt;LI&gt;in your dictionary, you have a different name for the field in the fields declaration and the feature attributes.&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;To solve your problem, create an empty dictionary, then loop through the FeatureSet and Push the corrected values into the feature array:&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;var port = "https://--------------.arcgis.com"

var itemID = "---------------------------------"

var layerID = 3

var fields = ['AGE']

var geo = False

// Set up the FeatureSet with which to call:
var FS = FeatureSetByPortalItem(Portal(port), itemID, layerID, fields, geo)

// return the set of features to use within indicator:

var newagesdictionary = {
    'fields': [
        { 'name': 'age_recalc', 'type': 'esriFieldTypeInteger'}
    ],
    'geometryType':'',
    'features':[]
}

for(var f in FS) {
    var new_age = When(f.AGE == 'BB', '0', f.AGE == 'NN', '0', f.AGE)
    var new_feature = {'attributes': {'age_recalc': Number(new_age)}}
    Push(newagesdictionary.features, new_feature)
}

var recalcagedict = FeatureSet(Text(newagesdictionary));

return recalcagedict&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 15 Aug 2022 06:32:57 GMT</pubDate>
    <dc:creator>JohannesLindner</dc:creator>
    <dc:date>2022-08-15T06:32:57Z</dc:date>
    <item>
      <title>Having syntax issues with 'When' function for recalculating ages</title>
      <link>https://community.esri.com/t5/arcgis-online-questions/having-syntax-issues-with-when-function-for/m-p/1202694#M47405</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;P&gt;Hi there,&lt;/P&gt;&lt;P&gt;I have what I thought to be a simple arcade expression that I'm trying to use for a dashboard indicator. Currently I'm working with a table where ages are a string variable and some&amp;nbsp; records have 'NN" or 'BB' to refer to their stage in infancy. I'm looking to recalculate those records to be 0 and then to convert all the ages to integers so I can find the average.&lt;/P&gt;&lt;P&gt;I've been playing with this script all day and I think I'm running into syntax issues with recalling my feature service. Either that or I'm just not understanding the When function. I would really appreciate any suggestions or help!&lt;/P&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;var port = "https://--------------.arcgis.com"

var itemID = "---------------------------------"

var layerID = 3

var fields = ['AGE']

var geo = False

// Set up the FeatureSet with which to call:
var FS = FeatureSetByPortalItem(Portal(port), itemID, layerID, fields, geo)

// return the set of features to use within indicator:

When($FS.AGE ='BB', '0', $FS.AGE = 'NN', '0', defaultValue);

var newagesdictionary = {
    'fields': [
        { 'name': 'age_recalc', 'type': 'esriFieldTypeInteger'}
    ],
    'geometryType':'',
    'features': 
    [{'attributes':
    {'newage': Number($FS.AGE)}
    }]}

var recalcagedict = FeatureSet(Text(newagesdictionary));

return recalcagedict&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I keep getting 'Invalid Parameter as an error message but I'm not sure where it's really referring to.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you so much!&lt;/P&gt;</description>
      <pubDate>Sun, 14 Aug 2022 19:06:05 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-online-questions/having-syntax-issues-with-when-function-for/m-p/1202694#M47405</guid>
      <dc:creator>slach</dc:creator>
      <dc:date>2022-08-14T19:06:05Z</dc:date>
    </item>
    <item>
      <title>Re: Having syntax issues with 'When' function for recalculating ages</title>
      <link>https://community.esri.com/t5/arcgis-online-questions/having-syntax-issues-with-when-function-for/m-p/1202730#M47407</link>
      <description>&lt;P&gt;You're misunderstanding the When function.&lt;/P&gt;&lt;P&gt;&lt;A href="https://developers.arcgis.com/arcade/function-reference/logical_functions/#when" target="_blank" rel="noopener"&gt;When()&lt;/A&gt; doesn't work on feature sets. It takes multiple booleans and objects and returns the object following the first true boolean.&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;var x = 1
// this will return 25
return When(x == 0, "a", x == 1, 25, x == 2, "z", "default")&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Other things:&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;the dollar sign denotes global variables like $feature, $map, or $layer. You can't just put it in front of custom variables.&lt;/LI&gt;&lt;LI&gt;In most programming languages, "=" is assignment ("var x = 3"). To test for equal, use "==" ("x == 3" -&amp;gt; true)&lt;/LI&gt;&lt;LI&gt;FS is a FeatureSet, a collection of Seatures. You can't call a field on a FeatureSet, only on the separate Features.&lt;/LI&gt;&lt;LI&gt;in your dictionary, you have a different name for the field in the fields declaration and the feature attributes.&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;To solve your problem, create an empty dictionary, then loop through the FeatureSet and Push the corrected values into the feature array:&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;var port = "https://--------------.arcgis.com"

var itemID = "---------------------------------"

var layerID = 3

var fields = ['AGE']

var geo = False

// Set up the FeatureSet with which to call:
var FS = FeatureSetByPortalItem(Portal(port), itemID, layerID, fields, geo)

// return the set of features to use within indicator:

var newagesdictionary = {
    'fields': [
        { 'name': 'age_recalc', 'type': 'esriFieldTypeInteger'}
    ],
    'geometryType':'',
    'features':[]
}

for(var f in FS) {
    var new_age = When(f.AGE == 'BB', '0', f.AGE == 'NN', '0', f.AGE)
    var new_feature = {'attributes': {'age_recalc': Number(new_age)}}
    Push(newagesdictionary.features, new_feature)
}

var recalcagedict = FeatureSet(Text(newagesdictionary));

return recalcagedict&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 15 Aug 2022 06:32:57 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-online-questions/having-syntax-issues-with-when-function-for/m-p/1202730#M47407</guid>
      <dc:creator>JohannesLindner</dc:creator>
      <dc:date>2022-08-15T06:32:57Z</dc:date>
    </item>
  </channel>
</rss>

