Saving Multiple Page Survey Answers as a JSON Object

Certified Associate Developer

Hi,

I am trying to save the user answers of multiple page survey interface in one local variable as a json object to save it to the database. I have rendered these multilevel questions from part of the json object like that:

{
"sections": [{
"sectionname": "1. Legal Information, Disclaimers and Acknowledgments",
"sectionlevel": 1,
"sectionparent": "root",
"questions": [{}]

},
{
"sectionname": "1.1 Legal information",
"sectionlevel": 2,
"sectionparent": "1. Legal Information, Disclaimers and Acknowledgments",
"sectioninstructions": "Test Instructions",
"questions": [{
"questiontitle": "Legal Terms of Use",
"questionnbr": "1.1.1",
"questioninstructions": "Test Instructions",
"questiontext": "Feugiat sed lectus vestibulum mattis ullamcorper. Tempor orci dapibus ultrices in. Pretium fusce id velit ut. Et magnis dis parturient montes nascetur. Lacus sed viverra tellus in. Consectetur purus ut faucibus pulvinar elementum integer enim. Lectus quam id leo in vitae turpis massa sed elementum. Sed nisi lacus sed viverra tellus in hac habitasse. In tellus integer feugiat scelerisque varius morbi enim nunc faucibus.",
"responsetype": "radiobutton",
"responseoptions": [{
"responsevalue": "1",
"responsetext": "Yes. I agree to the terms of use.",
"responselabel": ""
},
{
"responsevalue": "0",
"responsetext": "No. I do not agree.",
"responselabel": ""
}
],
"ismandatory": "yes",
"visibleif": "",
"answertext": ""
},
{
"questiontitle": "Privacy, Disclosure and Use of Data",
"questionnbr": "1.1.2",
"questioninstructions": "Test Instructions",
"responsetype": "radiobutton",
"responseoptions": [{
"responsevalue": "1",
"responsetext": "Yes. I agree to the terms and conditions.",
"responselabel": ""
},
{
"responsevalue": "0",
"responsetext": "No. I do not agree to the terms and conditions.",
"responselabel": ""
}
],
"ismandatory": "yes",
"visibleif": "",
"answertext": ""
}
]
},
{
"sectionname": "1.2 Disclaimers and Acknowledgements",
"sectionlevel": 2,
"sectionparent": "1. Legal Information, Disclaimers and Acknowledgments",
"sectioninstructions": "Test Instructions",
"questions": [{
"questiontitle": "Disclaimer",
"questionnbr": "1.2.1",
"questioninstructions": "Test Instructions",
"questiontext": "Feugiat sed lectus vestibulum mattis ullamcorper. Tempor orci dapibus ultrices in. Pretium fusce id velit ut. Et magnis dis parturient montes nascetur. Lacus sed viverra tellus in. Consectetur purus ut faucibus pulvinar elementum integer enim. Lectus quam id leo in vitae turpis massa sed elementum. Sed nisi lacus sed viverra tellus in hac habitasse. In tellus integer feugiat scelerisque varius morbi enim nunc faucibus.",
"responsetype": "radiobutton",
"responseoptions": [{
"responsevalue": "1",
"responsetext": "Yes. I agree.",
"responselabel": ""
},
{
"responsevalue": "0",
"responsetext": "No. I do not agree.",
"responselabel": ""
}
],
"ismandatory": "yes",
"visibleif": "",
"answertext": ""
},
{
"questiontitle": "Acknowledgement",
"questionnbr": "1.2.2",
"questioninstructions": "Test Instructions",
"responsetype": "radiobutton",
"responseoptions": [{
"responsevalue": "1",
"responsetext": "Yes. I acknowledge.",
"responselabel": ""
},
{
"responsevalue": "0",
"responsetext": "No. I do not accept the terms.",
"responselabel": ""
}
],
"ismandatory": "yes",
"visibleif": "",
"answertext": ""
}
]
}
]
}

i did nested for loop to show the interface like the picture below, I have created a code to render these questions successfully for each page and i am saving the questions for each page in a local variable and i am mapping the user answer for each field to answertext (in the json object), so it is mapping the value correctly but the challenge here when go to other page, the local variable which is holding the current questions with the answers will be updated with the new questions for the new page and i will not have the old answers any more. so i thought to save the all questions in a local variable, then map the answers to each question directly but i could not because it is hierarchy structure. so could you please share any thoughts please? 

This is part of code that i am applying to render the questions for each section using nested for loop, so here i am just testing on textfield object:

a!forEach(
items: local!questions,
expression: {
/*the start of the questions types*/
if(
fv!item.responsetype = "text",
a!textField(
value: fv!item.answertext,
saveInto: {
fv!item.answertext,
}
),
)})

I would appreciate any thoughts for that because this is an urgent task. thanks in advance!

  Discussion posts and replies are publicly visible

Parents Reply
  • 0
    Certified Lead Developer
    in reply to Ahmed Koriem

    At a high level I'm only referring to code you've already pasted previously in this post not within code boxes. 

    As per your issue: I looked through the code briefly and as of yet I don't have any specific suggestions other than my normal advice, which is, start with a simple-but-working version, and add complexity one step at a time making sure each addition still works.

    If you'd be willing to edit your original post to include the same code you posted originally, but moved into a code box and with restored indentation, I'll try to take a closer look.  (Restoring indentation should be as easy as opening a blank Expression Rule editor and pasting the code there, then re-copying the result).  As an aside: when posting Appian Expression/SAIL code I often select the "JAVA" language option in the Code Box settings as it adds context highlighting that seems generally compatible with Appian code (though I do wish they'd add an Appian-specific option...)

Children
No Data