Select to view content in your preferred language

Creating new records in a table

1411
0
05-19-2014 09:09 AM
BernardConrad
New Contributor
Hello, I am hoping someone out there has some experience with creating new records on tables because I have not seen any posts or examples of doing this. 

I have created my own editor tool consisting of the template picker and a custom attribute editor/field validator.  It also checks if the featurelayer supports attachments and related records.  If the selected feature supports related records it will allow the user to view/add/edit/delete records on the tables.  Everything is working except for the add record part. 

I added the table as a featurelayer which supports editing and deleting of the tables records.  When the user clicks a button to add a record, it queries the selected graphic and copies the related ID which is a GUID (Not the ObjectID field) and places it in the attribute object before edits are applied to the table.  The next step is to display the attributes to allow the user to enter more information for each field.  An error ("Internal error during object insert.") is thrown which is because it does not autoincrement the ObjectID.  So I used the StatisticDefinition to query the max ObjectID, add 1, and added it to the attribute table with the related ID.  It still throws the error.  The remaining fields are set to null. 

My question is does the related tables support adding new records?  If the server does not create an objectID for me and I cannot supply an objectID because that field is not editable, how do we create a new record on the client's side?  I am using version 3.6 and we are using ArcGIS Server 10.1.  Below is the code that fires after the user clicks a button to add a new record to the table.  Thanks.

$("#addRelBtn").click(function() {
 var query = new Query();
// Table is a featurelayer and gra is the selected graphic
 console.log(table);
 console.log(gra);
 
//      Get graphic attributes, create new record, query record and pass to buildAttr.
 var arr = {};
 var field;

//      Matches table and layer IDs
 $.each(table.relationships, function(i) {
  if (table.relationships.relatedTableId == gra._graphicsLayer.layerId) {
   field = table.relationships.keyField;
  }
 })  

//      Query the table to return incremented ID
 var statDef = new StatisticDefinition();
 statDef.statisticType = "max";
 statDef.onStatisticField = '"'+table.objectIdField+'"';
 statDef.outStatisticFieldName = "maxID";
 
 query.where = table.objectIdField+" >= 0";
 query.returnGeometry = false;
 query.outFields = [ table.objectIdField ];
 query.outStatistics = [ statDef ];
 queryTask.execute(query, function(result) {        
  
//              Loop each field to find the related field and objectid field
  $.each(table.fields, function(i) {
   console.log(table.fields);
   if (table.fields.name === field) {
    var attr = "gra.attributes."+oldResp.relationships[0].keyField;
    arr[table.fields.name] = eval(attr);;
   } else if (table.fields.name == table.objectIdField) {
    arr[table.fields.name] = result.features[0].attributes.maxID + 1;
   } else {
    arr[table.fields.name] = null;
   }
  })
  console.log(arr);

  var ng = new esri.Graphic(null, null, arr);
  console.log(ng);
  
  table.on("edits-complete", function(evt) {
   console.log(evt);
   if (evt.updates[0].success === true) {
    alert('New record added.');
//    buildAttr(table, result.features[str], table, "rel", null);
   }
  })
  table.applyEdits([arr], null, null)
 })
})
0 Kudos
0 Replies