You are here:   Blog
Register   |  Login

 

May 5

Written by: Michael Washington
5/5/2013 8:41 PM  RssIcon

image

You may have situations where you need to dynamically create records. Typically you will want to do this when you don’t want to navigate away to a new page to add a record.

Simple Example

For the simple example, we will show how to dynamically add a single simple Entity.

In this example we will add a Customer.

image

In the normal application program flow, we click the Add Customer button…

image

We add the Customer in the dialog that is displayed.

We click Save.

image

The Customer is added to the list.

image

However, if we click the Add Dynamic Customer button…

image

The Customer is added to the list immediately.

image

When we click on a record to edit it, or otherwise try to navigate away from the page, we will be asked to save the record.

(we will also cover how to save the records immediately).

Creating the Simple Example

image

To create the code, we add a button to the screen and call it AddDynamicCustomer.

image

We right-click on AddDynamicCustomer in the View Model and select Edit Execute Code.

We use the following code for the method:

myapp.BrowseCustomers.AddDynamicCustomer_execute = function (screen) {
    // Add a new Customer
    var newCustomer = new myapp.Customer();
    // Set the Status
    newCustomer.CustomerName = "New";
    // Set the Date
    newCustomer.CustomerAge = 0;
};

 

Note, If we wanted to save the records immediately we would add the following code to the method:

 

    // Save all changes on the screen
    return myapp.activeDataWorkspace.ApplicationData
        .saveChanges().then(function () {
            // Refresh the Customers
            screen.getCustomers();
        });

 

Complex Example

image

When a table (an Entity) has associated Entities, you have to perform a few extra steps. These extra steps may include searching a related Entity for a record to use as a default value.

In this example we have an OrderDetail Entity that has an associated Order and Product Entities.

image

In the normal application program flow, we click the Add Order Detail button…

image

We select the Product and set the Order Quantity in the dialog that is displayed.

The Order is also set but it is not shown because LightSwitch knows what to set because we have associated the Order Details collection with an Order on the screen and we are adding a record to that collection.

image

The Order Detail is added to the list.

image

However, if we click the Dynamic Add Order Detail button…

image

The Order Detail is added to the list immediately.

Creating The Complex Example

image

We create a button and then right-click on its method and select Edit Execute Code.

We use the following code for the method:

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);
            }
        });
}

 

Download Code

The LightSwitch project is available at http://lightswitchhelpwebsite.com/Downloads.aspx

(you must have Visual Studio 2012 Update 2 installed to run the code)

10 comment(s) so far...


Gravatar

Re: Dynamically Creating Records In The LightSwitch HTML Client

Loosely related, when I select on a (parent) entity on a Browse screen, I want to add logic that will select a particular View or Edit screen to appear. I want the logic to be based on one or more property values from the parent entity I clicked on on the Browse screen (e.g. I want a specific screen to be displayed based on entity values of the entity that was picked).
Any ideas?

By Michael Herman (Toronto) on   5/6/2013 6:12 AM
Gravatar

Re: Dynamically Creating Records In The LightSwitch HTML Client

@Michael Herman - You will want to post on the Official LightSwitch Forums at http://social.msdn.microsoft.com/Forums/en-US/lightswitch/threads

By Michael Washington on   5/6/2013 6:31 AM
Gravatar

Re: Dynamically Creating Records In The LightSwitch HTML Client

Where is the RSS feed for this blog?!

By Stuart Dobson on   5/8/2013 4:31 AM
Gravatar

Re: Dynamically Creating Records In The LightSwitch HTML Client

@Stuart Dobson - There is a RSS icon next to the date of each blog post.

By Michael Washington on   5/8/2013 4:34 AM
Gravatar

Re: Dynamically Creating Records In The LightSwitch HTML Client

Hi
but how can use the same logically 'Dynamically Creating Records ' with a DataBase attached?

Thanks
Regards

By Alex on   7/4/2013 1:38 AM
Gravatar

Re: Dynamically Creating Records In The LightSwitch HTML Client

@Alex - All the examples use a database. Download the sample code and examine it.

By Michael Washington on   7/4/2013 4:32 AM
Gravatar

Re: Dynamically Creating Records In The LightSwitch HTML Client

I tried something similar:
var photo = new myapp.Photo();
photo.photo1 = dataURL.substring(dataURL.indexOf(",") + 1);

But it says photo1 is undefined. More generally, I cannot access any property of photo, the're all undefined.
Any clue why?
Tks.

By francoisM on   10/16/2013 4:22 AM
Gravatar

Re: Dynamically Creating Records In The LightSwitch HTML Client

@francoisM - You have to be in a render not a screen method, also check that you did not misspell something or that the casing is correct. Otherwise post more code to the LightSwitch forums at: http://social.msdn.microsoft.com/Forums/vstudio/en-US/home?forum=lightswitch

By Michael Washington on   10/16/2013 4:24 AM
Gravatar

Re: Dynamically Creating Records In The LightSwitch HTML Client

Hi Michael,

How could I dynamically add a certain number of records? for eg, 12 records at a press of button. I tried to use for loop but couldn't do that.

Thanks

By aswinboy on   8/5/2014 8:16 PM
Gravatar

Re: Dynamically Creating Records In The LightSwitch HTML Client

@aswinboy - See Full Control LightSwitch (ServerApplicationContext And Generic File Handlers And Ajax Calls) http://lightswitchhelpwebsite.com/Blog/tabid/61/EntryId/174/Full-Control-LightSwitch-ServerApplicationContext-And-Generic-File-Handlers-And-Ajax-Calls.aspx

By Michael Washington on   8/5/2014 8:24 PM

Your name:
Gravatar Preview
Your email:
(Optional) Email used only to show Gravatar.
Your website:
Title:
Comment:
Security Code
CAPTCHA image
Enter the code shown above in the box below
Add Comment   Cancel 
Microsoft Visual Studio is a registered trademark of Microsoft Corporation / LightSwitch is a registered trademark of Microsoft Corporation