Hello! I am trying to use dynamic data attachments to auto-populate a survey with data from the most recent submission that has the same project name. To do this, I construct a unique identifier (string) using the project location/name and the date from calculating today(). Then I pull in the most recent date using: date(max(instance('same_form')/root/data[project_id =current()/../project_id]/todays_date))
It all works beautifully on the Enketo web form, but the most recent date is showing up as blank when I use the KoboCollect app. The issue seems to be with taking the max of a list using instance(). When I try to print out just the list of previous entry dates on KoboCollect, I get the error: XPath evaluation: type mismatch This field is repeated: instance(same_form)/root/data[10]/todays_date[1];instance(same_form)/root/data[32]/todays_date[1] You may need to use the indexed-repeat() function to specify which value you want.
Is there a way to retrieve the most recent submission with a matching project ID using KoboCollect or will it only work with the web form?
It seems, the problem has nothing to do with the max (or outside date) part, but the instance part. As you can see, also Enketo is showing one single date value only (which will then be the max, of course). I am afraid this is not what your are looking for. It seems similar to a repeat group: As far as I know, a ${repeatGroup} will only show the first value. (You would need to apply a join(…) to combine all repeat values into a string, separated with blanks.)
What is rather strange in your Enketo screen shot: There are 3 different dates! Why?
Further hint: You use today(), in line 6. Take care this will always be recalculated on load/save/edit of the form. Maybe, you need to use once(today). Or set as default? Or ${today} metadata
Dear @barbaraT ,
this error appear because the identifier is repeated,
meaning that, the “project_id” is duplicated in the data , whenever an instance find more than one same value it will throw this error , you have to ensure that the project_id is unique.
Thanks for your suggestions! I ended up using join(…) on the instance(…) and then selecting the last 10 characters using substr(…), which seems to work well enough.
For the Enketo output, I got three different dates because the first one is the first date converted to a string, and the next one is date() applied to that first date string, which converts it to UTC. Then the last one is the max date from whatever Enketo uses on the back end to allow the calculation right on instance().