Arcade dictionary to FeatureSet

5514
12
Jump to solution
04-14-2021 09:59 AM
MarcHoogerwerf_TAP
New Contributor II

Hi,

I'm trying to convert a dictionary to a FeatureSet in an Arcade data Expression. Everything works well until I use a Date field.

The following works ok, just inserting null values for the date field

var data_dict = {
'fields': [
{'name': 'EditDate','type': 'esriFieldTypeDate'},
{'name': 'country','type': 'esriFieldTypeString'},
{'name': 'status','type': 'esriFieldTypeInteger'}
],
'geometryType':'',
'features': []
};

for (var i=0; i < 10;i++) {
data_dict.features[i] = {
'attributes': {
'EditDate': null,
'country': 'NL',
'status': 230
}
}
}
Console(data_dict)
var fs = FeatureSet(Text(data_dict))
return fs

 It results in a FeatureSet with empty values for the date field and all other field filled.

However, if I try Now() or Text(Now(),"YYYY-MM-DD") or Timestamp() or just a date like string '2021-04-14' :

var data_dict = {
'fields': [
{'name': 'EditDate','type': 'esriFieldTypeDate'},
{'name': 'country','type': 'esriFieldTypeString'},
{'name': 'status','type': 'esriFieldTypeInteger'}
],
'geometryType':'',
'features': []
};

for (var i=0; i < 10;i++) {
data_dict.features[i] = {
'attributes': {
'EditDate': Now(),
'country': 'NL',
'status': 230
}
}
}
Console(data_dict)
var fs = FeatureSet(Text(data_dict))
return fs

 I end up with an empty Featureset

Any ideas?

Regards,

Marc

0 Kudos
1 Solution

Accepted Solutions
by Anonymous User
Not applicable

Hi @MarcHoogerwerf_TAP - the date field is looking for a UNIX timestamp, which you can create in Arcade with DateDiff().

var start = ToLocal(Date(1970, 0, 01, 0, 0, 0, 0))
var dt = DateDiff(Now(), start, 'milliseconds')

 

Hope this helps!

-Peter 

View solution in original post

12 Replies
by Anonymous User
Not applicable

Hi @MarcHoogerwerf_TAP - the date field is looking for a UNIX timestamp, which you can create in Arcade with DateDiff().

var start = ToLocal(Date(1970, 0, 01, 0, 0, 0, 0))
var dt = DateDiff(Now(), start, 'milliseconds')

 

Hope this helps!

-Peter 

MarcHoogerwerf_TAP
New Contributor II

Hi Peter,

Brilliant! It works now. Thanks

- Marc

DavidPike
MVP Frequent Contributor

@Anonymous User was scratching my head for a while on this, after a quick google it led me here - thanks for your solution.

DarrylKlassen1
Occasional Contributor II

This is great, i have been struggling with this for over a day.  Thanks for the help.

0 Kudos
by Anonymous User
Not applicable

Hi @Anonymous User, I'm tying to figure out where I would assign the "dt" variable within my dictionary. See Expression below. I'm having the same issue, in which the "CloseOutDate" field is not populating.

var portal = Portal("https://cms.maps.arcgis.com/"); 

var TandJSoft = FeatureSetByPortalItem(portal,"c6366ef0c8424130a76e9e9d08f292de",0,["*"],false); 

var JWSoft = FeatureSetByPortalItem(portal,"92497f50542248dcb798d091dd18d004",0,["*"],false); 

var start = ToLocal(Date(1970,0,01,0,0,0,0))

var dt = DateDiff(Now(), start, 'milliseconds')

var combinedDict = { 
  'fields': [ 
    { name: "Contractor", type:"esriFieldTypeString" },
    { name: "Total", type: "esriFieldTypeDouble" },
    { name: "Address", type: "esriFieldTypeString" }, 
    { name: "ProjectName", type: "esriFieldTypeString" },
    {name: "Status", type: "esriFieldTypeString" },
    {name: "CloseOutDate", type: "esriFieldTypeDate" },
  ], 
  geometryType: "", 
  features: [], 
};

var i = 0; 
// Loop through each FeatureSet and store its attributes 
for (var t in TandJSoft) { 
  combinedDict.features[i++] = { 
    attributes: {
      Contractor: t["Contractor"],
      Total: t["Total"],
      Address: t["Address"], 
      ProjectName: t["ProjectName"],
      Status: t["Status"],
      CloseOutDate: t["CloseOutDate"],
    }, 
  }; 
} 

for (var m in JWSoft) { 
  combinedDict.features[i++] = { 
    attributes: { 
      Contractor: m["Contractor"],
      Total: m["Total"],
      Address: m["Address"], 
      ProjectName: m["ProjectName"],
      Status: m["Status"],
      CloseOutDate: m["CloseOutDate"],
    }, 
  }; 
}

// Return dictionary cast as a FeatureSet  
return FeatureSet(Text(combinedDict));

 

by Anonymous User
Not applicable

@Anonymous User you can remove the variable line 'var dt = ...' completely as you're not wanting to get the date diff for Now(), as that's the current date at time of execution, instead you'll use your field value date to replace the Now(). For each of the date fields, in each featureset update them to the following but change the feature variable to be relevant i.e. t or m:

CloseOutDate: DateDiff(t["CloseOutDate"], start, 'milliseconds')

 

by Anonymous User
Not applicable

That did the trick! Thanks so much for the help.

by Anonymous User
Not applicable

Hi @Anonymous User what's the method for using an existing date field, coming from an exisitng FeatureSet, in a dictionary i.e. @Anonymous User's post below?

Having to do a DateDiff for each date field seems overly complicated. I'm guessing there are some smarts whereby the date field is automatically being converted to text on execution, instead of an epoch as the example from this page (below) https://developers.arcgis.com/arcade/function-reference/date_functions/#date doesn't work:

var recordDate = Date($feature.dateField)

 Thanks

0 Kudos
RobertAnderson3
MVP Regular Contributor

This was a huge help, the problem I was then running into was in using data from Survey123 it did not have the time zone correct and I could not figure out why.

I ended up using Number() around my date and it worked out, but I'm curious about the time zone issues I had with DateDiff. Even with Now() it is off by -4 hours, I'm in ADT (-3) which is (-4) when AST. Survey123 is right but the dashboard seems to compensate a second time? 

0 Kudos