Add a unique constraint to a field in Survey123

1521
7
04-07-2022 11:52 PM
RENDANIMUDAU
New Contributor III

Good day. I have created a survey123 to capture projects with Unique numbers and i would like to find out how to add a unique constraint to a field to avoid duplicates entries? 

 

Best regards 

Tags (1)
7 Replies
ChristopherCounsell
MVP Regular Contributor

This works for hosted feature layers:

https://doc.arcgis.com/en/arcgis-online/manage-data/add-unique-constraint.htm

I do not think this is possible as Survey123 is primarily designed to work offline; it won't check if the same unique value has been entered on a draft survey still offline on another device. You would get an error on submission when the feature edit is applied and the unique field constraint is encountered.

 

You could rely on the GlobalID which is created after submission. You could also create a hidden string to randomly generate an ID value. Some discussion on that here:

https://community.esri.com/t5/arcgis-survey123-questions/generate-unique-id-on-repeats-that-don-t-ch...

You could then use these IDs, editor tracking properties, date fields etc to create the unique IDs on the service after submission

RENDANIMUDAU
New Contributor III

Thank you so much for the insight and its much appreciated.

0 Kudos
ChristopherCounsell
MVP Regular Contributor

Glad to hear!

If it's useful please give kudos.

If it's the answer, please mark as such. You can wait to see if others want to comment.

Thanks.

 

IsmaelChivite
Esri Notable Contributor

Hi. To add to @ChristopherCounsell 's answer:

This blog post describes some techniques to create unique values. It may help.

If you survey is meant to work online, you can also use a custom JS function to query your survey layer and figure out if there is already a record with the same project number. The code is here:

https://github.com/IsmaelInRedlands/Survey123-Tricks-of-the-Trade/blob/main/getRecord

The custom JS function would be ideal for your constraint (again, for online workflows only).

For details on using custom JS functions, check this blog. Be aware that using custom JS functions is not allowed in public surveys.  We plan to include the getRecord JS function above as a built-in function within Survey123 later this year. This will allow you to use this type of query in public surveys without the custom JS function limitations.   

EricaNova
Occasional Contributor

Hi @IsmaelChivite , I see that there are now a lot of options for using getRecord type queries as built-in functions (here, https://doc.arcgis.com/en/survey123/desktop/create-surveys/xlsformformulas.htm). 

I wanted to therefore revisit this question. I'm designing an online-only survey to be used by only a handful of individuals.

For question "specimen_number", I'd like Survey123 to check to see if specimen_number the user enters is already present in that field. This would act as a constraint, with a custom constraint message.

Is this now possible to implement using variations of the pulldata feature? Or, will I need to use a custom JS function like the one available here? https://github.com/IsmaelInRedlands/Survey123-Tricks-of-the-Trade/blob/main/getRecord

I can't find an example of how to implement the custom script above so I'm a little lost. 

0 Kudos
EricaNova
Occasional Contributor

I wasn't able to figure out how to use the **getRecord** script above, but I altered the code from here, https://community.esri.com/t5/arcgis-survey123-questions/stop-survey123-entry-if-duplicate-project-i...  to do a check for duplicate values. That solution seems a bit cumbersome (5 fields); also, I cannot edit the record without the code forcing my user to enter a different test_id value... so if there is a more elegant solution, I'm all ears!

0 Kudos
ShelbyZelonisRoberson
Occasional Contributor III

I need to do something similar; check for duplicates in a non-hosted feature service used by Survey123 while someone is filling out the survey. I can't use a javascript function because the survey is public. @IsmaelChivite any ideas?? 

0 Kudos