If statement and add data to field

1050
8
11-05-2018 11:07 AM
joerodmey
MVP Alum

What is the structure of an if/else statement in Survey123?

I'm trying to check the username of the user, if true then grab the answer from a select one question, if not then just grab the username from the username question type. No luck so far

0 Kudos
8 Replies
NickDierks1
Occasional Contributor II

From the documentation:

if(condition, a, b)

Where:

  • condition is the statement to be evaluated
  • a is the result if true
    • ${question}
  • b is the result if false (else condition)
    • property('username')

The linked page has plenty of functions and operators to help you set up your if condition. If you're still stuck, describe exactly what you're trying to check and how, and someone can try to help you further.

0 Kudos
joerodmey
MVP Alum

I have this:

if(${Name}='Internal', ${Call_In_Name}, ${Name})

Where Name is the name of the account

Call_In_Name is a select one answer

This is going into a textbox. I want to grab Call_In_Name if the username (Name) is equal to "Internal", else just grab the username (Name) and put it into the textbox

There is also a requirement to overwrite what's in textbox if the username is already in there and the user clicks on the select one question

0 Kudos
NickDierks1
Occasional Contributor II

So if I understand correctly, it sounds like you have two conditions to trigger ${Call_In_Name} being used in the textbox:

  • When ${Name}='Internal'
  • When an option in ${Call_In_Name} is selected

For the second condition, you state "if the username is already in there and the user clicks on the select one question." Are there any situations where ${Call_In_Name} is selected, but you still want to use ${Name} instead? If not, then this should probably do what you need:

if(string-length(${Call_In_Name})>0, ${Call_In_Name}, ${Name})

This checks to see if ${Call_In_Name} has been answered, and if so, grabs that value. If not, ${Name} is used instead.

And just in case someone has 'Internal' for ${Name} but tries to skip answering ${Call_In_Name}, you can put this condition in the required column for ${Call_In_Name}:

if(${Name}='Internal')

(note, conditionally required questions apparently don't work in web forms)

With this setup, the textbox will always equal the ${Call_in_Name} as long as that question is answered. If instead you want to use ${Name} all the time unless A) it's 'Internal' or B) the user wants to optionally override it, you'll need to do that with at least another question and some additional statements.

0 Kudos
joerodmey
MVP Alum

To provide some info to your response:

  • There will be no situations where ${Call_In_Name} is selected, but you still want to use ${Name} instead
  • Using the following method: if(string-length(${Call_In_Name})>0, ${Call_In_Name}, ${Name}) doesn't seem to overwrite the data already in the textbox (which would be the username)
  • The select one question will be hidden to all users except to myself (admin account). Will using this approach and relevant criteria cause the fields to be blank?
  • Yes name should be used all the time unless I (admin) wants to override but not capture my AGOL login info and I then see the select one question and override the username

The workflow is the following:

  • staff member submits a request using the survey
  • the process continues through to be dispatched and work completed. A second survey is employed to edit and dispatch crews
  • But if a staff member doesn't use the survey and calls me, I want to be able to add in the request on their behalf
  • This creates the need for a workaround so that my AGOL username isn't shown in the data
0 Kudos
NickDierks1
Occasional Contributor II

In that use case, your initial if/then statement should work just fine (I'm assuming your username is 'Internal'). Why is there anything to overwrite in the textbox? It should start blank, and populate only via the if/then calculation:

namerelevantcalculation
Nameproperty('username')
Call_In_Name${Name}='Internal'
[Textbox]if(${Name}='Internal', ${Call_In_Name}, ${Name})

Are you trying to recalculate ${Name}, or do you have something set up in the default column for the textbox question? Are you able to share your csv file?

Incidentally, you could also use the coalesce() function and get rid of the ${Name} line:

namerelevantcalculation
Call_In_Nameproperty('username')='Internal'
[Textbox]coalesce(${Call_In_Name}, property('username'))

This will always input the AGOL username unless there's an answer selected in ${Call_In_Name}.

0 Kudos
joerodmey
MVP Alum

 That's perfect thanks!

The property('username')='Internal' formula is giving me an error.

Also with ${Name}='Internal' as the relevant field, when a user logins in they don't see the fields, which is great. But when they submit data it doesn't grab their username or email from my pulldata function.

Another question though. Can you put an if on pulldata()? I want to check if the user is me (the internal account), if true then grab the email from a csv file I have as it contains emails for all users...since I can't pull from the AGOL account as it would pull my email and not the user's email.

0 Kudos
NickDierks1
Occasional Contributor II

I'm not sure what's going on, then--the formulas are working fine in my test so far (see attached xlsx form). Is your username actually 'Internal'? Because what's in those quotes needs to match your username exactly for that formula to work.

As for the pulldata(), yes, you can use that as a result in an if/then statement or even in the coalesce() function. You could follow this example and put it where you like:

pulldata('ContactList', 'Email', 'UserName', property('username'))

The survey I attached uses two of these pulldata() functions within a coalesce() function, pulling the email of either the logged in user or the user selected in ${Call_In_Name}. Try it out, and make sure to save the attached ContactList.csv to the media folder to see it in action. Note that for this to work, the Call_In_Name choices have to match the other usernames exactly, including being case sensitive.

If you're still having trouble, try sharing your xlsx form (my email is in my profile if you don't want to attach your survey here). It's possible there's a typo in some formula or other that's throwing things off.

0 Kudos
joerodmey
MVP Alum

Still not working. Email sent containing xlsx

0 Kudos