May
5
Written by:
Michael Washington
5/5/2013 8:41 PM
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.
In the normal application program flow, we click the Add Customer button…
We add the Customer in the dialog that is displayed.
We click Save.
The Customer is added to the list.
However, if we click the Add Dynamic Customer button…
The Customer is added to the list immediately.
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
To create the code, we add a button to the screen and call it AddDynamicCustomer.
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
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.
In the normal application program flow, we click the Add Order Detail button…
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.
The Order Detail is added to the list.
However, if we click the Dynamic Add Order Detail button…
The Order Detail is added to the list immediately.
Creating The Complex Example
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)
21 comment(s) so far...
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
|
@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
|
Where is the RSS feed for this blog?!
By Stuart Dobson on
5/8/2013 4:31 AM
|
@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
|
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
|
@Alex - All the examples use a database. Download the sample code and examine it.
By Michael Washington on
7/4/2013 4:32 AM
|
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
|
@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
|
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
|
@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
|
i try ur code its working smooth. but i need html client Grid and add /edit functionalty in same screen.(not popub show). Please replay me
By Athi on
2/17/2015 11:51 PM
|
@Athi - I am sorry but I have no examples.
By Michael Washington on
2/18/2015 4:49 AM
|
Hello
can anyone explain me how do i set the new screen values to the collection on the afterclosed event ?
beforeShown: function (AddEditScreen) { var newOrder = new myapp.tblOrder(); AddEditScreen.tblOrder = newOrder; }, afterClosed: function (addEditScreen, navigationAction) { if (navigationAction === msls.NavigateBackAction.commit) { var newOrder = addEditScreen.tblOrder; screen.tblOrders.addNew(newOrder); <------------------------------ does not appear on the list collection (or appears as a new item not the one with the data from the new screen on the beforeShown event) } }
thank you :)
By Angelo on
4/7/2015 3:44 AM
|
@Angelo - 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
4/7/2015 3:44 AM
|
useful examples, thanks. I try this with a external database , like var newCustomer = new myapp.Customer(); return myapp.activeDataWorkspace.SQLData.saveChanges(); is not work. also try with another tutorial using serverApplicationContext still can't save the new entity to external SQLDatabase. I guess these method is work only for _IntrinsicData. Can you please post a tutorial for post new entity to external database?
By frank on
5/2/2015 7:24 PM
|
@frank - Sorry I don't have any examples.
By Michael Washington on
5/2/2015 7:25 PM
|
Hi Michael, thanks for all of your posts. How do you dynamically add child of a child. I have a Business with Clients with Social Media Accounts. I am able to dynamically add the Clients but the Social Media Accounts show "No Items". Hope you see this. I am stuck.
By Maria Granville on
10/1/2015 7:58 AM
|
@Maria Granville - Please see the example at: http://lightswitchhelpwebsite.com/Blog/tabid/61/EntryId/3288/LightSwitch-HTML-Tree-Administrator.aspx
By Michael Washington on
10/1/2015 8:01 AM
|
Michael, you are a lifesaver. The issue was setting the Clients.selectedItem to the new client and assigning that to the new social media Client. Thank you again.
By Maria Granville on
10/1/2015 1:36 PM
|
I have the code you are using above, and am attempting to add multiple records in a related table:
When an OrderLine table is added, I want whatever quantity is selected to be inserted into the related table like so (on the execute of a button):
screen.OrderLine.QuantityOrdered = 5; var counter = screen.OrderLine.QuantityOrdered;
while (counter >0) { counter--;
var newLine = new myapp.SerialScan(); newLine.setOrderLine(screen.OrderLine); newLine.SerialNumber = null; newLine.DateStamp = '13/05/2016';
var sheets = screen.details.dataWorkspace.DataSource.OrderLines.load().then(function (results) { var sheet = results.results[0]; if (sheet != undefined && sheet != null) { newLine.setOrderLine(sheet); } }); }
however this causes severe time delays of 10-15 seconds per save, any ideas on how I can stop this issue? if i comment out the line starting with var sheets, there is no issues, however this is needed to associate the parts together
thanks for any help on this
By crezzer7 on
5/13/2016 4:18 AM
|
@crezzer7 - I am only guessing but I would move the .load() call before you call the while loop. For more assistance, 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/13/2016 4:21 AM
|