locked
Setting defaults with foreign keys in Lightswitch HTMLClient RRS feed

  • Question

  • Hi

    I have a Lightswitch client app, and I want to set some defaults before the screen loads.

    Text fields and date fields work, all I need to do is add them to the table_Created() under HTMLClient.

    However, when I try to set the foreign key default, using eg. :

    entity.setActiveType (myapp.activeDataWorkspace.MeerkatData.ActiveTypes_SingleOrDefault(1));

    or

    entity.ActiveType = myapp.activeDataWorkspace.MeerkatData.ActiveTypes_SingleOrDefault(1);

    (Which seemed like the correct way to do it) I get
    Unable to get property 'entityState' of undefined or null reference

    My foreign key table is ActiveType (surfaced as ActiveTypes), and the field the main table is keyed to it on is Active

    Thanks

    Friday, May 17, 2013 7:28 PM

Answers

  • OK, I answered my own question:

    setActiveTypes doesn't work, but entity.ActiveType = *does* - see below:

    (Highlighted line does not work)

    • Marked as answer by Angie Xu Tuesday, June 4, 2013 2:13 AM
    Saturday, May 18, 2013 6:05 AM

All replies

  • To mention: I also tried putting code in the _Created on the server, but my breakpoint there never gets called.
    Friday, May 17, 2013 7:34 PM
  • Mark,

    You are sooo close...

    You do want this in the correct place, so to make sure we are talking about the same place: you want this code in the table created() method, which you get to by opening your table from the Solution Explorer -> Server folder -> MeerkatData database -> [Table_Name]. Then when the table is opened, you choose the "HTMLClient" button at the bottom of the screen, and then choose the "created" method from the "<> Write Code" drop down at the top.

    In the created method you want this code:

    myapp.activeDataWorkspace.MeerkatData.ActiveTypes_SingleOrDefault(1).execute().then(function (ActiveTypesQuery) {
        entity.setActiveType = ActiveTypesQuery.results[0];
    });

    What is going on here is that any call to the server to get data is done asynchronously, so LightSwitch uses Promise objects. What this means in reality is that when the server get to this line of code it will create a promise that will be fulfilled when the server gets around to it, but in the meantime LightSwitch will keep moving to the next line of code.

    When the server returns the data, the then() function is called and the code in there is executed.

    So in this case, you cannot assign the Active Type until the server returns that data, which happens when it fulfills it's promise to get back to you and is done (when the then() method is run).

    Happy Computing!


    -Christopher DeMars



    Friday, May 17, 2013 9:36 PM
  • Hi

    Thanks Chris - I definitely have the place right, and changing my code to match yours stops the error coming up, so it seems like it should have worked: but the screen still pops up with the empty dropdown.

    I've screengrabbed my code - I'm definitely getting the correct value, so it there something else wrong?

    Saturday, May 18, 2013 6:03 AM
  • OK, I answered my own question:

    setActiveTypes doesn't work, but entity.ActiveType = *does* - see below:

    (Highlighted line does not work)

    • Marked as answer by Angie Xu Tuesday, June 4, 2013 2:13 AM
    Saturday, May 18, 2013 6:05 AM
  • I'm glad you figured it out.

    Good luck on the rest of your project!


    -Christopher DeMars

    Saturday, May 18, 2013 2:32 PM
  • Hi Christopher,

    I saw your response, and it worked for my situation too- in the sense that I had a foreign key field populated for an HTML client screen.

    Do I assume correctly that what that code does is get the first record ( foreign key) in the related lookup table? If so, how do you code it to find a specific value in that lookup table?

    Here is the code that worked for me:

    myapp.activeDataWorkspace.ApplicationData.StatusSet_SingleOrDefault(1).execute().then(function (result) {
       
            entity.Status = result.results[0];

    Similar to yours.

    What I am trying to do is the following equivalent from the Silverlight side in the html client

            Me.DataWorkspace.ApplicationData.StatusSet.Where(Function(StatusSet) StatusSet.StatusDescription = "Not Started").FirstOrDefault
    

    Can the above HTML screen code be tweaked accordingly?

    Thanks for any help you can provide

    Friday, August 23, 2013 11:45 PM
  • btw sorry if my above question should have been located at end of thread :(
    Friday, August 23, 2013 11:46 PM