save and update values using record type in a local variable/local variable

I have a problem to run quiz using 10 questions answers in in foreach loop. I want to save values in a local or rule variable. (question id with selected answer value) 

If user modify his answer, then selected Ans value changed with new selected option.

I want to do with record type.

  Discussion posts and replies are publicly visible

Parents
  • 0
    Certified Lead Developer

    Hi Kavitar,

    It would be helpful if you could provide more information on your QEP Questions and QEP Answers records - i.e. available fields and their relationships.

    Is every question a!radioButtonField() component with 4 options?

    With the very limited information I have provided an example solution below with example questions/answer data in local variables. You can replace the local variables with rule inputs if required and feed the data into the interface however the core concept is the same. You can set up each question with a single a!radioButtonField() in a foreach (for each question) and use fv!item to access the key data points and store the users answers. 

    Take a look at the code below and feel free to adapt to own use case / data structure (you can paste the code into your own interface editor and it will work as an example):

    a!localVariables(
      local!questions: {
        a!map(
          questionId: 1,
          questionText: "What is a web browser?",
          selectedAnswerId: null
        ),
        a!map(
          questionId: 2,
          questionText: "What colour is pink?",
          selectedAnswerId: null
        ),
        a!map(
          questionId: 3,
          questionText: "Third Question?",
          selectedAnswerId: null
        ),
        a!map(
          questionId: 4,
          questionText: "Fourth Question?",
          selectedAnswerId: null
        )
      },
      local!answers: {
        a!map(
          answerId: 1,
          questionId: 1,
          answerText: "A software program that allows yuo to access sites on the world wide web",
          isCorrect: true
        ),
        a!map(
          answerId: 2,
          questionId: 1,
          answerText: "A person who likes to look at websites",
          isCorrect: false
        ),
        a!map(
          answerId: 3,
          questionId: 1,
          answerText: "A computer that stores www files",
          isCorrect: false
        ),
        a!map(
          answerId: 4,
          questionId: 1,
          answerText: "A kind of spider",
          isCorrect: false
        ),
        a!map(
          answerId: 5,
          questionId: 2,
          answerText: "Blue",
          isCorrect: false
        ),
        a!map(
          answerId: 6,
          questionId: 2,
          answerText: "Yellow",
          isCorrect: false
        ),
        a!map(
          answerId: 7,
          questionId: 2,
          answerText: "Pink",
          isCorrect: true
        ),
        a!map(
          answerId: 8,
          questionId: 2,
          answerText: "I Don't know",
          isCorrect: false
        ),
        a!map(
          answerId: 9,
          questionId: 3,
          answerText: "This one",
          isCorrect: false
        ),
        a!map(
          answerId: 10,
          questionId: 3,
          answerText: "That one",
          isCorrect: false
        ),
        a!map(
          answerId: 11,
          questionId: 3,
          answerText: "The other one",
          isCorrect: false
        ),
        a!map(
          answerId: 12,
          questionId: 3,
          answerText: "None of the above",
          isCorrect: false
        ),
        a!map(
          answerId: 13,
          questionId: 3,
          answerText: "I Don't know",
          isCorrect: true
        )
      },
      local!selectedQuestion: 1,
      local!outerColumnWidth: "NARROW_PLUS",
      {
        a!columnsLayout(
          columns: {
            a!columnLayout(width: local!outerColumnWidth),
            a!columnLayout(
              contents: {
                a!columnsLayout(
                  columns: {
                    a!forEach(
                      items: local!questions,
                      expression: a!columnLayout(
                        contents: {
                          a!cardLayout(
                            contents: {
                              a!richTextDisplayField(
                                label: "Card Title",
                                labelPosition: "COLLAPSED",
                                align: "CENTER",
                                value: a!richTextHeader(text: fv!item.questionId)
                              )
                            },
                            height: "AUTO",
                            style: if(
                              local!selectedQuestion = fv!item.questionId,
                              "ACCENT",
                              "TRANSPARENT"
                            ),
                            marginBelow: "STANDARD",
                            link: a!dynamicLink(
                              label: "Card Link",
                              value: fv!item.questionId,
                              saveInto: local!selectedQuestion
                            )
                          )
                        }
                      )
                    )
                  }
                ),
                a!forEach(
                  items: local!questions,
                  expression: a!sectionLayout(
                    contents: {
                      a!richTextDisplayField(
                        label: "Question Header",
                        labelPosition: "COLLAPSED",
                        value: a!richTextHeader(
                          text: concat(
                            "Q.",
                            fv!item.questionId,
                            ") ",
                            fv!item.questionText
                          )
                        )
                      ),
                      a!radioButtonField(
                        label: "Options are",
                        labelPosition: "COLLAPSED",
                        required: true,
                        choiceLabels: index(
                          local!answers.answerText,
                          wherecontains(
                            tointeger(fv!item.questionId),
                            tointeger(local!answers.questionId)
                          ),
                          {}
                        ),
                        choiceValues: index(
                          local!answers.answerId,
                          wherecontains(
                            tointeger(fv!item.questionId),
                            tointeger(local!answers.questionId)
                          ),
                          {}
                        ),
                        value: fv!item.selectedAnswerId,
                        saveInto: fv!item.selectedAnswerId,
                        choiceStyle: "CARDS"
                      )
                    },
                    showWhen: local!selectedQuestion = fv!item.questionId
                  )
                )
              }
            ),
            a!columnLayout(width: local!outerColumnWidth)
          }
        )
      }
    )

