<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Creating new records in a table in ArcGIS JavaScript Maps SDK Questions</title>
    <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/creating-new-records-in-a-table/m-p/29372#M2476</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;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.&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I have created my own editor tool consisting of the template picker and a custom attribute editor/field validator.&amp;nbsp; It also checks if the featurelayer supports attachments and related records.&amp;nbsp; If the selected feature supports related records it will allow the user to view/add/edit/delete records on the tables.&amp;nbsp; Everything is working except for the add record part.&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I added the table as a featurelayer which supports editing and deleting of the tables records.&amp;nbsp; 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.&amp;nbsp; The next step is to display the attributes to allow the user to enter more information for each field.&amp;nbsp; An error ("Internal error during object insert.") is thrown which is because it does not autoincrement the ObjectID.&amp;nbsp; So I used the StatisticDefinition to query the max ObjectID, add 1, and added it to the attribute table with the related ID.&amp;nbsp; It still throws the error.&amp;nbsp; The remaining fields are set to null.&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;My question is does the related tables support adding new records?&amp;nbsp; 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?&amp;nbsp; I am using version 3.6 and we are using ArcGIS Server 10.1.&amp;nbsp; Below is the code that fires after the user clicks a button to add a new record to the table.&amp;nbsp; Thanks.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;
$("#addRelBtn").click(function() {
 var query = new Query();
// Table is a featurelayer and gra is the selected graphic
 console.log(table);
 console.log(gra);
 
//&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Get graphic attributes, create new record, query record and pass to buildAttr.
 var arr = {};
 var field;

//&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Matches table and layer IDs
 $.each(table.relationships, function(i) {
&amp;nbsp; if (table.relationships&lt;I&gt;.relatedTableId == gra._graphicsLayer.layerId) {
&amp;nbsp;&amp;nbsp; field = table.relationships&lt;I&gt;.keyField;
&amp;nbsp; }
 })&amp;nbsp; 

//&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 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+" &amp;gt;= 0";
 query.returnGeometry = false;
 query.outFields = [ table.objectIdField ];
 query.outStatistics = [ statDef ];
 queryTask.execute(query, function(result) {&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp; 
//&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Loop each field to find the related field and objectid field
&amp;nbsp; $.each(table.fields, function(i) {
&amp;nbsp;&amp;nbsp; console.log(table.fields&lt;I&gt;);
&amp;nbsp;&amp;nbsp; if (table.fields&lt;I&gt;.name === field) {
&amp;nbsp;&amp;nbsp;&amp;nbsp; var attr = "gra.attributes."+oldResp.relationships[0].keyField;
&amp;nbsp;&amp;nbsp;&amp;nbsp; arr[table.fields&lt;I&gt;.name] = eval(attr);;
&amp;nbsp;&amp;nbsp; } else if (table.fields&lt;I&gt;.name == table.objectIdField) {
&amp;nbsp;&amp;nbsp;&amp;nbsp; arr[table.fields&lt;I&gt;.name] = result.features[0].attributes.maxID + 1;
&amp;nbsp;&amp;nbsp; } else {
&amp;nbsp;&amp;nbsp;&amp;nbsp; arr[table.fields&lt;I&gt;.name] = null;
&amp;nbsp;&amp;nbsp; }
&amp;nbsp; })
&amp;nbsp; console.log(arr);

&amp;nbsp; var ng = new esri.Graphic(null, null, arr);
&amp;nbsp; console.log(ng);
&amp;nbsp; 
&amp;nbsp; table.on("edits-complete", function(evt) {
&amp;nbsp;&amp;nbsp; console.log(evt);
&amp;nbsp;&amp;nbsp; if (evt.updates[0].success === true) {
&amp;nbsp;&amp;nbsp;&amp;nbsp; alert('New record added.');
//&amp;nbsp;&amp;nbsp;&amp;nbsp; buildAttr(table, result.features[str], table, "rel", null);
&amp;nbsp;&amp;nbsp; }
&amp;nbsp; })
&amp;nbsp; table.applyEdits([arr], null, null)
 })
})
&lt;/I&gt;&lt;/I&gt;&lt;/I&gt;&lt;/I&gt;&lt;/I&gt;&lt;/I&gt;&lt;/I&gt;&lt;/I&gt;&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Mon, 19 May 2014 16:09:32 GMT</pubDate>
    <dc:creator>BernardConrad</dc:creator>
    <dc:date>2014-05-19T16:09:32Z</dc:date>
    <item>
      <title>Creating new records in a table</title>
      <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/creating-new-records-in-a-table/m-p/29372#M2476</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;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.&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I have created my own editor tool consisting of the template picker and a custom attribute editor/field validator.&amp;nbsp; It also checks if the featurelayer supports attachments and related records.&amp;nbsp; If the selected feature supports related records it will allow the user to view/add/edit/delete records on the tables.&amp;nbsp; Everything is working except for the add record part.&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I added the table as a featurelayer which supports editing and deleting of the tables records.&amp;nbsp; 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.&amp;nbsp; The next step is to display the attributes to allow the user to enter more information for each field.&amp;nbsp; An error ("Internal error during object insert.") is thrown which is because it does not autoincrement the ObjectID.&amp;nbsp; So I used the StatisticDefinition to query the max ObjectID, add 1, and added it to the attribute table with the related ID.&amp;nbsp; It still throws the error.&amp;nbsp; The remaining fields are set to null.&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;My question is does the related tables support adding new records?&amp;nbsp; 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?&amp;nbsp; I am using version 3.6 and we are using ArcGIS Server 10.1.&amp;nbsp; Below is the code that fires after the user clicks a button to add a new record to the table.&amp;nbsp; Thanks.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;
