Select to view content in your preferred language

Arcade in ExB: Code fails if a feature is not selected

223
3
a week ago
Status: Open
ZachBodenner
MVP Regular Contributor

I've been playing around with the newly added Arcade functionality and I've run up against a frustrating issue. I have a text widget that is configured to work with selected features from a particular layer

// selectedFeatures1 is the selection view of data source.
 var selectedFeatures1 = $dataSources["dataSource_2-19962b4bcc1-layer-12-selection"].selectedFeatures;

var fproj = (first(selectedFeatures1))
console ("First Project: "+fproj)
var name = fproj.ProjectName
var sum = fproj.Summary
var ssum = concatenate(split(sum,"|"),`<br><br>`)
console(sum)
var time = fproj.Timeline
var back = fproj.Background
var sback = concatenate(split(back,"|"),`<br><br>`)
var contactName = fproj.CityStaff_Contact
//var contactPhone = fproj.CityStaff_Contact
//var contactEmail = fproj.CityStaff_Contact

/*(var returnString = 
`<h3 style = "color:#000000;font-weight:bold; font-size:24px">${name}</h3>
 <p style = "color:#586424;font-wight:bold">Project Summary
  <p style="color:#000000">${sum}</p>
  </p>
`*/
var header = `<h3 style = "color:#000000;font-weight:bold; font-size:24px">${name}</h3><br>`
//var header = `${name}`

var sumString = IIF(!isEmpty(sum),`<h4 style = "color:#586424;font-weight:bold; font-size:18">Project Summary
  <p style="color:#000000; font-weight:normal;font-size:14px">${ssum}</p>
</h4>`,'')
// var sumString = IIF(!isEmpty(sum),`${sum}`,'')

var timeString = IIF(!isEmpty(time),`<h4 style = "color:#586424;font-weight:bold; font-size:18">Project Timeline: <span style = "color:#000000">${time}</span></h4><br>`,'')
//var timeString = IIF(!isEmpty(time),`Project Timeline: ${time}`,'')

var backString = IIF(!isEmpty(back),`<h4 style = "color:#586424;font-weight:bold; font-size:18">Project Background
  <p style="color:#000000; font-weight:normal;font-size:14px">${sback}</p>
</h4>`,'')

var contactString = IIF(!isEmpty(contactName),`<h4 style = "color:#586424;font-weight:bold; font-size:18">Project Contact<br>
  <span style="color:#000000; font-weight:normal;font-size:14px">${contactName}</span>
</h4>`,'')

var returnString = `
  ${header}${sumString}${timeString}${backString}${contactString}
 `



return {
  value: `${returnString}`,
  text: {
    // size: 14,
    // color: 'rgb(0, 0, 255)',
    // bold: true,
    // italic: false,
    // underline: false,
    // strike: false
  },
};
 
  

Here's the rub: if I don't actually have a feature selected while I'm working on this code, it fails to evaluate and will not insert. 

ZachBodenner_0-1758831837683.png

 

Now, if I go into Live mode and select a feature, then leave Live mode and enter the Arcade editor, it will successfully evaluate and insert. This is an issue because someone could very easily write great, correct code, and will be presented with a very unhelpful error and a widget that wont even accept all that they just wrote. It's not immediately clear that not having a feature selected is what throws this error either. 

My proposed solutions are:

1 (preferred) just allow the code to insert if it fails to evaluate due to lack of selection. That's essentially what Arcade elements in the Map Viewer popup do if there's an issue with no features being grabbed anyway (which is to say, as long as the code itself is sound you can insert Arcade that just returns something empty).

2. (also good) better error messages that alerts the user to the lack of a selection, which causes the code to become un-insertable. Less desirable because it forces the user to leave the code window to go select something, but it would be better than nothing.

3 Comments
ShengdiZhang

This is an example of the best practices described in the documentation:
Advanced formatting guide.

Right now, it can be hard to figure out why the script isn't working. If the return type isn't valid, the application may still fail in other ways, so we must let the code insert safely.

Workarounds you can try:

1. Return an empty object when no selection is made:

if (Count(selected) == 0) { return {};}

2. Enable live view, select a feature, then add your Arcade expression 

Thanks,

Shengdi

ZachBodenner

That's a helpful idea, though perhaps it would be an improvement to have that if statement inserted by default in the same way that the filters are? When I first create this Arcade code based on the Selected Features from the layer, there is a filtering function as well as getting the selected features pre-coded. I thought that was really helpful, but having this one other thing in there would go a long way, I think.

MappyIan

I hit this exact same issue last week and spent far too long trying to figure out what the problem was.  Anything that prevents someone else from wasting hours of their day trying to figure out what the problem is is a good idea in my mind.  Adding the block of code to return an empty object to the default template (with a comment that explains what it's doing and why) seems like the obvious solution.  This idea gets my vote for sure.