How to loop a list of survey questions with each comments saved without looping back?

How to loop a list of survey questions with each comments saved without looping back?

  

Hi,

I have a list of survey questions for example 12 questions.

1) I have done a for each loop followed by a switch where if questions number equals 1 it goes to the next step (done).

2) Next i have to input comments before the results will be save (done).

3) But if comments box is empty, a validation is check and on debug it will re-loop through the list and questions 1 comments will be save again which is a duplicate records. How do i prevent it from going back to questions 1 when comments box is not empty. (some error).


Thank you,

Kelvin

Hi Kelvin,

Change the button method from Submit to Ajax Submit, and on validation failure, refresh the comment input, not the list.

The submit will run the action, than the preparation and finally will refresh the entire page, constructing the list again. Than the problem.

Using ajax you can control this, as the preparation will NOT be executed again.

Cheers,

Eduardo Jauch.

Hi Eduardo,

Sorry, i am doing it on mobile application. If not wrong, mobile app doesnt have Ajax Submit.


Thanks,

Kelvin

Hi,

Telling if it is mobile or web is important.

Yes, mobile relies on React, and so changes are automatic. 

Your button is navigating or calling an action?

cheers,

Eduardo Jauch

Hi,

When i click submit, it will do a check and if empty comments box it will stop user to proceed. The submit button is a client action. 


Thank you,

Kelvin

Ok...

Suddenly I realize that I probably are misunderstanding your problem...
Could you explain what the action is doing? 

Like, I'm on question 3. I press the button to save the answer (that is empty).
The action is called. The validation fails...
Than what? What are you doing after the validation fails?

Cheers,
Eduardo Jauch

Hi,

Actually for example.

Qns 1:

Comment box: not empty

Qns 2:

Comment box: empty


1.Save button - on click it do a for each to loop the questions list followed by a switch where i put condition of question 1 go to question 1 and save and so on. When it find a comment box empty a validation message is shown. The first questions comment is saved.

2. User went to fill in the empty comment box. Press save and the  logic goes again.. For each, then it iterates question 1 again n save that comment while saving question 2.


In the end, my table i see 2 questions 1 comments and 1 comment from questions 2. In a way, question 1 is duplicating again.


Thank you,

Kelvin

Ah!

Ok, don't know if your approach is the best, but one thing you can do to stop saving NEW comments for those that were already saved, is very simple.

If you are doing a ForEach, you are iterating a list, probably of a structure or an aggregate.
So, just add a new field (if it does not exist yet), that is the entity identifier to where you are saving the record.

In the ForEach, if the comment is ok, save it using a CreateOrUpdate. After it, update the id with the one returned by the entity action CreateOrUpdate. 

The first time, the id will be NULL, so a new record will be saved. From this point, everytime the action runs, you will just update the already existing records. This will allow the user to change comments also.

I'm assuming the list is of the type that you are saving. If not and you are filling a local variable that is of the entity record type, create a new field in the list source (aggregate/structure) to keep the saved ID and remember to pass this ID to the local variable.

Hope this helps. If not, let me know.

Cheers,
Eduardo Jauch

Hi,

Sorry the new field meaning? As for the CreateOrUpdate is to the question id? Am i right to say that?


Thanks,

Kelvin

Hi,

You have a set of questions to show to the user. It will fill the answers and save them.
I'm assuming that the questions come from an entity, but the answers will be save in a different one, with a FK (foreigny key) to the question id.

In this case, the CreateOrUpdate that saves the answer (comment), will return the id that is given for this record.
This is the id you must keep, in order to avoid crating a NEW record.

So, when you do the CreateOrUpdate, you need to save this id to someplace. The ideal is to the list you are using to show the questions. 

If the list is just an aggregate fetching data from the database, you can add a new column to the aggregate whose type is integer and set the value to 0, and than save the id to the correct line after saving the comment, and update the record you are using to save the comment with this id when you will save it.

Cheers,
Eduardo Jauch

Hi,

Oh ok i get what u mean. Let me try and update you tomorrow :).


Thanks for the help,

Kelvin