$("#addRelBtn").click(function() {
 var query = new Query();
// Table is a featurelayer and gra is the selected graphic
 console.log(table);
 console.log(gra);
 
//&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Get graphic attributes, create new record, query record and pass to buildAttr.
 var arr = {};
 var field;

//&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Matches table and layer IDs
 $.each(table.relationships, function(i) {
&amp;nbsp; if (table.relationships&lt;I&gt;.relatedTableId == gra._graphicsLayer.layerId) {
&amp;nbsp;&amp;nbsp; field = table.relationships&lt;I&gt;.keyField;
&amp;nbsp; }
 })&amp;nbsp; 

//&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 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+" &amp;gt;= 0";
 query.returnGeometry = false;
 query.outFields = [ table.objectIdField ];
 query.outStatistics = [ statDef ];
 queryTask.execute(query, function(result) {&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp; 
//&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Loop each field to find the related field and objectid field
&amp;nbsp; $.each(table.fields, function(i) {
&amp;nbsp;&amp;nbsp; console.log(table.fields&lt;I&gt;);
&amp;nbsp;&amp;nbsp; if (table.fields&lt;I&gt;.name === field) {
&amp;nbsp;&amp;nbsp;&amp;nbsp; var attr = "gra.attributes."+oldResp.relationships[0].keyField;
&amp;nbsp;&amp;nbsp;&amp;nbsp; arr[table.fields&lt;I&gt;.name] = eval(attr);;
&amp;nbsp;&amp;nbsp; } else if (table.fields&lt;I&gt;.name == table.objectIdField) {
&amp;nbsp;&amp;nbsp;&amp;nbsp; arr[table.fields&lt;I&gt;.name] = result.features[0].attributes.maxID + 1;
&amp;nbsp;&amp;nbsp; } else {
&amp;nbsp;&amp;nbsp;&amp;nbsp; arr[table.fields&lt;I&gt;.name] = null;
&amp;nbsp;&amp;nbsp; }
&amp;nbsp; })
&amp;nbsp; console.log(arr);

&amp;nbsp; var ng = new esri.Graphic(null, null, arr);
&amp;nbsp; console.log(ng);
&amp;nbsp; 
&amp;nbsp; table.on("edits-complete", function(evt) {
&amp;nbsp;&amp;nbsp; console.log(evt);
&amp;nbsp;&amp;nbsp; if (evt.updates[0].success === true) {
&amp;nbsp;&amp;nbsp;&amp;nbsp; alert('New record added.');
//&amp;nbsp;&amp;nbsp;&amp;nbsp; buildAttr(table, result.features[str], table, "rel", null);
&amp;nbsp;&amp;nbsp; }
&amp;nbsp; })
&amp;nbsp; table.applyEdits([arr], null, null)
 })
})
&lt;/I&gt;&lt;/I&gt;&lt;/I&gt;&lt;/I&gt;&lt;/I&gt;&lt;/I&gt;&lt;/I&gt;&lt;/I&gt;&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 19 May 2014 16:09:32 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/creating-new-records-in-a-table/m-p/29372#M2476</guid>
      <dc:creator>BernardConrad</dc:creator>
      <dc:date>2014-05-19T16:09:32Z</dc:date>
    </item>
  </channel>
</rss>

