Pop-up Arcade Expression Help, IIf Statement Not Working

610
6
Jump to solution
01-19-2024 08:50 AM
Labels (1)
AnninaRupe1
New Contributor III

All,

I have been stumped with getting an Arcade expression working! In the end, I would like to have an expression that has a Primary Contact (with address information) and, if present, Secondary Contact (with address information). I have the two working separately, but if there is not a Secondary Contact, it returns a blank line. If there isn't a Secondary Contact I'd like it to just be skipped so there isn't an extra line in the pop-up.

Below is my expression. Lines 1-8 are for the Primary Contact, lines 9-16 are the Secondary Contact, and line 17 is where my problem lies. If the expression returns false, it appears correctly. If the expression returns true, however, nothing is written. I've tried too many different things to list here--help is much appreciated!

var step1Phone = $feature.prim_phone
var step2Phone = FeatureSetByPortalItem(Portal('Removed for Privacy'), 'Removed for Privacy',
0, ['contact_name', 'contact_address1', 'contact_address2', 'contact_city', 'contact_state', 
'contact_zip', 'contact_phone', 'contact_email'], false )
var step3Phone = First(Filter(step2Phone, 'contact_phone=@step1Phone'))
var step4Phone = Concatenate("Primary Contact Name:" + TextFormatting.NewLine + step3Phone.contact_name + " (" + Proper($feature.prim_contact_type) + ")" + TextFormatting.NewLine + 
step3Phone.contact_address1 + (IIf(IsEmpty(step3Phone.contact_address2), '', TextFormatting.NewLine + step3Phone.contact_address2)) + TextFormatting.NewLine + step3Phone.contact_city + 
", " + step3Phone.contact_state + " " + step3Phone.contact_zip + TextFormatting.NewLine + step3Phone.contact_phone + TextFormatting.NewLine + step3Phone.contact_email)
var step5Phone = $feature.sec_phone
var step6Phone = FeatureSetByPortalItem(Portal('Removed for Privacy'), 'Removed for Privacy',
0, ['contact_name', 'contact_address1', 'contact_address2', 'contact_city', 'contact_state', 
'contact_zip', 'contact_phone', 'contact_email'], false )
var step7Phone = First(Filter(step6Phone, 'contact_phone=@step5Phone'))
var step8Phone = Concatenate("Secondary Contact Name:" + TextFormatting.NewLine + step7Phone.contact_name + " (" + Proper($feature.sec_contact_type) + ")" + TextFormatting.NewLine + 
step7Phone.contact_address1 + (IIf(IsEmpty(step7Phone.contact_address2), '', TextFormatting.NewLine + step7Phone.contact_address2)) + TextFormatting.NewLine + step7Phone.contact_city + 
", " + step7Phone.contact_state + " " + step7Phone.contact_zip + TextFormatting.NewLine + step7Phone.contact_phone + TextFormatting.NewLine + step7Phone.contact_email)
return IIf(IsEmpty(step7Phone), step4Phone, step4Phone + TextFormatting.NewLine + TextFormatting.NewLine + step8Phone)

  

0 Kudos
1 Solution

Accepted Solutions
JohannesLindner
MVP Frequent Contributor

You either have an error in the construction of the secondary contact info or the Filter() errors out when you search for null.

Try something like this:

 

// load the data
var fs = Featureset({
  fields: [{name: "contact_name", type: "esriFieldTypeString"}, {name: "contact_phone", type: "esriFieldTypeString"}, {name: "contact_address1", type: "esriFieldTypeString"}, {name: "contact_address2", type: "esriFieldTypeString"}, {name: "contact_city", type: "esriFieldTypeString"}, {name: "contact_state", type: "esriFieldTypeString"}, {name: "contact_zip", type: "esriFieldTypeString"}, {name: "contact_email", type: "esriFieldTypeString"}, ],
  features: [
    {attributes: {contact_name: "James Bond", contact_address1: "SIS Building", contact_city: "London", contact_state: "United Kingdom", contact_phone: "007", contact_zip: "99999", contact_email: "bond@sis.gov.uk"}},
    {attributes: {contact_name: "Sherlock Holmes", contact_address1: "221B Baker Street", contact_address2: "First Floor", contact_city: "London", contact_state: "United Kingdom", contact_phone: "98765", contact_zip: "11111", contact_email: "sherlock@holmes-pi.co.uk"}},
  ],
  geometryType: ""
})
//var fs = FeaturesetByPortalItem(...)

