Fill up a new record with data from the last entry of a list in LightSwitch HTMLClient RRS feed

  • Question

  • I have a list of items for which there is an Add button to add items to the list. I would like to pre-populate the Add Screen fields with fields from the last item in the list (sorted by date). Any ideas on how to do this for LightSwitch HTMLClient? I created a query for retrieving the desired record from the list and added this query to the Add Screen, but in the

    myapp.AddEditItem.created = function(screen){} I cannot get hold of the query for the last record.

    Thanks in Advance!

    Thursday, December 5, 2013 10:42 AM


All replies

  • You can use beforeShown. See: Visual Studio LightSwitch Screen Navigation and Advanced JavaScript Examples

        myapp.showAddEditProduct(null, {
            beforeShown: function (addNewScreen) {
                var copied_item = screen.Product;
                var new_item = new myapp.Product();
                new_item.ProductName = copied_item.ProductName + '_copy';
                new_item.ProductPrice = copied_item.ProductPrice;
                // Set Product
                addNewScreen.Product = new_item;

    Unleash the Power - Get the LightSwitch HTML Client book


    Thursday, December 5, 2013 1:12 PM
  • Hi Michael,

    Thanks for the quick answer! This is my override now:

    myapp.AddEditMandatesRunsStatus.created = function (screen) {
        // Write code here.
        myapp.showAddEditMandatesRunsStatus(null, {
            beforeShown: function (addNewScreen) {
                var copied_item = screen.MandatesRunsStatus;
                var new_item = new myapp.MandatesRunsStatus();

                new_item.Allocation = copied_item.Allocation + '_copy';
                new_item.AllocationComment = copied_item.AllocationComment;

                // Set Status
                addNewScreen.MandatesRunsStatus = new_item;

    I am the following error though:

    Any idea why this is happening?


    Thursday, December 5, 2013 3:45 PM
  • You put that code on the page you are coming from, not the page that you are going to.

    If you are a old time web developer like myself this is a very odd thing to get used to.

    Unleash the Power - Get the LightSwitch HTML Client book


    Thursday, December 5, 2013 4:13 PM
  • That makes sense, of course, to put the code the initiating page! :). The code still produced the same error somehow. I eventually got to fill us the AddEdit screen using the following code:

    myapp.AddEditMandatesRunsStatus.created = function (screen) {
        myapp.activeDataWorkspace.DataRepository.MandatesRunsStatuses.orderByDescending("EffectiveDate").top(1).execute().then(function (result)
            var copied_item = result.results[0];
            var new_item = screen.MandatesRunsStatus;
            new_item.Allocation = copied_item.Allocation;
            new_item.AllocationComment = copied_item.AllocationComment;
            // ... etc.
    Thanks a lot for your help!

    Thursday, December 5, 2013 5:23 PM