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
  • When you say 'multiple pages' are you referring to a wizard on the same Interface or multiple user inputs tasks? If everything is on the same interface, then you can abstract your dictionary a little more by having a map
    a!map(

         pageOne: local!jsonOne,

         pageTwo: local!jsonTwo

    )

  • 0
    Certified Associate Developer
    in reply to Danny Verb

    Hi Danny,

    Thank you for your response! The survey has a hierarchy structure like in the Json above like (children and parents relationship). but any way i already built it by nested loop on the json. but the challenge here how i am going to map all of the answers for all of the subsections of the survey in one place. till now i can map each page answers with a local variable but when i move to another subsection, the local variable will have a new value of the new page questions. I am trying to collect all of the answers in one local variable to save it in a database table as a json object. any thoughts to achieve that?

  • You can use the sql type TEXT as your column and store the JSON in there. I know you say you're looping through the JSON. I'm not sure what you mean but the simplest way to handle JSON in Appian is to use a map/dictionary for everything and then to use the a!toJSON() and a!fromJSON() functions

  • 0
    Certified Associate Developer
    in reply to Danny Verb

    I already read successfully the json object using those functions as the code below:

    local!roots: a!fromJson(
    a!jsonPath(
    rule!PCEMS_querySurveyJSON(),
    "['sections'][*][?(@.sectionparent=='root')]"
    )
    )

    and i am looping on the objects successfully and everything renders fine. the issue here i need to save the user responses for the survey questions in one local variable. i am able to save just each page responses through this code:

    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,
    }
    ),
    )})

    but when i move to another section, this local!questions will be refreshed with the new questions of that subsection page. so i need to have a local variable which has all of the answers and if the user update the value it should be updated in there also.

Reply
  • 0
    Certified Associate Developer
    in reply to Danny Verb

    I already read successfully the json object using those functions as the code below:

    local!roots: a!fromJson(
    a!jsonPath(
    rule!PCEMS_querySurveyJSON(),
    "['sections'][*][?(@.sectionparent=='root')]"
    )
    )

    and i am looping on the objects successfully and everything renders fine. the issue here i need to save the user responses for the survey questions in one local variable. i am able to save just each page responses through this code:

    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,
    }
    ),
    )})

    but when i move to another section, this local!questions will be refreshed with the new questions of that subsection page. so i need to have a local variable which has all of the answers and if the user update the value it should be updated in there also.

Children
No Data