# Dependency Cycle - trying to have a calculation for a question reference itself from a previous repeat given certain logic

259
4
04-17-2024 02:06 PM
by
New Contributor III

Hi,

Background info: I am creating a survey for people to go out and test different materials at different locations. The user may need to conduct an unlimited number of tests so I have contained everything in a repeat. On the first repeat, i.e. position(..) = 1, the user is asked to input their location. The question is a text field and their response would be something like 'Road 1' or 'Stormwater Line'. Call this \${location_initial}

On the second repeat they are presented with a select_one question asking them if they have changed location. If they select 'Yes' then a new question appears asking them to input their new location, and if they select 'No' then the initial location is retained in a field in the background.

Calculation for background location field, called \${location_a}:

if ( position(..) = 1, \${location_initial}, if ( \${yesno_newlocation} = 'Yes', \${new_location}, \${location_previous}) )

The calculation for \${location_previous} is:

indexed-repeat(\${location_a},\${ndm_bs_repeater},\${rp_num_bs}-1)

What these calculation's aim to do is check that if the user selects 'No' to being in a new location, then the location of the previous repeat is called in. It causes a dependency cycle though, I guess because I am referencing \${location_a} in the \${location_previous} calculation.

A short sighted way around this would be to change the calculation for \${location_previous} to:

indexed-repeat(\${new_location},\${ndm_bs_repeater},\${rp_num_bs}-1)

The issue with this is that if a person tests at one location for the first repeat, then changes on the second but doesnt change for the third, fourth and fifth... the \${new_location} field doesn't have anthing to pull from after the third repeat because they kept answering 'No' to the 'Have you changed location' question.

Any help would be much appreciated! I have several fields to apply this logic to..... eek

4 Replies
MVP Esteemed Contributor

Yea it cannot be done this way.  123 works with Names as the way to track fields.  So it has no idea that FieldA in repeat 1 is different from FieldA repeat 2.  I have been working on this for 6 years now with no luck.  Lots of posts out there if you want to look around.

Only way I kinda got to work was javascript.  But for me my repeat was too big.

This post here has links to the two options that I have found.

https://community.esri.com/t5/arcgis-survey123-ideas/quot-same-as-last-quot-option-for-repeat-record...

Hope that helps

by
New Contributor III

Thanks Doug, I'll give your function a go. Out of curiosity how big was your repeat? mine is about 70 lines in the xls form and it wouldn't be uncommon for it to be repeated 100 times in a single response.

by
New Contributor III

Hi Doug,

I've tried:

 begin group testing testing begin repeat ndm ndm text location_initial location initial text location_a location a if ( \${rp_num} = 1, \${location_initial}, if ( \${yesno_newloc} = 'Yes', \${new_loc}, pulldata("@javascript", "functions.js","sameAsLast",\${ndm},\${rp_num},\${location_a}))) text prev_loc previous location text new_loc new location select_one yesno_newloc yesno_newloc yes no new location integer rp_num repeat number position(..) end repeat end group

as well as:

 begin group testing testing begin repeat ndm ndm text location_initial location initial text location_a location a if ( \${rp_num} = 1, \${location_initial}, if ( \${yesno_newloc} = 'Yes', \${new_loc}, \${prev_loc})) text prev_loc previous location pulldata("@javascript", "functions.js","sameAsLast",\${ndm},\${rp_num},\${location_a}) text new_loc new location select_one yesno_newloc yesno_newloc yes no new location integer rp_num repeat number position(..) end repeat end group

But getting a dependency cycle for both :(, is this the correct implementation of the script?

Script i invoked:

function sameAsLast(ndm, rp_num, location_a) {

if (position > 1){

return ndm[rp_num - 2][location_a];

}

}

MVP Esteemed Contributor

I think the way you have it loc_a has an if to prev_loc and that calls loc_a so that is the loop I think.

First test just having it show you the value without the calc in loc_a and see if that works.  Then go from there.

It has been years since I used the code.