web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Apps / How to correct the onc...
Power Apps
Answered

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

(0) ShareShare
ReportReport
Posted on by 2

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

 

Categories:
I have the same question (0)
  • RogierE Profile Picture
    879 Moderator on at

    The OnChange is only triggered by a change of your TextBox, but it does not write anything back. What you could do though is use a value varStockQty in your default property and then use the OnChange the code to manipulate that variable: 

     

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

    Set(varStockQty,ThisItem.StockQty);

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

    Reset(Self))

     

    Note: you can copy my code directly, but there might be slight value, source, naming and lannguage differences. So it is better to follow my lead but write/check the code yourself.

     

    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
    2 on at

    Dear Rogier,

     Thanks for your reply.

    I applied the code onchange event and set the default of textbox to VarStockQty

    It worked, if StockQty was less than input, it moved normal, if more than stockQty then it set it to VarStockQty.

     

    BUT the issue noted in the next new selection, As the gallery is based on the collection. When user Add to the collection it always takes the stockQty. While before I set value to 1.

    Because when user cart the item, by default I want it to be 1, then user can input/change as per his requirement.

    By this method, all the new added item takes the stockqty

     

    Any changes in the advised code, where it reset back to 1 for the new one.

     

    please guide

     

     

  • MIA27 Profile Picture
    2 on at

    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

  • RogierE Profile Picture
    879 Moderator on at

    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
    2 on at

    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
     
     
     
  • mmbr1606 Profile Picture
    14,629 Super User 2026 Season 1 on at

    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
    2 on at

    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.

  • Verified answer
    RogierE Profile Picture
    879 Moderator on at

    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
    2 on at

    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.

     

  • RogierE Profile Picture
    879 Moderator on at

    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)

     

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Users!

Kudos to our 2025 Community Spotlight Honorees

Congratulations to our 2025 community superstars!

Congratulations to the March Top 10 Community Leaders!

These are the community rock stars!

Leaderboard > Power Apps

#1
11manish Profile Picture

11manish 534

#2
WarrenBelz Profile Picture

WarrenBelz 416 Most Valuable Professional

#3
Valantis Profile Picture

Valantis 306

Last 30 days Overall leaderboard