locked
Add records to a many-to-one table. RRS feed

  • Question

  • I am learning Lightswitch by Visual Studio Lightswitch 2015 written by Tim Leung. When I tried to walk through the example in this book, I encountered a problem.

    Now I have two tables named Issue and IssueStatus. I define just one property "StatusDescription" for IssueStatus. There is a one-to-many relationship between Issue and IssueStatus. I also define a button on a screen. When I click it, app should add two issue-status records with status descriptions "Open" and "Closed". The code is as below:

    myapp.SystemSetup.CreatRecords_execute = function (screen) {
       
        var statusOpen = myapp.activeDataWorkspace.ApplicationData.IssueStatusSet.addNew();
        statusOpen.StatusDescription = "Open";
    
        var statusClosed = myapp.activeDataWorkspace.ApplicationData.IssueStatusSet.addNew();
        statusClosed.StatusDescription = "Closed";
    
        return myapp.activeDataWorkspace.ApplicationData.saveChanges();
       
    };
    
    But when I clicked the button, app returned me the error massage "Data in the Issue Status record is invalid". As long as I detach two tables from each other, the code works well. So I guess it is the relationship that causes the problem. Could anyone tell me how to fix it? Any hint will be appreciated.
     

    Thursday, July 14, 2016 1:13 AM

All replies

  • Hi,

    You need to set the parent 'Issue' to link the records together. Something like:

    myapp.SystemSetup.CreatRecords_execute = function (screen) {
       
        var statusOpen = myapp.activeDataWorkspace.ApplicationData.IssueStatusSet.addNew();
        statusOpen.StatusDescription = "Open";
        statusOpen.Issue = screen.Issue;
    
        var statusClosed = myapp.activeDataWorkspace.ApplicationData.IssueStatusSet.addNew();
        statusClosed.StatusDescription = "Closed";
        statusClosed.Issue = screen.Issue;
    
        return myapp.activeDataWorkspace.ApplicationData.saveChanges();
       
    };

    Cheers

    Dave


    Dave Baker | AIDE for LightSwitch | Xpert360 blog | twitter : @xpert360 | Xpert360 website | Opinions are my own. For better forums, remember to mark posts as helpful/answer.

    Thursday, July 14, 2016 8:30 AM
  • Hi, Dave,

    Thanks for your clear reply. I didn't realize that the child can't find his parents automatically. But now I face another problem. After adding the parent link to code, I got js runtime error message "Unable to get property 'entityState' of undefined or null reference". Could you tell me what caused this issue? Thanks again.

    Friday, July 15, 2016 1:08 AM