How do you create a trigger to run a javascript in an xls form?

589
8
08-13-2020 11:57 AM
BryceHancock
New Contributor III

Hi,

I have form that runs a custom javascript function to calculate the number of records based on a selection and value entered outside of a repeat. The javascript works as expected but it seems to run whenever a new repeat is added, creating inefficiencies with data entry in the field; each new repeat record takes up to 4 seconds to load. Is there a way to set a trigger in a javascript function, so it runs only when values from two fields are changed outside a repeat? I found the javascript .change method but I am not sure the proper syntax to implement it in Survey123? Can anyone help with this? Thank you.

0 Kudos
8 Replies
Philip-Wilson
Esri Notable Contributor

Hi Bryce,

Have you tried making the question that has the pulldata @ JS function in it only relevant when those two questions have a value or the value changes. When a question is not relevant, the calculation or pulldata expression does not execute.

Regards,

Phil.

0 Kudos
BryceHancock
New Contributor III

Thanks Phil. What would the proper syntax be for the relevant expression? I tried (${SPECIES_LU}!='') and (${FISH_LENGTH_LU}!='') but the Javascript still fires when the value in only one field is changed. Thank you.

0 Kudos
BryceHancock
New Contributor III

Ok, I figured out the proper syntax for the relevant field. I was using single quotes instead of double quotes and I added another parenthesis to enclose the two expressions as follows: ((${SPECIES_LU}!=") and (${FISH_LENGTH_LU}!="))


The expression works fine on my computer through Survey123 connect but when I publish and download to my device, the relevant expressions do not work. The script still fires when a value is entered in just one field in the capture probability calculator, and the lag time between entering repeat records in the fish measurements is still approximately 4 seconds. Any ideas?

I attached my xls form for further reference. The script operates on the capture probability calculator. I do have another script in the fish measurement repeat to autodefault to last record entered for two fields.

0 Kudos
Philip-Wilson
Esri Notable Contributor

Hi Bryce,

Sorry for the delay, I am still looking into this issue. Is the problem you are seeing with the field app or the web app? It wasn't clear from your comments above? If you are seeing it work as expected in Connect, then it should work the same in the field app, however there could be an issue if you try to run the same JS and relevant fields in repeats in the web app?

Phil.

0 Kudos
BryceHancock
New Contributor III

No problem Philip and thanks so much for your help. The issue occurs in the field app on my tablet. Everything works as expected in Connect but when I publish the form and download it to my tablet, the relevant expressions do not work to trigger the Javascript. I have tried downloading to two different device models, one running android 6 and one running android 7.1 - same result.

0 Kudos
BryceHancock
New Contributor III

A bit more information: the relevant fields I am using to trigger the javascript calculation are outside the repeat. I do have a different javascript within the repeats but it is only to repeat the value in two fields for the last record entered, so should not have any effect on the relevant expression outside the repeat. This seems like a bug to me. Like the relevant expressions are not being recognized in the field app on my tablet for some reason.

0 Kudos
Philip-Wilson
Esri Notable Contributor

Hi Bryce,

James has picked up this same issue you reported on EAC and asked you a few questions, so if you work with him via that post we can hopefully get a solution for you.

Regards,

Phil.

0 Kudos
BryceHancock
New Contributor III

Hi Philip,

I answered James' questions and sent a couple of follow-up messages in EAC on performance issues we had last week when attempting to use the survey. I haven't heard back from James yet, and I know you're all very busy, so don't mean to pester you, but could you advise on whether relevant issue is a bug, or do I need to rewrite the Javascript to achieve the desired result? Thanks so much!

0 Kudos