How to store arcade expression as variable for use in another expression

334
4
Jump to solution
08-27-2021 02:30 AM
Labels (1)
DataOfficer
Regular Contributor

I am using Arcade to customise my popups in ArcGIS Online. I have a scenario where I am wanting to run one Arcade expression to return a value, and then use that value in another expression. However it is not quite working out and I am probably missing something obvious. Here is my expression:

var Status = $feature.RefStatus
var label = $feature.RefStatus; if (Status =='Ants present under artificial cover object'){
return 'Artificial cover object status: ';
} else if (Status=='Artificial cover object not found') {
return 'Artificial cover object status: ';
} else if (Status=='Artificial cover object not checked'){
return 'Artificial cover object status: ';
} else if (Status=='Current'){
return 'Waterbody status: ';
} else if (Status=='Empty'){
return 'Waterbody status: ';
} else if (Status=='Inaccessible'){
return 'Waterbody status: ';
} else if (Status=='Polluted'){
return 'Waterbody status: ';
} else if (Status=='Filled'){
return 'Waterbody status: ';
} else {
return '';
}

IIF(isEmpty($feature.RefStatus), "", label)

I want the first expression to store either the value 'Artificial cover object status: ' or 'Waterbody status: ' based on the contents of the RefStatus field, and the second expression to return the output of the first expression if the field is not empty.

Is there a way of storing an expression as a variable for use in a subsequent expression?

I have tried creating the expression separately and then referencing that expression in the 2nd one (e.g.  IIF(isEmpty($feature.RefStatus), "", {expression/expr6}) but this throws an error. I have also tried assigning the first expression to a variable (e.g. var label  = if (Status....) but this also does not work.

 

Thanks,
Rob

0 Kudos
1 Solution

Accepted Solutions
JohannesLindner
Regular Contributor

You just have to order your code differently:

var Status = $feature.RefStatus

// exit early if RefStatus is null
if(IsEmpty(Status)) {
  return ''
}

var cover_status = [
  'Ants present under artificial cover object',
  'Artificial cover object not found',
  'Artificial cover object not checked'
]
var water_status = [
  'Current',
  'Empty',
  'Inaccessible',
  'Polluted'
  'Filled'
]
if(Includes(cover_status, Status)) {
  return 'Artificial cover object status: ' + Status
}
if(Includes(water_status, Status)) {
  return 'Waterbody status: ' + Status
}
return ''

// for older Arcade versions (< Arcade 1.12):
var status_type = ''
if (Status=='Ants present under artificial cover object' || Status=='Artificial cover object not found' || Status=='Artificial cover object not checked') {
  return 'Artificial cover object status: ' + Status
}
if (Status=='Current' || Status=='Empty' || Status=='Inaccessible' || Status=='Polluted' || Status=='Filled') {
  return 'Waterbody status: ' + Status
}
return ''

Have a great day!
Johannes

View solution in original post

4 Replies
RPGIS
by
Regular Contributor

I do not think that this is possible simply because arcade expressions, when used for popups, only work for the particular layer that it corresponds to. You could try setting up the arcade expression to work with other features/geometries to see when one feature changes, then the arcade expression i....

This is my best guess for when it comes to creating custom popups using arcade expressions. Perhaps someone else in the community or Esri can give you further insight.

 

0 Kudos
DataOfficer
Regular Contributor

Sorry I should have been clearer. The expressions are pulling information from the same layer. 

0 Kudos
JohannesLindner
Regular Contributor

You just have to order your code differently:

var Status = $feature.RefStatus

// exit early if RefStatus is null
if(IsEmpty(Status)) {
  return ''
}

var cover_status = [
  'Ants present under artificial cover object',
  'Artificial cover object not found',
  'Artificial cover object not checked'
]
var water_status = [
  'Current',
  'Empty',
  'Inaccessible',
  'Polluted'
  'Filled'
]
if(Includes(cover_status, Status)) {
  return 'Artificial cover object status: ' + Status
}
if(Includes(water_status, Status)) {
  return 'Waterbody status: ' + Status
}
return ''

// for older Arcade versions (< Arcade 1.12):
var status_type = ''
if (Status=='Ants present under artificial cover object' || Status=='Artificial cover object not found' || Status=='Artificial cover object not checked') {
  return 'Artificial cover object status: ' + Status
}
if (Status=='Current' || Status=='Empty' || Status=='Inaccessible' || Status=='Polluted' || Status=='Filled') {
  return 'Waterbody status: ' + Status
}
return ''

Have a great day!
Johannes

View solution in original post

DataOfficer
Regular Contributor

Amazing, thank you so much for this. This is much more elegant as well.
It wasn't returning the Artificial cover object status: text but it turned out that this was due to the fact that 3 of the status' were listed as domains. 
Changing the first line to 

var Status = DomainName($feature,"RefStatus")

resolved the issue.

Also just had to fix a missing comma on line 17 after 'Polluted'.

0 Kudos