Arcade Labeling based on NULL or NOT NULL values

4097
6
Jump to solution
04-27-2021 11:47 AM
Labels (1)
Brian_McLeer
Occasional Contributor II

We have a description field in our address feature class and one of the values is 'BUSINESS'. We have two other fields, Unit Number and Building Number. In working to migrate labeling language from VB Script to Arcade, we would like to label on the following conditions:

Applies to all where the description is BUSINESS

 - If the building number is not null, label the building number and not the situs number.

 - if the unit number is not null, label the unit number and not the situs number. 

 - If the building and unit numbers are both NULL, label the situs number

-  Not often, but if both unit number and building number are both not null, label both unit number and building number, do not label situs number

Below is the code that I currently am working with. The portion that pertains to my question is where the description is 'BUSINESS'.

 

var d = $feature.Description
var s = $feature.SitusNum
var u = $feature.UnitNum
var b = $feature.BuildingNum

if(d == 'SINGLE'){
    return s
} else if(d == 'TOWNHOME'){
    return s
} else if(d == 'ACCESSORY DWELLING'){
    if(IsEmpty(u)){
        return s}
    else{
        return s + ' - ' + u}
} else if(d == 'DUPLEX'){
    if(IsEmpty(u)){
        return s}
    else{
        return s + ' - ' + u}
} else if(d == 'MANUFACTURED'){
    if(IsEmpty(u)){
        return s}
    else{
        return s + ' - ' + u}
} else if(d == 'CONDO'){
    if(IsEmpty(u)){
        return s}
    else{
        return u}
} else if(d == 'MULTIFAMILY'){
    if(IsEmpty(u)){
        return s}
    else{
        return u}
} else if(d == 'BUSINESS'){
    if(IsEmpty(b)){
        return s}
    else{
        return b}
} else if(d == 'BUSINESS'){
    if(IsEmpty(u)){
        return s}
    else{
        return u}
} else if(d == 'CARE FACILITY'){
    if(IsEmpty(u)){
        return s}
    else{
        return u}
} else if(d == 'PRIVATE PARK'){
    return s
} else if(d == 'PUBLIC FACILITY'){
    if(IsEmpty(u)){
        return s}
    else{
        return u}
} else if(d == 'GARAGE'){
    if(IsEmpty(u)){
        return s}
    else{
        return u}
} else if(d == 'PARKING GARAGE'){
    return s
} else if(d == 'STORAGE UNIT'){
    if(IsEmpty(u)){
        return s}
    else{
        return u}
}

 

 

Brian
0 Kudos
1 Solution

Accepted Solutions
KenBuja
MVP Honored Contributor

Hmmm...I tested a version of this in Pro using my data and it performed as expected.

arcade.png

What happens if you start by adding one When condition at a time?

View solution in original post

6 Replies
DavidPike
MVP Frequent Contributor

The issue is that a return statement ends a function (moves on to the next feature).

You might have better luck creating something like var result = '' 

and then creating a result string, at the end of your conditionals you just have:

return result.

Or something like:

 

 

 

 

else if(d == 'BUSINESS'){
    if(IsEmpty(b) == False && IsEmpty(u) == False){
        return (b + ', ' + u)}
    else if(IsEmpty(b) == False){
        return b}
    else if(IsEmpty(u) == False){
        return u}
    else if(IsEmpty(b) && IsEmpty(u)){
        return s}
}

 

 

 

 

 

KenBuja
MVP Honored Contributor

It may be easier to visualize this using the When function and consolidating the conditions with the same responses using the || (or) operator (this is just showing some of them).

The building response was a little more complex, but I think this covers all bases. This is how I tested it in the Arcade playground.

 

var d = 'BUSINESS'
var s = 'SitusNum'
var u = 'UnitNum' //UnitNum
var b = 'BuildingNum' //BuildingNum

return When(d == 'SINGLE' || d == 'TOWNHOME', s,
            d == 'DUPLEX', IIf(IsEmpty(u), s, s + ' - ' + u),
            d == 'BUSINESS', 
              IIf(IsEmpty(b) && IsEmpty(u), s, 
                IIf(!IsEmpty(b) && !IsEmpty(u), b + ' - ' + u,
                  IIf(!IsEmpty(b), b, IIf(!IsEmpty(u), u, null)))),
           null)

 

 

KenBuja
MVP Honored Contributor

This is even cleaner:

var d = 'BUSINESS'
var s = 'SitusNum'
var u = 'UnitNum' //UnitNum
var b = 'BuildingNum' //BuildingNum

return When(d == 'SINGLE' || d == 'TOWNHOME', s,
            d == 'DUPLEX', IIf(IsEmpty(u), s, s + ' - ' + u),
            d == 'BUSINESS', 
              IIf(!IsEmpty(b) && !IsEmpty(u), b + ' - ' + u,
                IIf(!IsEmpty(b), b, 
                  IIf(!IsEmpty(u), u, s)
                )
              ),
           null)

 

Brian_McLeer
Occasional Contributor II

Thank you @KenBuja , 

So I get the below error when trying to update on my end. It is a direct copy and paste, all I did was keep the '$feature.' in front of each variable. 

 

image.PNG

Brian
0 Kudos
KenBuja
MVP Honored Contributor

Hmmm...I tested a version of this in Pro using my data and it performed as expected.

arcade.png

What happens if you start by adding one When condition at a time?

Brian_McLeer
Occasional Contributor II

Thank you @KenBuja 

I tried again and I got the below to work based on your feedback. I also created a new label class just for where the description is business so I was not trying to do so much in one label class. Thank you very much for your help!

var d = $feature.Description
var s = $feature.SitusNum
var u = $feature.UnitNum
var b = $feature.BuildingNum

return When(d == 'BUSINESS', 
              IIf(!IsEmpty(b) && !IsEmpty(u), b + ' - ' + u,
                IIf(!IsEmpty(b), b, 
                  IIf(!IsEmpty(u), u, s)
                )
              ),
           null)

Brian
0 Kudos