Select to view content in your preferred language

Arcade expression for returning optional fields

601
6
Jump to solution
11-08-2023 12:04 PM
Labels (1)
leahmaps
Frequent Contributor

Hello!

I need a bit of help with an arcade expression and optional fields. 

I am running a survey 123 where people can submit a name/contact information (phone AND/OR email), or choose not to and stay anonymous. I want to set up the expression to return either

1) a phone number
2) an email
3) both a phone number and an email
4) neither 

While I can write IIF statements to find if the field is empty or not, I am not sure how to write statements that will return one or another, both, or neither. Has anyone done something like this before and been  able to provide guidance?

Here is a simple flow chart to help get an idea of returns and show some potential outputs.

Drawing6.png

Thank you in advance!

0 Kudos
1 Solution

Accepted Solutions
KenBuja
MVP Esteemed Contributor

Here's one way to do it:

var name;// = 'Jane Doe';
var phone;// = '999-999-9999'
var email;// = 'Joe@email.com';

if (IsEmpty(name)) name = 'Anonymous';
if (IsEmpty(phone)){ 
  iif(IsEmpty(email), `${name}, no contact information provided`, `${name}: ${email}`)
} else {
  iif(IsEmpty(email), `${name}: ${phone}`, `${name}: ${phone}, ${email}`)
}                

 

 

 

 

View solution in original post

0 Kudos
6 Replies
KenBuja
MVP Esteemed Contributor

Here's one way to do it:

var name;// = 'Jane Doe';
var phone;// = '999-999-9999'
var email;// = 'Joe@email.com';

if (IsEmpty(name)) name = 'Anonymous';
if (IsEmpty(phone)){ 
  iif(IsEmpty(email), `${name}, no contact information provided`, `${name}: ${email}`)
} else {
  iif(IsEmpty(email), `${name}: ${phone}`, `${name}: ${phone}, ${email}`)
}                

 

 

 

 

0 Kudos
leahmaps
Frequent Contributor

Worked! Here is what I did with the dynamic content/name so that if nothing was provided it returned anonymous

var name = IIf(IsEmpty($feature.full_name) == True, 'Anonymous', $feature.full_name);
var phone= $feature.phone_number;
var email = $feature.email;


if (IsEmpty(phone)){ 
  iif(IsEmpty(email), `${name}, no contact information provided`, `${name}: ${email}`)
} else {
  iif(IsEmpty(email), `${name}: ${phone}`, `${name}: ${phone}, ${email}`)
}
0 Kudos
KenBuja
MVP Esteemed Contributor

I had made a subsequent edit to my script after posting which takes care of the empty name. In your case, you can just use

var name = IIf(IsEmpty($feature.full_name), 'Anonymous', $feature.full_name);

 

0 Kudos
leahmaps
Frequent Contributor

Cool, two ways to do it. This is much more of an intense script than I can write. If I may ask, what is the purpose of the ` in the script? It is not acting as a quotation mark but make it so they are not necessary for text.

 

0 Kudos
KenBuja
MVP Esteemed Contributor

That's a template literal. It allows you to embed expressions.

`${name}: ${email}` //with template literals
name + ': ' + email //using regular quotes

 

leahmaps
Frequent Contributor

You are awsome! Thanks much for your help 😊

0 Kudos