Prepopulate answers in a repeat with last values

788
12
08-31-2021 02:40 AM
PaoloBasile_ST
New Contributor

Hello everyone,
I can't seem to create a Survey where the repeat values are prepopulated with the same values as the last record inserted in the repeat (I'm also using the Inbox option).

Let me explain:
I have a form with a geopoint and a repeat that contains two values: date and note

Geopoint
Repeat
-date
-note
end repeat

Image1.JPG

The surveyor has to find the nearest point to him (use Inbox) and has to insert a new repeat record with the date calculated automatically (I know how to do it) and a new note whose value must not be empty but must be prepopulated by the value of the previous note (I don't know how to do it).

I tried using the indexed-repeat () function but I get a cyclic error.

Image2.JPG

Do you have any suggestions?

Thank you in advance for your attention,

 

Paolo

 

0 Kudos
12 Replies
DougBrowning
MVP Notable Contributor

I have been working on this for over 3 years now and have posted on this a bunch of times.  The way you posted will never work as it is a loop.  When 123 displays a page each element has a name so you can't just say give Field A the value of Field A.  I was really hoping when indexed-repeat came out it was smart enough to handle this but no.  indexed-repeat above you can get the value in a different field but not the same field.

The only one I got to work was with javascript.  For me my repeat is too big and so this crashed the tablet.  But it may work for you.

pulldata is 

pulldata("@javascript", "functions.js", "sameaslast", ${repeat name here}, current repeat number here, "field name")

javascript is 

function sameaslast(repeat, position, fieldname) {
	if (position > 1){
		return repeat[position - 2][fieldname];

	}
}

 

My crews ask me for this every single year repeatedly.  I have prob tried 10 diff ways.  Then this one broke for a diff reason.  If anyone has got anything else to try I would really love to hear it!

For you I would also consider using Field Maps for the main point then have each new visit a form.  Use a relationship to tie them all together.  This way they get all the spatial info.  Then you can just pass the note from Field Maps to 123 using the URL parms.  This also gives you full spatial, editor tracking, etc for each visit not just the last one.  It also protects the main point for accidental changes.  This is what we do and it works great.  We can see if they say logged it under the wrong point once since I can see where they were standing for example.

Just an idea.  Let me know if this works.

PaoloBasile_ST
New Contributor

Thanks Doug,

I suspected that index-repeat wasn't refined enough to understand that even if the field was the same, you were going to copy values from another record.

We are also pondering your suggestions with javascript and Field Map and will let you know if we use them.

We may have come up with a more low-level and much "dirtier" solution that has advantages and a big disadvantage.

Instead of making a model like:

Geopoint
Repeat
-date
-note
end repeat

We were thinking of doing a model like this:

-Geopoint

-lastDate

-lastNote

Repeat
-date (hidden, calculate from lastDate)
-note (hidden, calculate from lastNote)
-image* (required)

end repeat

(actually we have many more fields to fill in, mine was just an example to simplify the problem).

Basically, the table structure of the Repeat is almost reproduced on the geographical table which is the one that is compiled on 123.

The fields in the Repeat are automatically calculated by 123 from those in the geographic table.

Therefore, the geographic table becomes a sort of "copy" of the last survey made, and with the Repeat the historicity of the surveys is maintained.

 

Advantages:

-fields are pre-filled with the last set value

-filters and queries can be made directly on the geographical table within the ArcGIS Online/Enterprise WebApps.

 

Disadvantage:

-management of value changes on the Web App becomes more difficult: for example, if you need to change a field value from the Web App, you have to do it twice: once for the geographic table and once for the related Repeat table. That is unless you find an automatic mechanism on the Web App.

 

Was this one of your possible 10 solutions?

 

Paolo

0 Kudos
DougBrowning
MVP Notable Contributor

I think if you try to put the last value in the main form and then read it back to the repeat it is still a loop, just a longer one.  Pretty sure I tried that.  The only way I ever got last value to work was javascript.  It is really easy to use actually.  

I do not love the idea of repeating the values of the last repeat in the parent - that is double storing data really.  Also what can happen is someone may review the webmap, see an issue and fix it right there.  If they do that the form is skipped so none of the calcs run.  I still always like 1 visit one form.  You can use Arcade to display any values from the repeat in the parent if you need it.  The main issue with this can be symbolizing.  We had luck with using dashboards instead for this.

Another idea that is coming this yearish to AGOL (already in SDE) is attribute rules that may work to roll up to the parent.

Either way I would add location to the repeat. Always good to see if they went to the correct spot.  Plus then you can map all failed inspections for example.

Let me know what you come up with.

0 Kudos
PaoloBasile_ST
New Contributor

Hi Doug,

thank you that you are so kind to always respond to me.

 

I'll try to answer you point by point:

“I think if you try to put the last value in the main form and then read it back to the repeat it is still a loop, just a longer one.  Pretty sure I tried that.  The only way I ever got last value to work was javascript.  It is really easy to use actually. “

We actually managed to achieve what I was writing to you.

On Survey123 the operator fills the parent table while the child table values (hidden) are automatically filled by the copy formulas in the calculate ($Namefield).

We force the operator to create the record of the child table by putting on it the photo fields, one of which is mandatory.

There are of course many limitations to this solution: an important limitation concerns the Survey123 Web on which you cannot use the calculate formulas.

 

“I do not love the idea of repeating the values of the last repeat in the parent - that is double storing data really.  Also what can happen is someone may review the webmap, see an issue and fix it right there.  If they do that the form is skipped so none of the calcs run.  I still always like 1 visit one form.  You can use Arcade to display any values from the repeat in the parent if you need it.  The main issue with this can be symbolizing.  We had luck with using dashboards instead for this.”

You're right, this is the other real limitation! Duplicating information if you use a web app, you are forced to enter/edit from two sides, unless you come up with some automation to align the database.

(I had never considered the Dashboard for symbology. I'll try to study the topic, thanks.)

 

“Another idea that is coming this yearish to AGOL (already in SDE) is attribute rules that may work to roll up to the parent.”

We are working on Enterprise (without SDE) and as you know updates always arrive a bit late.

 

“Either way I would add location to the repeat. Always good to see if they went to the correct spot.  Plus then you can map all failed inspections for example.”

For our specific case this is a solution that we cannot take. However I will keep it in mind for future Surveys.

 

Thank you for now,

Paolo

0 Kudos
LeahSperduto1
New Contributor II

Hi Doug,

I too, am trying to create a Survey where the repeat values are prepopulated with the same values as the last record inserted in the repeat.

I have a form with a repeat that contains a text value (Site_Addr) I would like to have available for entry, and upon the second repeat entry, be prepopulated by the value of the previous text value entered.  I also tried to use the indexed-repeat () function but get the expected cyclical error.  I was hoping your workaround using Javascript would work here as an alternative, but I am not having success and wondered if you could help me troublshoot what I might be doing wrong.  I am new to using the pulldata function with Javascript and not familiar enough with Javascript to recognize what is not working; I'll give you the basics of what I have:  

begin repeat (Dev_CaseInfo)
-Site_Addr -Calculation: pulldata("@javascript", "RepeatAdd.js", "sameaslast", ${Dev_CaseInfo}, position, "Site_Addr")
end repeat

Javascript is (RepeatAdd.js): 

function sameaslast(Dev_CaseInfo,position, Site_Addr) {
             if (position > 1){
                        return repeat[position - 2][Site_Addr];


             }
}

 

I am wondering if I am misusing "position" in the function. Is this something indented in the original commenters function because of the geopoint?

I've also attached the survey and js file.

Any help you can offer is greatly appreciated! 

Thank you!

Leah Sperduto

0 Kudos
DougBrowning
MVP Notable Contributor

In your pulldata call you just have position.  It is a function so you need to do position(..) or if you have nested repeats use the name of the repeat position(${repeatname}).

Then in my code I wrapped it in number() so it may need that also I forget.

So change line 22 to

pulldata("@javascript", "RepeatAdd.js", "sameaslast", ${Dev_CaseInfo}, number(position(${Dev_CaseInfo})), "Site_Addr")

0 Kudos
LeahSperduto1
New Contributor II

Thank you for the prompt reply Doug!

I tried what you suggested and it did not work.  If I do not have a nested repeat, but just a basic repeat, would it look like this?

pulldata("@javascript", "RepeatAdd.js", "sameaslast", ${Dev_CaseInfo}, number(position(..1)), "Site_Addr")

in connect, the error it gives me upon testing it is:

@javascript error:ReferenceError: repeat is not defined in RepeatAdd.js:sameaslast

0 Kudos
DougBrowning
MVP Notable Contributor

No it would just be position(..)  the .. just tells it to use the repeat the question is in.  If you are outside the repeat you may need the repeat name.  Not sure where you are getting the 1 part?

I retested my form and this does all work so something else is up.  Did you say you are using this on a geopoint?  I am not sure that would work as javascript has no idea what a geopoint is.  May have to send lat/long then return that as a string like you would for URL passing.

0 Kudos
LeahSperduto1
New Contributor II

This error shows up in the Site_Addr when I attempt to add the 2nd repeat:

@javascript error:ReferenceError: repeat is not defined in RepeatAdd.js:sameaslast

0 Kudos