// return a default value if no primary contact is given
var prim_phone = "007"//$feature.prim_phone
if(prim_phone == null) { return "No contact information given." }
// find the primary contact
var primary = First(Filter(fs, "contact_phone = @prim_phone"))
// return a default value if no contact was found
if(primary == null) { return "No contact information found." }

// assemble the primary contact information
var prim_type = Proper("secret service agent")//Proper($feature.prim_contact_type)
var prim_address = primary.contact_address1 + IIf(IsEmpty(primary.contact_address2), "", TextFormatting.NewLine + primary.contact_address2)
var prim_contact_info = `Primary Contact Name: ${primary.contact_name} (${prim_type})
${prim_address}
${primary.contact_city}
${primary.contact_state} ${primary.contact_zip}
${primary.contact_phone}
${primary.contact_email}`


// find the secondary contact
// return the primary contact info if no secondary contact was found
var sec_phone = "98765"//$feature.sec_phone
if(sec_phone == null) { return prim_contact_info }
var secondary = First(Filter(fs, "contact_phone = @sec_phone"))
if(secondary == null) { return prim_contact_info }

// assemble the secondary contact information
var sec_type = Proper("private investigator")//Proper($feature.sec_contact_type)
var sec_address = secondary.contact_address1 + IIf(IsEmpty(secondary.contact_address2), "", TextFormatting.NewLine + secondary.contact_address2)
var sec_contact_info = `Secondary Contact Name: ${secondary.contact_name} (${sec_type})
${sec_address}
${secondary.contact_city}
${secondary.contact_state} ${secondary.contact_zip}
${secondary.contact_phone}
${secondary.contact_email}`


// return both contact information
return `${prim_contact_info}

${sec_contact_info}`

 

 

Primary Contact Name: James Bond (Secret Service Agent)
SIS Building
London
United Kingdom 99999
007
bond@sis.gov.uk

Secondary Contact Name: Sherlock Holmes (Private Investigator)
221B Baker Street
First Floor
London
United Kingdom 11111
98765
sherlock@holmes-pi.co.uk

Have a great day!
Johannes

View solution in original post

0 Kudos
6 Replies
JohannesLindner
MVP Frequent Contributor

 If the expression returns true, however, nothing is written.

The condition is true when step7Phone is null. In that case, you try to call attributes (eg contact_name) on a null (non-existing) object in lines 14-16, which results in an error, the expression doesn't return anything.

Instead, you want to check if step7Phone is null before you use it. Something like this:

var step7Phone = First(Filter(...))

// if no secondary contact info is found, only return the primary info
if(step7Phone == null) { return step4Phone }

// create the secondary info
var step8Phone = ...

// return both primary and secondary info
return step4Phone + TextFormatting.NewLine + TextFormatting.NewLine + step8Phone

Have a great day!
Johannes
AnninaRupe1
New Contributor III

Thanks, Johannes. That makes sense, but it seems I'm still doing something wrong. If there are both Primary and Secondary, both "step4Phone" and "step8Phone" displays (as I want). However, if there is just Primary, nothing displays. Any ideas? I feel like there should be an "else" statement, but I tried that and it didn't work.

var step1Phone = $feature.prim_phone
var step2Phone = FeatureSetByPortalItem(Portal('Removed for Privacy'), 'Removed for Privacy',
0, ['contact_name', 'contact_address1', 'contact_address2', 'contact_city', 'contact_state', 
'contact_zip', 'contact_phone', 'contact_email'], false )
var step3Phone = First(Filter(step2Phone, 'contact_phone=@step1Phone'))
var step4Phone = Concatenate("Primary Contact Name:" + TextFormatting.NewLine + step3Phone.contact_name + " (" + Proper($feature.prim_contact_type) + ")" + TextFormatting.NewLine + 
step3Phone.contact_address1 + (IIf(IsEmpty(step3Phone.contact_address2), '', TextFormatting.NewLine + step3Phone.contact_address2)) + TextFormatting.NewLine + step3Phone.contact_city + 
", " + step3Phone.contact_state + " " + step3Phone.contact_zip + TextFormatting.NewLine + step3Phone.contact_phone + TextFormatting.NewLine + step3Phone.contact_email)
var step5Phone = $feature.sec_phone
var step6Phone = FeatureSetByPortalItem(Portal('Removed for Privacy'), 'Removed for Privacy',
0, ['contact_name', 'contact_address1', 'contact_address2', 'contact_city', 'contact_state', 
'contact_zip', 'contact_phone', 'contact_email'], false )
var step7Phone = First(Filter(step6Phone, 'contact_phone=@step5Phone'))
if(step7Phone==null){return step4Phone}

