Select to view content in your preferred language

Using a When() Statement for a select_multiple in Dashboard List

329
1
04-26-2023 01:08 PM
JasminePrater
Frequent Contributor

I was able to create a When() statement in Arcade for the ArcGIS Online pop-up of a Work Order hosted feature class that displays the workers' names without the underscore in the name and puts each new name on a separate line.  I am trying to recreate this in Dashboard, since Arcade cannot be done in Experience Builder Lists.  However, the syntax is different when doing the code for pop-ups versus in the Dashboard List.  Can someone please help me determine how to do it, if possible?  The current setup looks something like this:

var worker = $feature["WORKERS_ASSIGNED"]

     When(

     worker == 'Worker_One',"Worker One",

     worker == 'Worker_Two',"Worker Two",

     worker == 'Worker_Three',"Worker Three",

     "Unknown")

Depending on which worker(s) are chosen, the names show up in the pop-up as:

     Worker One

     Worker Two

     Worker Three

My reasoning for doing this is so the names look nicely formatted in the app List (whether in Dashboard or Experience Builder), instead of having a list like Worker_One,Worker_Two,Worker_Three.  I currently have a field for each worker name (~25) that formats the name in Survey123 Connect, but I'd love to be able to get rid of those extra fields.  This is the only way I've found that might allow me to do this.  Any advice is welcome!

0 Kudos
1 Reply
JohannesLindner
MVP Alum

To post code:

JohannesLindner_0-1677736512957.pngJohannesLindner_1-1677736529803.png

 

 

However, the syntax is different when doing the code for pop-ups versus in the Dashboard List

The main difference is that you have to return a Featureset for Dashboard elements.

 

Let me start by saying this: If all you're doing is removing the underscores, you're making it much too complicated. Your popup expression could look like this:

// replace underscore with space
return Replace($feature["WORKERS_ASSIGNED"], "_", " ")

 

 

Now, a data expression that goes through your Survey layer and replaces the underscores in the WORKERS_ASSIGNED field could look like this:

// this is my test data
//var fs = {geometryType:"",fields:[{name:"WORKERS_ASSIGNED",type:"esriFieldTypeString"}, {name:"X",type:"esriFieldTypeInteger"}],features:[{attributes:{WORKERS_ASSIGNED:"Joe_Biden", X:46}},{attributes:{WORKERS_ASSIGNED:"Donald_Trump", X:45}},{attributes:{WORKERS_ASSIGNED:"Barack_Obama", X:44}},{attributes:{WORKERS_ASSIGNED:"George_W_Bush", X:43}},{attributes:{WORKERS_ASSIGNED:"Bill_Clinton", X:42}},{attributes:{WORKERS_ASSIGNED:"George_H_W_Bush", X:41}},{attributes:{WORKERS_ASSIGNED:"Ronald_Reagan", X:40}}]}
//fs = Featureset(Text(fs))

// load your Layer
var fs = FeaturesetByPortalItem(...)

// Copy the schema of your layer into a new emppty Featureset
var fs_schema = Schema(fs)
var out_fs = {
    geometryType: fs_schema.geometryType,
    fields: fs_schema.fields,
    features: []
}

// loop over your Layer's features
for(var f in fs) {
    // get the attributes
    var att = Dictionary(Text(f)).attributes
    // replace underscores
    att.WORKERS_ASSIGNED = Replace(att.WORKERS_ASSIGNED, "_", " ")
    // append the edited feature to the output featureset
    Push(out_fs.features, {geometry: Geometry(f), attributes: att})
}

// return the output featureset
return Featureset(Text(out_fs))

 

Be sure that you actually load your Layer in line 6. Using the FeaturesetByPortalItem function, you also can limit the fields you want to import.

This is my test input

JohannesLindner_0-1682550694724.png

 

And the output it created

JohannesLindner_1-1682550746847.png

 


Have a great day!
Johannes
0 Kudos