locked
Adding a record to a table using javascript RRS feed

  • Question

  • Hello,  I am getting a javascript error when I attempt to add a record to the database:

    Unable to get value of the property 'entityState': object is null or undefined

    I followed this guide to add a record to a table: Dynamically add a record.  I have a table that contains foreign keys to 2 other tables.

    The table I am trying to add to is called "GroupSubGroupPair".  It contains a key "Id" field, the standard LS fields (Created, CreatedBy, RowVersion), a foreign key called "GroupSubGroupPair_Group" to the Group table, and a foreign key called "GroupSubGroupPair_SubGroup" to the SubGroup table.  

    In the Group table there is a record with Id = 7

    In the SubGroup table there is a record with Id = 67

    I have a button that has execute code with the following: 

    myapp.AddEditSubGroupChildGroups.submitSubGroup_execute = function (screen) {


    var newGroupSubGroupPair = new myapp.GroupSubGroupPair;

    newGroupSubGroupPair.setGroup(7);          //debugging throws the javascript error on this line
            newGroupSubGroupPair.setSubGroup(67);

            //save changes.
            myapp.activeDataWorkspace.ApplicationData.saveChanges();

    };

    I hard coded the values 7 and 67 just for debugging purposes.  In the working code I will have these as integer variables.  When I manually add these values to the table the record is inserted with no problem.  I do delete the record after my manual insert to avoid duplicate records but the key on the table would still be different.

    Can anyone help as to why I am getting an error when this executes?

    Thank you.


    Thursday, October 1, 2015 7:57 PM

Answers

  • Hey Matt,

    It's something like this (air code - not tested)

    myapp.AddEditSubGroupChildGroups.submitSubGroup_execute = function (screen) { //execute query passing screen property - assumes your table is called Group myapp.activeDataWorkspace.ApplicationData.Groups_SingleOrDefault(screen.parentGroupId).execute() //then, when promise completes .then( function (results) { //if there are results from query if (results.results.length > 0) { //get the first result var parentGroup = results.results[0]; var childGroup = screen.Groups.selectedItem; var newGroupSubGroupPair = new myapp.GroupSubGroupPair(); newGroupSubGroupPair.setGroup(parentGroup); newGroupSubGroupPair.setSubGroup(childGroup); myapp.activeDataWorkspace.ApplicationData.saveChanges(); } }, function (error) { alert(error); } ); };

    PS...I may have reversed parent & child...not sure I understood which was which.

    HTH,

    Josh


    • Edited by joshbooker Friday, October 2, 2015 5:29 PM PS
    • Proposed as answer by ADefwebserver Monday, October 5, 2015 9:30 PM
    • Marked as answer by mountainDev Tuesday, October 6, 2015 1:07 AM
    Friday, October 2, 2015 5:17 PM

All replies

  • What are your actual property names?

    Are they Group and SubGroup?

    or are they GroupSubGroupPair_Group and GroupSubGroupPair_SubGroup

    If the latter, then try the following:

    myapp.AddEditSubGroupChildGroups.submitSubGroup_execute = function (screen) {
    
    //notice parenthesis on next line
    var newGroupSubGroupPair = new myapp.GroupSubGroupPair();
    
    newGroupSubGroupPair.setGroupSubGroupPair_Group(7);          //debugging throws the javascript error on this line
            newGroupSubGroupPair.setGroupSubGroupPair_SubGroup(67);
    
            //save changes.
         myapp.activeDataWorkspace.ApplicationData.saveChanges();
    
    };

    Alternatively, rename the properties unless you have a compelling reason for confusing names.

    HTH,

    Josh


    • Edited by joshbooker Friday, October 2, 2015 1:59 AM
    Friday, October 2, 2015 1:58 AM
  • Hi Josh,

    Thanks for the suggestions.  Yes, the actual names of the database fields are "GroupSubGroupPair_Group" and "GroupSubGroupPair_SubGroup".  I did try your suggestion but it did not work.  Also the reason I am using "setGroup" and "setSubGroup" is that is what comes up from Intellisense.

    Another thought I had is what is the correct type to but used in the setGroup(whatTypehere??) command?  Intellisense says it is a "value" type.  Is that one of my "Group" entities or an integer as defined by the database field?

    -Matt

    Friday, October 2, 2015 4:08 PM
  • Hey Matt,

    I understand.  Yes these are navigation properties so, rather than set them by ID, you have to set them to a valid Group or SubGroup entities respectively.

    There are a few ways to do this but the best way for your case depends on what values you have on the screen to work with. 

    For example, are the Group and SubGroup entities on the screen already or do we have to look them up in the database by ID?

    HTH,

    Josh

    Friday, October 2, 2015 4:19 PM
  • Josh,

    The screen I am working  displays a list of group entities.  The user will select which group they would like added to the parent group.   I was using the "screen.Groups.selectedItem.Id" property and passing this to one of the "set" functions.    The screen also has a parameter called "parentGroupId" which is an integer value.  This was the other value I was passing to the second "set" function.  So according to your advice, this would need to be looked up in the database to create the entity for the parent group.  

    Would you mind showing me what this looks like?

    Thank you.

    -Matt

    Friday, October 2, 2015 4:37 PM
  • Hey Matt,

    It's something like this (air code - not tested)

    myapp.AddEditSubGroupChildGroups.submitSubGroup_execute = function (screen) { //execute query passing screen property - assumes your table is called Group myapp.activeDataWorkspace.ApplicationData.Groups_SingleOrDefault(screen.parentGroupId).execute() //then, when promise completes .then( function (results) { //if there are results from query if (results.results.length > 0) { //get the first result var parentGroup = results.results[0]; var childGroup = screen.Groups.selectedItem; var newGroupSubGroupPair = new myapp.GroupSubGroupPair(); newGroupSubGroupPair.setGroup(parentGroup); newGroupSubGroupPair.setSubGroup(childGroup); myapp.activeDataWorkspace.ApplicationData.saveChanges(); } }, function (error) { alert(error); } ); };

    PS...I may have reversed parent & child...not sure I understood which was which.

    HTH,

    Josh


    • Edited by joshbooker Friday, October 2, 2015 5:29 PM PS
    • Proposed as answer by ADefwebserver Monday, October 5, 2015 9:30 PM
    • Marked as answer by mountainDev Tuesday, October 6, 2015 1:07 AM
    Friday, October 2, 2015 5:17 PM
  • Thank you Josh!  Your answer was exactly what I was looking for.  Really appreciate it!
    Tuesday, October 6, 2015 1:08 AM