var step8Phone = Concatenate("Secondary Contact Name:" + TextFormatting.NewLine + step7Phone.contact_name + " (" + Proper($feature.sec_contact_type) + ")" + TextFormatting.NewLine + 
step7Phone.contact_address1 + (IIf(IsEmpty(step7Phone.contact_address2), '', TextFormatting.NewLine + step7Phone.contact_address2)) + TextFormatting.NewLine + step7Phone.contact_city + 
", " + step7Phone.contact_state + " " + step7Phone.contact_zip + TextFormatting.NewLine + step7Phone.contact_phone + TextFormatting.NewLine + step7Phone.contact_email)

return step4Phone + TextFormatting.NewLine + TextFormatting.NewLine + step8Phone

 

0 Kudos
JohnEvans6
Occasional Contributor

Any luck moving your returns to the end in an if/else?

 

if(step7Phone==null)
    {return step4Phone}
else 
    {return step4Phone + TextFormatting.NewLine + TextFormatting.NewLine + step8Phone}

 

After line 6 if you just return step4Phone does it work for everything? Is step7Phone not returning null and instead returning something unhelpful like [' ']?

0 Kudos
AnninaRupe1
New Contributor III

No luck: I put exactly that in the expression and it's giving me the same response:
For those polygons that have both Primary Contact (step3Phone) and Secondary Contact (step7Phone)--that is, they are not Null--the pop-up displays both Primary and Secondary Contact as expected.
For those polygons that only have a Primary Contact (step3Phone)--that is, step7Phone is Null--the pop-up displays only a blank line.

I have gotten both the Primary and Secondary Contact expressions working individually, so I know it's not something in lines 1-18. I would just like the blank line to disappear if there isn't a Secondary Contact, similar to what happens in lines 7 and 17, where if the second address line is Null it just goes on to the City/State/Zip and doesn't place a blank line in the address block.

Appreciate the help...any other suggestions?

0 Kudos
JohannesLindner
MVP Frequent Contributor

You either have an error in the construction of the secondary contact info or the Filter() errors out when you search for null.

Try something like this:

 

// load the data
var fs = Featureset({
  fields: [{name: "contact_name", type: "esriFieldTypeString"}, {name: "contact_phone", type: "esriFieldTypeString"}, {name: "contact_address1", type: "esriFieldTypeString"}, {name: "contact_address2", type: "esriFieldTypeString"}, {name: "contact_city", type: "esriFieldTypeString"}, {name: "contact_state", type: "esriFieldTypeString"}, {name: "contact_zip", type: "esriFieldTypeString"}, {name: "contact_email", type: "esriFieldTypeString"}, ],
  features: [
    {attributes: {contact_name: "James Bond", contact_address1: "SIS Building", contact_city: "London", contact_state: "United Kingdom", contact_phone: "007", contact_zip: "99999", contact_email: "bond@sis.gov.uk"}},
    {attributes: {contact_name: "Sherlock Holmes", contact_address1: "221B Baker Street", contact_address2: "First Floor", contact_city: "London", contact_state: "United Kingdom", contact_phone: "98765", contact_zip: "11111", contact_email: "sherlock@holmes-pi.co.uk"}},
  ],
  geometryType: ""
})
//var fs = FeaturesetByPortalItem(...)

// return a default value if no primary contact is given
var prim_phone = "007"//$feature.prim_phone
if(prim_phone == null) { return "No contact information given." }
// find the primary contact
var primary = First(Filter(fs, "contact_phone = @prim_phone"))
// return a default value if no contact was found
if(primary == null) { return "No contact information found." }

