none
Create Order, Add Products and Products Details RRS feed

  • Question

  • Hey guys, to me this seems like a very basic question, and should have a fairly simple solution, yet I have been able to find not a single article and/or tutorial that covers this basic question.

    I have a simple Customer, Order, Order Details, and Products type of setup and their respective relationships. Customer to Many Orders, Orders to Many Order Details, and Order Details to One Product. My question is when I create an Order and add an Order Details to that order, upon selecting a Product I want to populate that Products associated Unit Price. How should I do this? Below is a visualization of my tables and the result I am going for...

    Customers Orders Order Details Products
    CustomerID OrderNumberID OrderDetailsID ProductID
    CustomerName OrderNumber Order_OrderDetailsID UnitPrice


    Add New Order Details to Order Number #101 (CreateOrderDetails)
    Product (Dropdown menu, upon selection update Unit Price----->): Unit Price: $11.00


    Thank you in advance for your assistance!
    Tuesday, May 27, 2014 3:02 PM

Answers

  • I would do this in the Product_Changed event of the OrderDetails entity.

    Check the Product really IsChanged and IsNot Nothing and then copy its UnitPrice to the OrderDetails UnitPrice field.

    If Me.Details.Properties.Product.IsChanged AndAlso 
       Product IsNot Nothing Then
        UnitPrice = Product.UnitPrice
    End If

    You have to check IsChanged because the Changed event is called as the data is loaded from the database.

    Using the Changed event means your code will run however the OrderDetails is created/edited. It does not rely on you remembering to write this functionality into every screen that deals with Orders.


    Simon Jones
    If you found this post helpful, please "Vote as Helpful". If it actually answered your question, please remember to "Mark as Answer". This will help other people find answers to their problems more quickly.

    • Proposed as answer by Angie Xu Thursday, June 5, 2014 12:13 PM
    • Marked as answer by Angie Xu Friday, June 6, 2014 1:41 AM
    Wednesday, May 28, 2014 10:36 AM

All replies

  • You did not specify whether you're using SL or HTML client.  That would determine the entry point which is used to capture the product selection.  For example.  In HTML Client you would set a change handler on product which would set the Unit Price.  One way is to use binding like this:

    myapp.AddEditOrder.Product_postRender = function (element, contentItem) {
        // Write code here.
        contentItem.dataBind("value", function (newValue) {
    
            var price = contentItem.data.UnitPrice;
            if (price && price != newValue.UnitPrice) { contentItem.data.UnitPrice = newValue.UnitPrice };
        });
    };

    I have a screen called AddEditOrder with a table of OrderDetails each row having a Details Picker for Product.  In the postRender Code of Product we add binding.  The contentItem.data is the OrderDetails entity and the newData is the newly selected Product.  This binding basically says...when the "value" of the OrderDetails.Product changes, then set the OrderDetail.UnitPrice to the newly selected Product.UnitPrice. 

    One thing you may wish to consider is that this binding also sets the UnitPrice when the screen is first rendered, potentially overwriting an older price on the OrderDetail.  If this isn't your desired behavior you may consider adding a change listener like so:

    myapp.AddEditOrder.Product_postRender = function (element, contentItem) {
        // Write code here.
        function updatePrice() {
            contentItem.data.UnitPrice = contentItem.value.UnitPrice;
        }
        contentItem.addChangeListener("value", updatePrice);
    
        // Clean up when screen is closed.
        contentItem.handleViewDispose(function () {
            contentItem.removeChangeListener(
                "value", updatePrice);
        });
    };

    HTH,

    Josh

     
    • Edited by joshbooker Wednesday, May 28, 2014 1:32 AM
    Tuesday, May 27, 2014 10:43 PM
  • I would do this in the Product_Changed event of the OrderDetails entity.

    Check the Product really IsChanged and IsNot Nothing and then copy its UnitPrice to the OrderDetails UnitPrice field.

    If Me.Details.Properties.Product.IsChanged AndAlso 
       Product IsNot Nothing Then
        UnitPrice = Product.UnitPrice
    End If

    You have to check IsChanged because the Changed event is called as the data is loaded from the database.

    Using the Changed event means your code will run however the OrderDetails is created/edited. It does not rely on you remembering to write this functionality into every screen that deals with Orders.


    Simon Jones
    If you found this post helpful, please "Vote as Helpful". If it actually answered your question, please remember to "Mark as Answer". This will help other people find answers to their problems more quickly.

    • Proposed as answer by Angie Xu Thursday, June 5, 2014 12:13 PM
    • Marked as answer by Angie Xu Friday, June 6, 2014 1:41 AM
    Wednesday, May 28, 2014 10:36 AM