none
HTML Client: Settng Defaults to a new record from another record in another table

    Question

  • Hi

    This is easily achieved in the LS SL client, but so far I am unable to set default values in the HTML client as described in this gem of a resource: http://msdn.microsoft.com/en-us/library/jj733572.aspx (see the 1st tutorial).

    But I need to copy fields from the selected record of the parent table from which I invoked the AddAndEditNew screen.

    Please assist.

    Many thanks, Mark.


    Mark


    • Edited by ITPSB Thursday, July 04, 2013 1:28 AM typo
    Wednesday, July 03, 2013 8:27 AM

Answers

  • This may help:

    Dynamically Creating Records In The LightSwitch HTML Client

    myapp.AddEditOrder.DynamicAddOrderDetail_execute = function (screen) {
    
        // Make a new OrderDetail
    
        var newOrderDetail = new myapp.OrderDetail();
    
        // Set the Order property
    
        // Whenever you have associated Entities, there will 
    
        // be a .set[Entity Name] method available
    
        newOrderDetail.setOrder(screen.Order);
    
        // Set the Quantity
    
        newOrderDetail.OrderQuantity = 0;
    
    
        // Try to find a Product
    
        var Products = screen.details.dataWorkspace.ApplicationData.Products
    
            .load().then(function (results) {
    
                // Try to get the first Product
    
                var FirstProduct = results.results[0];
    
                // Did we find a first Product?
    
                if (FirstProduct != undefined && FirstProduct != null) {
    
                    // Set the first Product as the Product for the OrderDetail
    
                    newOrderDetail.setProduct(FirstProduct);
    
                }
    
            });
    
    }
    


    The Visual Studio LightSwitch Marketplace

    http://LightSwitchHelpWebsite.com

    • Marked as answer by ITPSB Wednesday, July 03, 2013 3:08 PM
    Wednesday, July 03, 2013 12:25 PM

