Skip to main content

Notifications

Power Apps - Power Apps Pro Dev & ISV
Answered

Real-Time Stock Upate Issue in Multi-User Order Application

Posted on by 1
Hi guys i have some issue in my  orders app in updating the  multiple stock values.Let me explain my issue.
  • Initial State:
    • Stock for "Apples" is 10 in the Products table.
    • Two users, User A and User B, place orders at the same time:
      • User A orders 6 apples.
      • User B orders 5 apples.
  • Problem:
    • Without synchronization, both users might see the initial stock as 10.
    • Orders might process independently, resulting in overselling (6 + 5 = 11) when the stock should only allow up to 10 apples.
  • Expected Behavior:
    • If User A confirms their order first, the stock should update to 4.
    • User B should only be able to order up to 4 apples.
    • If User B tries to order 5 apples after User A’s order, the app should prevent it.
This is my code

Set(myorder,orderid.Run().orderid);

Patch(

    Orders,LookUp(Orders,Title=myorder),

    {

        'Item Count': Value(Label21.Text),

        'Total Amount': Value(Label9.Text)

    }

);

ForAll(

    Gallery2.AllItems,

    Patch(

        'product Items',

        Defaults('product Items'),

        {

            Title: ThisRecord.Label5.Text,

            Quantity: ThisRecord.Label6.Text,

            Amount: ThisRecord.Label7.Text,

            'Order ID':{Value:"",Id:LookUp(Orders,Title=myorder,ID)}

        }

    )

);

ForAll(

    Gallery2.AllItems,

    Patch(

        Products,LookUp(Products,Title=Label5.Text),{Stock:Value(ThisRecord.Label6_1.Text)})

);

 

Refresh(Products);

UpdateContext({varplace:true});

  • Verified answer
    ronaldwalcott Profile Picture
    ronaldwalcott 903 on at
    Real-Time Stock Upate Issue in Multi-User Order Application
    This is a classic issue and in other programming solutions was solved by essentially locking the record for updates.
    Other ways to solve this are
    - show a low stock message before the order is completed (this manages expectations and will allow you to cancel an order if the product count goes negative)
    - continually check the stock using a timer
    - check the stock before performing the update
    - check the stock after performing the update (to adjust the order if necessary)
    - you could simulate a record lock for updates but I don't know how much delay that would add to the ordering update process.
  • lbendlin Profile Picture
    lbendlin 7,828 on at
    Real-Time Stock Upate Issue in Multi-User Order Application
    And this is a conflict between just two people! Imagine rolling that out to 20 or more...
     
    You need to drastically increase the refresh frequency, and include a refresh as the first part of the order placement code. But even then there is no guarantee that your latency is low enough to avoid situations like you describe.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

November 2024 Newsletter…

November 2024 Community Newsletter…

Community Update Oct 28…

Power Platform Community Update…

Tuesday Tip #7 Community Profile Tips…

Welcome to a brand new series, Tuesday Tips…

Leaderboard

#1
WarrenBelz Profile Picture

WarrenBelz 143,297

#2
RandyHayes Profile Picture

RandyHayes 76,308

#3
Pstork1 Profile Picture

Pstork1 63,890

Leaderboard