I have a form in a Power App where questions can be selected and contained in a gallery. I'm using the gallery to provide an answer field that when the Next button is clicked, it patches the values from the gallery to a single row in a SharePoint list. There will not be a specific number of questions & answers, but the number will always range between 5 and 15 questions & answers. I've created the SharePoint list to have a column fields that correspond to the question and answer order (i.e. Question1, Answer1, Question2, Answer2).
I've been able to figure out a way that writes the data like I want it to by using First(GalleryItem.AllItems).labelName.Text and Last(FirstN(GalleryItem.AllItems, Order).labelName.Text formula formats. However, this only works if there are exactly 15 questions and answers. If there are less, the last answer data is repeated for the remaining slots. If there are less questions and answers, those fields should be blank in SharePoint.
My basic Patch command is:
Patch(
Form, Defaults(Form),
{
Question1: First(formGallery.AllItems).Question.Text,
Answer1: First(formGallery.AllItems).Answer.Text,
Question2: Last(FirstN(formGallery.AllItems, 2)).Question.Text,
Answer2: Last(FirstN(formGallery.AllItems, 2)).Answer.Text,
Question3: Last(FirstN(formGallery.AllItems, 3)).Question.Text,
Answer3: Last(FirstN(formGallery.AllItems, 3)).Answer.Text,
Question4: Last(FirstN(formGallery.AllItems, 4)).Question.Text,
Answer4: Last(FirstN(formGallery.AllItems, 4)).Answer.Text,
....
Question15: Last(FirstN(formGallery.AllItems, 15)).Question.Text,
Answer15: Last(FirstN(formGallery.AllItems, 15)).Answer.Text
}
));
Any thoughts on how I can accomplish my goal of writing the gallery items to a single SharePoint row, leaving any of the 15 question and answer fields blank if they are not used?