All replies

  • This may help:

    Dynamically Creating Records In The LightSwitch HTML Client

    myapp.AddEditOrder.DynamicAddOrderDetail_execute = function (screen) {
    
        // Make a new OrderDetail
    
        var newOrderDetail = new myapp.OrderDetail();
    
        // Set the Order property
    
        // Whenever you have associated Entities, there will 
    
        // be a .set[Entity Name] method available
    
        newOrderDetail.setOrder(screen.Order);
    
        // Set the Quantity
    
        newOrderDetail.OrderQuantity = 0;
    
    
        // Try to find a Product
    
        var Products = screen.details.dataWorkspace.ApplicationData.Products
    
            .load().then(function (results) {
    
                // Try to get the first Product
    
                var FirstProduct = results.results[0];
    
                // Did we find a first Product?
    
                if (FirstProduct != undefined && FirstProduct != null) {
    
                    // Set the first Product as the Product for the OrderDetail
    
                    newOrderDetail.setProduct(FirstProduct);
    
                }
    
            });
    
    }
    


    The Visual Studio LightSwitch Marketplace

    http://LightSwitchHelpWebsite.com

    • Marked as answer by ITPSB Wednesday, July 03, 2013 3:08 PM
    Wednesday, July 03, 2013 12:25 PM
  • Hi Michael

    Your suggestion worked like a charm!

    Many thanks indeed.

    FYI, this is what the code looks like for my specific requirement:

    myapp.ViewStoreGroup.addDynamicSurvey_execute = function (screen) {
        var newSurvey = new myapp.Survey();
        newSurvey.setStore(screen.GroupList.selectedItem.Store);
        // Save all changes on the screen
        return myapp.activeDataWorkspace.ApplicationData
            .saveChanges();
    };


    Mark

    Wednesday, July 03, 2013 3:08 PM
  • Hi Michael

    Your suggestion worked like a charm!

    Many thanks indeed.

    FYI, this is what the code looks like for my specific requirement:

    myapp.ViewStoreGroup.addDynamicSurvey_execute = function (screen) {
        var newSurvey = new myapp.Survey();
        newSurvey.setStore(screen.GroupList.selectedItem.Store);
        // Save all changes on the screen
        return myapp.activeDataWorkspace.ApplicationData
            .saveChanges();
    };


    Mark

    That is a nice clean bit of code that does a lot.

    I Love LightSwitch :)


    The Visual Studio LightSwitch Marketplace

    http://LightSwitchHelpWebsite.com

    Wednesday, July 03, 2013 4:10 PM
  • Hi Michael

    Agree. I absolutely love LightSwitch! It has been an absolute saviour and there's no other way I could have achieved what I have in such a short time using any other tool.

    If you have a moment, please consider a work-flow issue following on from the above that I have bumped up against.

    On dynamic creating a new Survey record (as a child of the Stores table), rather than return to the originating "Store Details" tab on refreshing, I want the user to progress to the next nominated tab.

    I took a stab at it by adding a 2nd refresh as shown below:

    myapp.ViewStoreGroup.addDynamicSurvey_execute = function (screen) {
        var newSurvey = new myapp.Survey();
        newSurvey.setStore(screen.GroupList.selectedItem.Store);
        // Save all changes on the screen
        screen.showTab("StartedSurveys");
        return myapp.activeDataWorkspace.ApplicationData
            .saveChanges().then(function ()
            {
                // Refresh the Surveys
                screen.showTab("StoreDetail");
            })
       screen.showTab("StartedSurveys");
    };

    But that results in this error message "Cannot perform this operation while navigation is in progress".

    Interestingly, if I click OK to dismiss the message I get the desired result i.e. I wind up in the "StartedSurveys" tab.

    Any assistance will be appreciated.

    Thanks again, Mark.


    Mark

    Thursday, July 04, 2013 1:57 AM
  • This should work:

    myapp.ViewStoreGroup.addDynamicSurvey_execute = function (screen) {
        var newSurvey = new myapp.Survey();
        newSurvey.setStore(screen.GroupList.selectedItem.Store);
        // Save all changes on the screen
        screen.showTab("StartedSurveys");
        return myapp.activeDataWorkspace.ApplicationData
            .saveChanges().then(function ()
            {
                screen.showTab("StartedSurveys");
            })
    };


    The Visual Studio LightSwitch Marketplace

    http://LightSwitchHelpWebsite.com

    Thursday, July 04, 2013 12:17 PM
  • Hi Michael

    Thanks for assisting with this issue, but after applying your suggested change the app behaves no differently.

    I'll experiment some more today and hopefully I'll come up with something.

    What appears to happen is the "saveChanges()" returns the user to the "ViewStoreGroup" screen and that navigation action takes priority over the 2nd navigation to the "StartedSurveys" screen.

    The following is a full step-by-step description of what I observe:

    (1) On clicking a store ViewStoreGroup screen to fire the above "myapp.ViewStoreGroup.addDynamicSurvey_execute" function,  on occasions I am returned to the "ViewStoreGroup" screen and other times (randomly without doing anything differently) I get the error message "Cannot perform this operation while navigation is in progress".

    (2) I dismiss the error message and am taken to the "StartedSurveys" screen but the newly created "Survey" record doesn't appear. However if I navigate away from the "StartedSurveys"  screen and start the process again, the previously created "Survey" record is present and the newly created one is again absent.

    Thanks again, Mark.


    Mark


    • Edited by ITPSB Friday, July 05, 2013 1:43 AM typo
    Friday, July 05, 2013 1:41 AM
  • Oh I forgot about that.

    That is really bad of me because I wrote an article about it:

    Deleting Data In The Visual Studio LightSwitch HTML Client

     Anyway, I think what you need is applyChanges:

    • commitChanges      
      • Runs validation on the current active screen tab
      • If there are no validation errors, calls the data service saveChanges()
      • Navigates back to previous screen
    • applyChanges      
      • Runs validation on the current active screen tab
      • If there are no validation errors, calls the data service saveChanges()
    • cancelChanges      
      • Invokes data service discardChanges()
      • Navigates back to previous screen
    • discardChanges      
      • Invokes data service discardChanges()

    The Visual Studio LightSwitch Marketplace

    http://LightSwitchHelpWebsite.com

    Friday, July 05, 2013 2:00 AM
  • (2) I dismiss the error message and am taken to the "StartedSurveys" screen but the newly created "Survey" record doesn't appear. However if I navigate away from the "StartedSurveys"  screen and start the process again, the previously created "Survey" record is present and the newly created one is again absent.

    Thanks again, Mark.


    Mark


    This is solved by:

    New API For Refreshing Data in LightSwitch in Visual Studio 2013

    in VS2012 you would have to use:

    Full Control LightSwitch (ServerApplicationContext And Generic File Handlers And Ajax Calls)

    To get the latest values (it does work, I just used it for that the other day).


    The Visual Studio LightSwitch Marketplace

    http://LightSwitchHelpWebsite.com

    Friday, July 05, 2013 2:05 AM
  • Hi Michael

    Many thanks. Your solution works in VS 2013 Preview which is what I will use when I deploy so problem solved.

    Best regards, Mark.


    Mark

    Friday, July 05, 2013 3:15 AM