Search code examples
offlinepowerapps

Can I use a ForAll and UpdateIf within a local offline Powerapps collection?


Can anyone help?I need assistance to collect multiple records in a gallery and save it to a local collection when offline. When my app is connected, my script uses a ForAll to go through all the gallery items then if the Question ID matches the ID in the gallery, it patches the records to the SQL database. This part works fine. However, when offline, I collect the items and save them to a local collection called LocalAnswers. It only saves 1 record (instead of 20) and does not pull in the Question ID. I have tried inserting a ForAll and UpdateIf within my Collect function but can't seem to get it right. Any ideas?

If(
    Connection.Connected,
    ForAll(
        Gallery2.AllItems,
        UpdateIf(
            AuditAnswers,
            ID = Value(IDGal.Text),
            {
                AuditID: IDAuditVar,
                Answer: Radio1.Selected.Value,
                Action: ActionGal.Text,
                AddToActionPlan: tglAction.Value
            }
        )
    ),
    Collect(
        LocalAnswers,
        {
            AuditID: IDAuditVar,
            Answer: Radio1.Selected.Value,
            Action: ActionGal.Text,
            AddToActionPlan: tglAction.Value
        }
    )
);

Solution

  • Collect only pulls a single record because you only have a single record defined (everything between the {}).

    I don't typically create collections from Gallery.AllItems but rather from a Data Source (Sharepoint, SQL, another Collection, etc.), so not sure if this will work without testing.

    Try something like:

    ForAll(Gallery2.AllItems,
        Collect(colLocalAnswers,
            {
                AuditID: ThisRecord.AuditID, //or whatever the control's name is
                Answer: ThisRecord.Radio1.Selected.Value,
                Action: ThisRecord.ActionGal.Text,
                AddToActionPlan: ThisRecord.tglAction.Value
            }
        )
    );
    
    SaveDate(colLocalAnswers, "localfile")