none
How to load related entity of external data source in Lightswitch (Visual Studio 2013)

    Question

  • (Cross-posted from stackoverflow: http://stackoverflow.com/questions/21230381/how-to-load-related-entity-of-external-data-source-in-lightswitch-visual-studio)

    I have 2 tables which are both in an Azure SQL Database which is connected to my Lightswitch Sharepoint app. I am doing some manipulation of the data in code, and it appears to be working, except that when I load the entities from one table, I am not able to see the related entities in the other.

    Basically, I have a products table and an invoice lines table. Each invoice line record contains a product code, which relates to the products table PK. I have defined the relationship in Lightswitch, but when I load the invoice line record, I can't see the product information.

    My code is as follows:

    // Select invoice and get products
    myapp.AddEditServiceRecord.InvoicesByCustomer_ItemTap_execute = function (screen) {
        screen.ServiceRecord.InvoiceNumber = screen.InvoicesByCustomer.selectedItem.INVO_NO;
    
        // Delete existing lines (if any)
        screen.ServiceDetails.data.forEach(function (line) {
            line.deleteEntity();
        });
    
        // Add products for selected invoice
        screen.getInvoiceLinesByNumber().then(function (invLines) {
            invLines.data.forEach(function (invLine) {
                invLine.getProduct().then(function (invProduct) {
                    var newLine = new myapp.ServiceDetail();
    
                    newLine.ServiceRecord = screen.ServiceRecord;
                    newLine.ProductCode = invLine.ProductCode;
                    newLine.ProductDescription = invProduct.Description;
                    newLine.CasesOrdered = invLine.Cases;
                });            
            });
        });
    };

    The idea is that a list of invoices are on the screen 'InvoicesByCustomer', and the user clicks one to add the details of that invoice to the 'ServiceRecord' table. If I comment out thenewLine.ProductDescription = invProduct.Description line it works perfectly in adding the correct product codes and cases values. I have also tried a few other combinations of the below code, but in each case the related product entity appears as undefined in the Javascript debugger.

    EDIT: I also read this article on including related data (http://blogs.msdn.com/b/bethmassi/archive/2012/05/29/lightswitch-tips-amp-tricks-on-query-performance.aspx) and noticed the section on 'Static Spans'. I checked and this was set to 'Auto (Excluded)' so I changed it to 'Included', but unfortunately this made no difference. I'm still getting theinvProduct is undefined message. I also tried simply invLine.Product.Description but it gives the same error.

    Wednesday, January 22, 2014 1:47 PM

Answers

  • The solution in this case was a simple one. My data was wrong, and therefore Lightswitch was doing it's job correctly!

    In my Invoices table, the product code was something like 'A123' whereas in my Products table, the product code was 'A123 ' (padded with spaces on the right). When doing SQL queries against the data, it was able to match the records but Lightswitch (correctly) saw the 2 fields as different and so could not relate them.

    I may have wasted several hours on this, but it's not wasted when something has been learnt...or so I'll tell myself!

    • Marked as answer by soupyc87 Sunday, January 26, 2014 7:41 PM
    Sunday, January 26, 2014 7:40 PM