Question T17226
Visible to All Users

complete the survey only after a successfull api call

created 10 months ago (modified 10 months ago)

Hey, I'm trying to make sure the survey completes only after i get a response from an external API service we have.
I'm using the onCompleting event and try to set the allow field to false/true based on the result but I guess i'm doing something wrong because the options.allow = false does prevent from the complete page to show up but when I change it back to true it doesn't go to the complete page.

This is my code:

TypeScript
survey.onCompleting.add((_, options) => { options.allow = false; if (questionnaireId) { completeQuestionnaireToast( completeQuestionnaire(gatherProcessId, questionnaireId) ).then(() => (options.allow = true)); } });

Answers approved by surveyjs Support

created 10 months ago

Hello Amit,
You can introduce an additional flag which would indicate whether or not a survey can be completed. After data is successfully sent to a storage, call the survey.completeLastPage() function to initiate survey completion. Consider the following code:

JavaScript
let canComplete = false; survey.onCompleting.add((sender, options) => { options.allow = canComplete; setTimeout(() => { // Send data to a storage canComplete = true; survey.completeLastPage(); // This would invoke the survey.onCompleting event with canComplete set to true }, 1000); })

Please let me know if using the survey.completeLastPage function solves the issue.

Thanks