Reply
  • 0
    Certified Lead Developer

    Hi Kavitar,

    It would be helpful if you could provide more information on your QEP Questions and QEP Answers records - i.e. available fields and their relationships.

    Is every question a!radioButtonField() component with 4 options?

    With the very limited information I have provided an example solution below with example questions/answer data in local variables. You can replace the local variables with rule inputs if required and feed the data into the interface however the core concept is the same. You can set up each question with a single a!radioButtonField() in a foreach (for each question) and use fv!item to access the key data points and store the users answers. 

    Take a look at the code below and feel free to adapt to own use case / data structure (you can paste the code into your own interface editor and it will work as an example):

    a!localVariables(
      local!questions: {
        a!map(
          questionId: 1,
          questionText: "What is a web browser?",
          selectedAnswerId: null
        ),
        a!map(
          questionId: 2,
          questionText: "What colour is pink?",
          selectedAnswerId: null
        ),
        a!map(
          questionId: 3,
          questionText: "Third Question?",
          selectedAnswerId: null
        ),
        a!map(
          questionId: 4,
          questionText: "Fourth Question?",
          selectedAnswerId: null
        )
      },
      local!answers: {
        a!map(
          answerId: 1,
          questionId: 1,
          answerText: "A software program that allows yuo to access sites on the world wide web",
          isCorrect: true
        ),
        a!map(
          answerId: 2,
          questionId: 1,
          answerText: "A person who likes to look at websites",
          isCorrect: false
        ),
        a!map(
          answerId: 3,
          questionId: 1,
          answerText: "A computer that stores www files",
          isCorrect: false
        ),
        a!map(
          answerId: 4,
          questionId: 1,
          answerText: "A kind of spider",
          isCorrect: false
        ),
        a!map(
          answerId: 5,
          questionId: 2,
          answerText: "Blue",
          isCorrect: false
        ),
        a!map(
          answerId: 6,
          questionId: 2,
          answerText: "Yellow",
          isCorrect: false
        ),
        a!map(
          answerId: 7,
          questionId: 2,
          answerText: "Pink",
          isCorrect: true
        ),
        a!map(
          answerId: 8,
          questionId: 2,
          answerText: "I Don't know",
          isCorrect: false
        ),
        a!map(
          answerId: 9,
          questionId: 3,
          answerText: "This one",
          isCorrect: false
        ),
        a!map(
          answerId: 10,
          questionId: 3,
          answerText: "That one",
          isCorrect: false
        ),
        a!map(
          answerId: 11,
          questionId: 3,
          answerText: "The other one",
          isCorrect: false
        ),
        a!map(
          answerId: 12,
          questionId: 3,
          answerText: "None of the above",
          isCorrect: false
        ),
        a!map(
          answerId: 13,
          questionId: 3,
          answerText: "I Don't know",
          isCorrect: true
        )
      },
      local!selectedQuestion: 1,
      local!outerColumnWidth: "NARROW_PLUS",
      {
        a!columnsLayout(
          columns: {
            a!columnLayout(width: local!outerColumnWidth),
            a!columnLayout(
              contents: {
                a!columnsLayout(
                  columns: {
                    a!forEach(
                      items: local!questions,
                      expression: a!columnLayout(
                        contents: {
                          a!cardLayout(
                            contents: {
                              a!richTextDisplayField(
                                label: "Card Title",
                                labelPosition: "COLLAPSED",
                                align: "CENTER",
                                value: a!richTextHeader(text: fv!item.questionId)
                              )
                            },
                            height: "AUTO",
                            style: if(
                              local!selectedQuestion = fv!item.questionId,
                              "ACCENT",
                              "TRANSPARENT"
                            ),
                            marginBelow: "STANDARD",
                            link: a!dynamicLink(
                              label: "Card Link",
                              value: fv!item.questionId,
                              saveInto: local!selectedQuestion
                            )
                          )
                        }
                      )
                    )
                  }
                ),
                a!forEach(
                  items: local!questions,
                  expression: a!sectionLayout(
                    contents: {
                      a!richTextDisplayField(
                        label: "Question Header",
                        labelPosition: "COLLAPSED",
                        value: a!richTextHeader(
                          text: concat(
                            "Q.",
                            fv!item.questionId,
                            ") ",
                            fv!item.questionText
                          )
                        )
                      ),
                      a!radioButtonField(
                        label: "Options are",
                        labelPosition: "COLLAPSED",
                        required: true,
                        choiceLabels: index(
                          local!answers.answerText,
                          wherecontains(
                            tointeger(fv!item.questionId),
                            tointeger(local!answers.questionId)
                          ),
                          {}
                        ),
                        choiceValues: index(
                          local!answers.answerId,
                          wherecontains(
                            tointeger(fv!item.questionId),
                            tointeger(local!answers.questionId)
                          ),
                          {}
                        ),
                        value: fv!item.selectedAnswerId,
                        saveInto: fv!item.selectedAnswerId,
                        choiceStyle: "CARDS"
                      )
                    },
                    showWhen: local!selectedQuestion = fv!item.questionId
                  )
                )
              }
            ),
            a!columnLayout(width: local!outerColumnWidth)
          }
        )
      }
    )

Children
No Data