cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
_sourcecod3_
Helper IV
Helper IV

Update item in collection from selected item in gallery.

Hi all,

 

I'm working on an inventory app and i need to be able to add , edit and remove items from a  collection as they're selected from a gallery. I'm getting stuck on updating a record in a collection, which in my scenario is the quantity of items selected from the gallery. Users should be able to click on the same items multiple times and those items will be added to the collection, then patched to a SharePoint list. When the items are added to the collection they show up on another gallery which the source is the collection. This is what I have so far on a button: 

If(
    varCount <= varCount,
    Set(
        varCount,
        varCount + 1
    );
    If(
        !IsBlank(
            LookUp(
                colProductSelected,
                itemID = mainGallery.Selected.ID
            )
        ),
        UpdateIf(
            colProductSelected,
            mainGallery.Selected.ID = itemID,
            {itemQTY: varCount}
        ),
        Collect(
            colProductSelected,
            {
                itemDescription: lbItemTitle.Text,
                itemStars: Value(lbRequiredStars.Text),
                itemQTY: varCount,
                itemID: Value(lbGalleryItemID.Text),
                itemGUID: varGUID
            }
        )
    )
)

 

My code adds the item to the collection. when you click on item1 it adds it to the collection and it shows the quantity of "1" in the quantity column of my second gallery, the problem is that when i click on item2 and gets added to the collection i can't go back and click on item1 to add it again. It adds a number to the last selected item not the current selection....

 

I know this is happening because of the 'Last' function on my code. What can i use instead so that it updates the selected item not the last selected one. 

 

Thanks in advance.

10 REPLIES 10
prajyot
Resolver III
Resolver III

If I understood correctly you are facing issues while updating the record, it's not fetching the selected record ( itemID has always previous value).

 

In order to do this you would be using any control ( button or pane) to edit that specific row. You can pass the current selected item using {selecteditem:ThisItem}. Once you fetch ThisItem.itemID you will be able to get the selected row itemID.

 

Let me know if it is not the case.

 

 

Thanks,

Prajyot M

 

 

WarrenBelz
Super User
Super User

Hi @_sourcecod3_ ,

Before I start on the rest of the code (and I like your use of UpdateIf and Collect), this test 

If(
    varCount <= varCount,

will always be true (a variable will always equal itself). What is your logic here. Could you also share a screen shot.

 

@prajyot I can modify the selected item with the code above, the issue is that It's not updating the itemQTY column in my collection in incremental numbers. I'm trying to get it to when the user selects the same item from the gallery this itemQTY number increases. I'm attempting to use the patch function in place of UpdateIf but i can't get it to update the 'selected item'. This is what I came up with:  

If(
    varCount <= varCount,
    Set(
        varCount,
        varCount + 1
    );
    If(
        !IsBlank(
            LookUp(
                colProductSelected,
                itemID = mainGallery.Selected.ID
            )
        ),
        Patch(
            colProductSelected,
            Last(colProductSelected),
            {itemQTY: varCount}
        ),
        Collect(
            colProductSelected,
            {
                itemDescription: lbItemTitle.Text,
                itemStars: Value(lbRequiredStars.Text),
                itemQTY: varCount,
                itemID: Value(lbGalleryItemID.Text),
                itemGUID: varGUID
            }
        )
    )
)

I know using the Last Function updates only the last item added to the collection, and that's where I'm stuck. 

@WarrenBelz  Hi, the logic behind the first if statement was to check if the count was less then or equal to the count that was added to the collection. It was not needed so i deleted it. Here's my updated code and it works to modify the entry on my collection, but I can't  figure out a way to increase the quantity by a number every time the same item in the gallery is selected: 

// Check if item exists in collection.
If(
    !IsBlank(
        LookUp(
            colProductSelected,
            itemID = mainGallery.Selected.ID
        )
    ),
    
// Loop through all the items that match the ID from the selected item in gallery and update the quantity value.
    ForAll(
        colProducts,
        UpdateIf(
            colProductSelected,
            itemID = mainGallery.Selected.ID,
            {itemQTY: varQTYCounter}
        )
    ),

// Add to the collection.
    Collect(
        colProductSelected,
        {
            itemDescription: lbItemTitle.Text,
            itemStars: Value(lbRequiredStars.Text),
            itemQTY: varCount,
            itemID: Value(lbGalleryItemID.Text),
            itemGUID: varGUID
        }
    )
)

Not sure if the screenshot of the app will do much but here's an idea. When they click the plus sign on gallery1 it adds that item to the collection on the right side. if they click on the plus sign again for the same item the "Quantity" value increases by a number.

 

app.png

HI @_sourcecod3_ ,

Two things

  • How is varQTYCounter​ determined?
  • How would someone select the same item in the gallery more than once?

Hi @_sourcecod3_ ,

Just checking if you got the result you were looking for on this thread. Happy to help further if not.

 

Please click Accept as solution if my post helped you solve your issue. This will help others find it more readily. It also closes the item. If the content was useful in other ways, please consider giving it Thumbs Up.

@WarrenBelz No i still haven't been able to figure it out. This is my updated code: 

If(
    !IsBlank(
        LookUp(
            colProductSelected,
            itemID = mainGallery.Selected.ID
        )
    ),
    UpdateContext(
        {
            varExists: true,
            varQTYCounter: varQTYCounter + 1
        }
    ),
    UpdateContext(
        {
            varExists: false,
            varQTYCounter: 1
        }
    )
);
If(
    varExists,
    ForAll(
        colProducts,
        UpdateIf(
            colProductSelected,
            itemID = mainGallery.Selected.ID,
            {itemQTY: varQTYCounter}
        )
    ),

    Collect(
        colProductSelected,
        {
            itemDescription: lbItemTitle.Text,
            itemStars: Value(lbRequiredStars.Text),
            itemQTY: varQTYCounter,
            itemID: Value(lbGalleryItemID.Text),
            itemGUID: varGUID
        }
    )
)

 

When i click on an item in the gallery, the code works fine. It adds it to the collection and if i click on the same item it will update the quantity increasing the number. When i click on a different item in the galley it creates a new item in the collection and the quantity column starts from '1'. The problem that i'm running into is that if I have selected the same item 5 times, select a different item let's say 10 times, if i go back and select an additional item on the first item again it will pick up the count where i left off on the previous number and add '1' to it. So in this case the first item will jump from 5 items to 11. I need to find a way that when I click on a different item to somehow save the itemQTY from the collection into a variable so that i can compare them when clicking on a different item. Unless there's a different more efficient way of doing this please share your thoughts. 

 

Thanks again.

 

@_sourcecod3_ ,

Before we get into to the result of all of this, I may see a fundamental flow in the logic of your code.

I have one question on this - how many records in colProductSelected are potentially being updated?

If one, you do not need the ForAll around UpdateIf, however (which I think is the case) if you are doing more, then you are only testing the current one with the first bit of code. This needs to be written into the collection at the creation of each item.

 

@WarrenBelz  So there are 9 items in the gallery, but only that one item that's selected from the gallery with the same ID in the collection should be updated in the colProductSelected.  I just tested removing the ForAll function, but the initial problem persists. 

Helpful resources

Announcements
Power Platform Conf 2022 768x460.jpg

Join us for Microsoft Power Platform Conference

The first Microsoft-sponsored Power Platform Conference is coming in September. 100+ speakers, 150+ sessions, and what's new and next for Power Platform.

365 EduCon 768x460.png

Microsoft 365 EduCon

Join us for two optional days of workshops and a 3-day conference, you can choose from over 130 sessions in multiple tracks and 25 workshops.

Users online (3,244)