locked
Creating new records and setting default relationships and values - VS2015 LS HTML C# RRS feed

  • Question

  • I'm having problems creating new records, setting up the default relationships automatically, and setting a default value based on a query. Here's an very simplified example of what I'm trying to do:

    Customer.ID
    Customer.Name

    Location.ID
    Location.Name
    Location.Customer (relates to Customer)

    Product.ID
    Product.Name
    Product.ListPrice

    Order.ID
    Order.Name
    Order.Product (relates to Product)
    Order.Price
    Order.Customer (relates to Customer)
    Order.Location (relates to Location)

    I've got data, screens, and interactions for Customers, Locations, and Product all set and working fine.

    I'm using a grid that shows the Locations filtered by Customer. That works, and it only shows Locations for the desired Customer.

    Now, I want to add a Order by selecting the Location from the grid and clicking on an "Add Order" button.
    I get the AddEditOrder screen to appear, and the Location field is set correctly (from Locations grid).
    I don't show the Location picker on the AddEditOrder screen since the user really shouldn't be changing it in this example.

    I've tried several ways of setting Order.Customer programmatically, but I'm not having any luck.
    I don't need the user to see it at this point, but I do want it in the Order record for use on other screens, queries, and reports without having to join/relate through the Location field.

    I though I would be able to do something like Order.Customer = Order.Location.Customer, or by passing the Customer as a parameter from the Location screen to the AddEditOrder screen, but I can't get it to work.

    I also want to set the default value of Order.Price to the Product.ListPrice based on the Order.Product picker (a pulldown). Once the default price pops in, I want the user to be able to override the price on the Order.  The pulldown works and the Order.Product field is set correctly, but I can't figure out how to copy Product.ListPrice into Order.Price when the user makes the Product selection.

    I'm using VS2015 with Lightswitch HTML C#. I've spent hours looking through docs and examples. Any help would be greatly appreciated.

    Thanks!

    Friday, January 29, 2016 10:22 PM

All replies

  • Got the first part figured out...

    CHOICE #1 - For generic defaults whenever a record is created, use the "myapp.[EntityName].created" method under the HTML Client perspective in the entity designer.
    ~~~~~~~~~~~~~~~~~   
    myapp.Order.created = function (entity) {
       myapp.activeDataWorkspace.ApplicationData.Customers_SingleOrDefault(*ID*).execute().then(function (CustomersQuery) {
            entity.Customer = CustomersQuery.results[0];
        });
    }
    ~~~~~~~~~~~~~~~~~

    Where "*ID*" is the hardcoded ID of the default Customer (not realistic in my example since I want the default to vary based on the Location, but handy to know for other cases).
    Note: the Customers_SingleOrDefault() query could be replaced by other available queries for the entity.



    CHOICE #2 - For defaults for the screen, based on things that are available at screen time (exactly what I need in this case), use the myapp.[ScreenName].created method under "Write Code - Created" pulldown in the screen designer.

    ~~~~~~~~~~~~~~~~~
    myapp.AddEditOrder.created = function (screen) {
        myapp.activeDataWorkspace.ApplicationData.Customers_SingleOrDefault(screen.Order.Location.Customer.Id).execute().then(function (CustomersQuery) {
            screen.Order.Customer = CustomersQuery.results[0];
        });
    }
    ~~~~~~~~~~~~~~~~~
    Wrap this in an "if !screen.Order.Customer" statement to keep it from running if you're using the same screen for edits.
    Note: the "screen.Order.Location.Customer.Id" parameter works for me because Location is set automatically when I launch the screen from the Locations grid.

       
    Saturday, January 30, 2016 9:25 PM