Skip to main content

Notifications

Community site session details

Community site session details

Session Id :
Power Apps - Building Power Apps
Answered

How to correct the onchange event code for updating a field in the gallery

(0) ShareShare
ReportReport
Posted on by

I am working on Teams power app.

 

Trying to use the below code in the gallery, that If the stockQty is less than the user's Input Qty then error message and set the Qty as the stock Qty

 

The default property of the TextBox is 1, therefore I put OnChange prorperty of the text box the below code:

If(ThisItem.StockQty<Value(Self.Value),ThisItem.StockQty;Notify("stock Qty is less than order",NotificationType.Warning,3000),Value(Self.Value))

 

During Testing the code found, Currenty Stock is 5,  when the Input Qty is more than it brings the notification , but does not set the StockQty as the value the textbox.

 

I want if the user Input Qty is less than stock, then let the user qty as it is, BUT if User's Input Qty is more than Stock, the set the textbox value to StockQty.

 

Please advise what changes to be done in my above code.

 

MIA27_0-1713706288244.png

 

  • Verified answer
    RogierE Profile Picture
    862 Super User 2025 Season 1 on at
    Re: How to correct the onchange event code for updating a field in the gallery

    Of course you can! As long as it is a unique ID you can use this for your Updateif

  • MIA27 Profile Picture
    on at
    Re: How to correct the onchange event code for updating a field in the gallery

    Also please advise for the ID part which I posted in earlier reply. 

    ID=ThisItemID

    The table  is based on Teams Table (Dataverse), therefore in Dataverse there is No ID column by system.

    Can I use PartNo field as it is unique and has no duplication.

     

     

     

  • RogierE Profile Picture
    862 Super User 2025 Season 1 on at
    Re: How to correct the onchange event code for updating a field in the gallery

    Sorry for the typo

    The record {Qty:ThisItem>StockQty}

    Should be {Qty:ThisItem.StockQty}

    so you write the StockQty value of the item in the gallery to its Qty row. 

    Please LIKE the reply and ACCEPT it as the solution if the answer is what you were looking for.
    (to help me grow in community rank and help others find the solution)

     

  • MIA27 Profile Picture
    on at
    Re: How to correct the onchange event code for updating a field in the gallery

    Sorry not getting clear in the below line:

     

    UpdateIf(CollectCardItems,ID=ThisItemID,{Qty:ThisItem>StockQty});

     

     

    ID=ThisItemID

    The table  is based on Teams Table (Dataverse), therefore in Dataverse there is No ID column by system.

    Can I use PartNo field as it is unique and has no duplication.

     

    {Qty:ThisItem> 

    Is Qty is declaration for the situation?.

    ThisItem - is referring to what ?.

     

    Please guide, I want to understand and then apply to my application.

     

  • Verified answer
    RogierE Profile Picture
    862 Super User 2025 Season 1 on at
    Re: How to correct the onchange event code for updating a field in the gallery

    Okay, so now what you would have to do is update your value in the collection

     

    If(ThisItem.StockQty<Value(Self.Value),

    UpdateIf(CollectCardItems,ID=ThisItemID,{Qty:ThisItem>StockQty});

    Notify("stock Qty is less than order",NotificationType.Warning,3000);

     

    and keep the default value to Thisitem.Qty

     

    That should do the trick. 

     

    Please LIKE the reply and ACCEPT it as the solution if the answer is what you were looking for.
    (to help me grow in community rank and help others find the solution)

  • MIA27 Profile Picture
    on at
    Re: How to correct the onchange event code for updating a field in the gallery

    once the updatecontext triggers, it impact the on the all items of the carted gallery, I mean on all the row of records.

    Similar test result I already posted in the earlier reply.

    In that case it was Set() function, in your advise it is UpdateContext(). In both coding the default value gets impacted for next new records.

     

    Any other idea.

  • mmbr1606 Profile Picture
    12,112 Super User 2025 Season 1 on at
    Re: How to correct the onchange event code for updating a field in the gallery

    hey @MIA27 

     

    can u try this:

    UpdateContext({currentQty: 1}) // Sets default quantity as 1
    

     

    onchange:

    If(
     Value(TextInput1.Text) > ThisItem.StockQty,
     UpdateContext({currentQty: ThisItem.StockQty}),
     UpdateContext({currentQty: Value(TextInput1.Text)})
    );
    If(
     Value(TextInput1.Text) > ThisItem.StockQty,
     Notify("Stock quantity is less than the order", NotificationType.Warning, 3000)
    )
    

     

    default of text box:

    currentQty
    

     

    Let me know if my answer helped solving your issue.

    If it did please accept as solution and give it a thumbs up so we can help others in the community.



    Greetings

  • MIA27 Profile Picture
    on at
    Re: How to correct the onchange event code for updating a field in the gallery

    I am using a collection based on another gallery. 

    I am developing an Parts Store Inventory application - Power App in Teams - the table are based on Dataverse,

    As shown in the below image,

    User selects from one gallery, then the collection is used in the carted gallery to input the items he needs, then this collection is been used to store the order placed and same time, it will updated to the stock.

    I am stuck in the mid way now. 

     

    MIA27_0-1713772001326.png

     

    On the carted Gallery, in the items property is the collection name 

    CollectCartItems
     
    This collection was build when user clicked the + button on the left gallery by this code:
    Collect(CollectCartItems,ThisItem);
     
    Kindly guide, as it is important to control the order - according to the stock.
     
    regards
     
     
     
  • RogierE Profile Picture
    862 Super User 2025 Season 1 on at
    Re: How to correct the onchange event code for updating a field in the gallery

    Ah, of course. Then you should do the same trick, but not with a variable but with the items in your Gallery. What are you using as Items property for your gallery? a collection or a direct connection to a SharePoint list? Could you share the code of the Items Property in your Gallery? 

  • MIA27 Profile Picture
    on at
    Re: How to correct the onchange event code for updating a field in the gallery

    And also please note, it store the Value of StockQty in VarStockQty,

    And applies to any new added items to collection, which is not correct. for example

    I added an item, it was more than stock, example 7. it correct after change to 7

    but next new record CartQty became 7, while its actual stock is only 5

     

    So it went beyond control.

    It must be controlled, lets say, example must be set to 1 back.

    Pls. guide

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

🌸 Community Spring Festival 2025 Challenge 🌸

WIN Power Platform Community Conference 2025 tickets!

Markus Franz – Community Spotlight

We are honored to recognize Markus Franz as our April 2025 Community…

Kudos to the March Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
WarrenBelz Profile Picture

WarrenBelz 146,660 Most Valuable Professional

#2
RandyHayes Profile Picture

RandyHayes 76,287 Super User 2024 Season 1

#3
Pstork1 Profile Picture

Pstork1 65,999 Most Valuable Professional

Leaderboard