// assemble the primary contact information
var prim_type = Proper("secret service agent")//Proper($feature.prim_contact_type)
var prim_address = primary.contact_address1 + IIf(IsEmpty(primary.contact_address2), "", TextFormatting.NewLine + primary.contact_address2)
var prim_contact_info = `Primary Contact Name: ${primary.contact_name} (${prim_type})
${prim_address}
${primary.contact_city}
${primary.contact_state} ${primary.contact_zip}
${primary.contact_phone}
${primary.contact_email}`


// find the secondary contact
// return the primary contact info if no secondary contact was found
var sec_phone = "98765"//$feature.sec_phone
if(sec_phone == null) { return prim_contact_info }
var secondary = First(Filter(fs, "contact_phone = @sec_phone"))
if(secondary == null) { return prim_contact_info }

// assemble the secondary contact information
var sec_type = Proper("private investigator")//Proper($feature.sec_contact_type)
var sec_address = secondary.contact_address1 + IIf(IsEmpty(secondary.contact_address2), "", TextFormatting.NewLine + secondary.contact_address2)
var sec_contact_info = `Secondary Contact Name: ${secondary.contact_name} (${sec_type})
${sec_address}
${secondary.contact_city}
${secondary.contact_state} ${secondary.contact_zip}
${secondary.contact_phone}
${secondary.contact_email}`


// return both contact information
return `${prim_contact_info}

${sec_contact_info}`

 

 

Primary Contact Name: James Bond (Secret Service Agent)
SIS Building
London
United Kingdom 99999
007
bond@sis.gov.uk

Secondary Contact Name: Sherlock Holmes (Private Investigator)
221B Baker Street
First Floor
London
United Kingdom 11111
98765
sherlock@holmes-pi.co.uk

Have a great day!
Johannes
0 Kudos
AnninaRupe1
New Contributor III

Oooh, success! After looking at your expression, I modified mine a bit to remove the duplicate lines (e.g. I didn't need to run the FeatureSetByPortalItem twice) and to check if the Secondary Contact existed before I did all of the Secondary Contact information gathering. It worked! I copied my expression here just in case it will help someone else. Appreciate your input!

var primPhone = $feature.prim_phone
var secPhone = $feature.sec_phone
var fs = FeatureSetByPortalItem(Portal(Removed for Privacy), 'Removed for Privacy',
0, ['contact_name', 'contact_address1', 'contact_address2', 'contact_city', 'contact_state', 
'contact_zip', 'contact_phone', 'contact_email'], false )
var primPhoneID = First(Filter(fs, 'contact_phone=@primPhone'))
var primPhoneConcat = Concatenate("Primary Contact Name:" + TextFormatting.NewLine + primPhoneID.contact_name + " (" + Proper($feature.prim_contact_type) + ")" + TextFormatting.NewLine + 
primPhoneID.contact_address1 + (IIf(IsEmpty(primPhoneID.contact_address2), '', TextFormatting.NewLine + primPhoneID.contact_address2)) + TextFormatting.NewLine + primPhoneID.contact_city + 
", " + primPhoneID.contact_state + " " + primPhoneID.contact_zip + TextFormatting.NewLine + primPhoneID.contact_phone + TextFormatting.NewLine + primPhoneID.contact_email)
if(secPhone==null) {return primPhoneConcat}
var secPhoneID = First(Filter(fs, 'contact_phone=@secPhone'))
var secPhoneConcat = ("Secondary Contact Name:" + TextFormatting.NewLine + secPhoneID.contact_name + " (" + Proper($feature.sec_contact_type) + ")" + TextFormatting.NewLine + 
secPhoneID.contact_address1 + (IIf(IsEmpty(secPhoneID.contact_address2), '', TextFormatting.NewLine + secPhoneID.contact_address2)) + TextFormatting.NewLine + secPhoneID.contact_city + 
", " + secPhoneID.contact_state + " " + secPhoneID.contact_zip + TextFormatting.NewLine + secPhoneID.contact_phone + TextFormatting.NewLine + secPhoneID.contact_email)
return primPhoneConcat + TextFormatting.NewLine + TextFormatting.NewLine + secPhoneConcat

 

0 Kudos