Search code examples
sharepointpowerapps

How can I update two SharePoint lists based on a single schema? (PowerApps)


I am trying to create a priority queue system for a PowerApp I am developing. The priority queue (RequestPriorityQueue) is stored in a SharePoint list with the following columns: Title, UniqueID, and Priority. Title is a GUID representing the queue item, UniqueID is a short identifier for the queue item, and Priority is a number, with 1 being top priority. There is also a separate SharePoint list (RequestsList) with the same columns, plus a number of other columns representing data associated with the queue item.

What I am trying to do is patch both these SharePoint lists with the same collection which represents the priorities changing due to a queue item moving up or down in priority (which pushes all queue items above/below it down/up the queue). However, this doesn't seem to work - whenever I patch the priority list, it adds new items to the list, instead of updating the list items with the same Title/UniqueID - what am I doing wrong?

If(!(ThisItem.Priority = PriorityDropdown.Selected.Value),

    ClearCollect(PrioritiesToChange, {Title: "init", UniqueID0: Blank(), Priority: -1});
    ForAll(ShowColumns(RequestPriorityQueue, "Title", "UniqueID0", "Priority"), Collect(PrioritiesToChange, {Title: Title, UniqueID0: UniqueID0, Priority: Priority}));

    If(ThisItem.Priority > PriorityDropdown.Selected.Value,

        ClearCollect(PrioritiesToChange, Filter(PrioritiesToChange, Priority >= PriorityDropdown.Selected.Value && Priority < ThisItem.Priority));
        UpdateIf(PrioritiesToChange, true, {Priority: Priority + 1});
        UpdateIf(PrioritiesToChange, Title = ThisItem.Title, {Priority: PriorityDropdown.Selected.Value});,

        ClearCollect(PrioritiesToChange, Filter(RequestPriorityQueue, Priority <= PriorityDropdown.Selected.Value && Priority > ThisItem.Priority));
        UpdateIf(PrioritiesToChange, true, {Priority: Priority - 1});
        UpdateIf(PrioritiesToChange, Title = ThisItem.Title, {Priority: PriorityDropdown.Selected.Value});
    );

    RemoveIf(PrioritiesToChange, Title = "init");

    Patch(RequestPriorityQueue, PrioritiesToChange);
    Patch(RequestsList, PrioritiesToChange);
    Refresh(RequestsList);
    Notify("Priority changed");
);

Solution

  • Finally figured it out. The trick was to patch using a collection with matching IDs to the SharePoint list items. This was easy enough for the first list, but it took a while to figure out how to update the second the list. The trick there is to update the IDs in the collection after the first patch with a lookup from the second list (i.e. the last UpdateIf from the code below).

    If(!(ThisItem.Priority = PriorityDropdown.Selected.Value),
    
        ClearCollect(PrioritiesToChange, ShowColumns(RequestPriorityQueue, "ID", "Priority", "Title"));
    
        If(ThisItem.Priority > PriorityDropdown.Selected.Value,
    
            ClearCollect(PrioritiesToChange, Filter(PrioritiesToChange, Priority >= PriorityDropdown.Selected.Value && Priority <= ThisItem.Priority));
            UpdateIf(PrioritiesToChange, true, {Priority: Priority + 1});
            UpdateIf(PrioritiesToChange, Title = ThisItem.Title, {Priority: PriorityDropdown.Selected.Value});,
    
            ClearCollect(PrioritiesToChange, Filter(RequestPriorityQueue, Priority <= PriorityDropdown.Selected.Value && Priority >= ThisItem.Priority));
            UpdateIf(PrioritiesToChange, true, {Priority: Priority - 1});
            UpdateIf(PrioritiesToChange, Title = ThisItem.Title, {Priority: PriorityDropdown.Selected.Value});
        );
    
        Patch(RequestPriorityQueue, ShowColumns(PrioritiesToChange, "ID", "Priority", "Title"));
        UpdateIf(PrioritiesToChange, true, {ID: LookUp(RequestsList, RequestsList[@Title] = PrioritiesToChange[@Title]).ID});
        Patch(RequestsList, ShowColumns(PrioritiesToChange, "ID", "Priority", "Title"));
        Refresh(RequestsList);
        Notify("Priority changed");
    );