Search code examples
gallerypowerapps

How do I update multiple db records using a gallery in PowerApps?


I have an Audit app made in PowerApps which saves the answers to 18 questions to a database. I have another screen which allows users to edit the answers to those questions. I can display the previous answers and edit them but can't seem to update the original records in the db. It just displays the original records in the db. My code for the 'Save changes' button is below...

Update(AuditCollection,First(Filter(AuditCollection,ID=Value(IDGal_1.Text))),{AuditID:IDAuditVar, Question:txtQuestion.Text, Answer:txtComplianceEdit1.Text,Action :txtActionsEdit1.Text,AddToActionPlan:tglActionPlanEdit.Value});

I've also tried adding Collect('dbo.auditanswers',AuditCollection) on the end but this stores a further set of 18 questions although it does take in the new values.

EditAnswersScreen


Solution

  • I am doing something similar in one of my apps but instead of questions I store 2 values for the 12 months of the year.

    1. Start by Creating a collection of the Gallery Items

    ClearCollect(colItemsToSave,Gallery3.AllItems);

    1. Then iterate through those Items using a ForAll and then Patching the existing records
    ForAll(colItemsToSave,
        Patch('My Data Source',
            {ID: Value(lblItemID.Text)}, //Hidden ID label
            {
                 FFActuals:Value(inpActuals.Text),
                 FFForecasted:Value(inpForecast.Text)
            }
        )
    );
    

    The Patch functions goes as Patch(Datasource, ItemToBePatched, Updates).

    Patch can be used to created new records as well is you use it this way:

    Patch(Datasource,Defaults(Datasource),{ Property1:"", Property2:Value(input1.Text) })