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.
Thank you in advance!
Solved! Go to Solution.
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}`)
}
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}`)
}
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}`)
}
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);
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.
That's a template literal. It allows you to embed expressions.
`${name}: ${email}` //with template literals
name + ': ' + email //using regular quotes
You are awsome! Thanks much for your help 😊