Select to view content in your preferred language

Create New Feature, Populate a field based of ObjectID value

2723
9
Jump to solution
06-05-2013 03:04 PM
NathalieNeagle
Regular Contributor
I have an applicaiton JS and when a users creates a new feature, I want to automatically populate a field called "FeatureID" with the value that was assigned in the ObjectID field (by ArcGIS).  I'm struggling with how to do this and I'm wondering if someone could give me a push or insight. 

I would love to do it on the back-end in the database schema (like how you do it for a domain and you have a default domain in your GDB but I don't think this is possilble so I'm looking to do it in code).

Thanks
Nathalie
0 Kudos
1 Solution

Accepted Solutions
BenFousek
Deactivated User
I've never used the editor widget until now. The api reference doesn't list any events for the editor widget and looking through the editor in the DOM didn't help much either.

The feature layer has an 'onEditsComplete' event. Connect to this event when you create the feature layer:

var editSignSupports = new esri.layers.FeatureLayer("https://myserver/pubgis/rest/services/Signs/Signs2/FeatureServer/1", {  id: 'editSignSupports', //always assign the layer id to all layers of all types; never let the api do it  mode: esri.layers.FeatureLayer.MODE_ONDEMAND,  outFields: ["*"]      }); dojo.connect(editSignSupports, 'onEditsComplete', function(result) {  var add = result[0];  if (add !== undefined && add.success) { //check to see that add was successful and in fact this was an add and not an update or delete   dojo.forEach(editSignSupports.graphics, function(graphic) { //iterate through feature layer graphics    if (graphic.attributes.OBJECTID === add.objectId) {          graphic.attributes.SOME_OTHER_FIELD = add.objectId; //set your other field with object id      editSignSupports.applyEdits(null, [graphic], null, function (response) { //apply edits to object      //console.log(response);     }, function (error) {      //console.log(error)     });     return; //stop loop once the only feature with that object id has been updated    }   })     } });


Attribute/field names are case sensitive. If your feature class has an OID called "objectid", referring to "OBJECTID" won't work.

Hope this helps. I got it to work with this example http://developers.arcgis.com/en/javascript/samples/ed_simpletoolbar/. Probably needs some clean up and thorough testing prior to deployment.

View solution in original post

0 Kudos
9 Replies
BenFousek
Deactivated User
The callback on applyEdits returns the objectId of the new feature.

In the add feature callback function update the attribute and send it back for an update.

//create "feature"
app.editLayer.applyEdits([feature], null, null, function (response) {
 console.log('feature added');
 
 feature.attributes.SOME_OTHER_FIELD = response.addResults[0].objectId;
 
 app.editLayer.applyEdits(null, [feature], null, function (response) {
 
         console.log('feature updated');
 
 }, function (error) {
  console.log(error)
 })
 
}, function (error) {
 console.log(error)
});
0 Kudos
NathalieNeagle
Regular Contributor
Ben,

Thanks for the reply,

Sorry still green, where do I place the call back?


HTML
[HTML]<div id='editTab' data-dojo-type="dijit.layout.ContentPane" data-dojo-props="title:'EDITOR'">
           <div id='editorDiv'></div>
          </div>[/HTML]

my layout.js code
        
         var editSignSupports = new esri.layers.FeatureLayer("https://myserver/pubgis/rest/services/Signs/Signs2/FeatureServer/1", {
          mode: esri.layers.FeatureLayer.MODE_ONDEMAND,
          outFields: ["*"]     
        });
        
        mapLayers.push(editSignSupports);
        legendLayers.push({layer:editSignSupports,title:'SignSupport'});



The code is fairly light
0 Kudos
BenFousek
Deactivated User
Are you using the editor widget?
0 Kudos
NathalieNeagle
Regular Contributor
Yes I am using the editor widget
0 Kudos
NathalieNeagle
Regular Contributor
Here's my editor code I left out.

      function initEditing(results) {
        var featureLayerInfos = dojo.map(results, function(result) {
          return {
            'featureLayer': result.layer
          };
        });

        editorSettings = {
          map: map,
          layerInfos: featureLayerInfos
        };
        
        //when the editor tab is clicked we'll initialize the editor
        var showHandler = dojo.connect(dijit.byId('editTab'),'onShow',function(evt){
            //disconnect the handler so the editor is only initialized once
            dojo.disconnect(showHandler);
            var params = {
              settings: editorSettings
            };
            
            
            var editorWidget = new esri.dijit.editing.Editor(params, 'editorDiv');

            
            editorWidget.startup();        
            
        });
      }




Thanks for your time.
Nathalie
0 Kudos
BenFousek
Deactivated User
I've never used the editor widget until now. The api reference doesn't list any events for the editor widget and looking through the editor in the DOM didn't help much either.

The feature layer has an 'onEditsComplete' event. Connect to this event when you create the feature layer:

var editSignSupports = new esri.layers.FeatureLayer("https://myserver/pubgis/rest/services/Signs/Signs2/FeatureServer/1", {  id: 'editSignSupports', //always assign the layer id to all layers of all types; never let the api do it  mode: esri.layers.FeatureLayer.MODE_ONDEMAND,  outFields: ["*"]      }); dojo.connect(editSignSupports, 'onEditsComplete', function(result) {  var add = result[0];  if (add !== undefined && add.success) { //check to see that add was successful and in fact this was an add and not an update or delete   dojo.forEach(editSignSupports.graphics, function(graphic) { //iterate through feature layer graphics    if (graphic.attributes.OBJECTID === add.objectId) {          graphic.attributes.SOME_OTHER_FIELD = add.objectId; //set your other field with object id      editSignSupports.applyEdits(null, [graphic], null, function (response) { //apply edits to object      //console.log(response);     }, function (error) {      //console.log(error)     });     return; //stop loop once the only feature with that object id has been updated    }   })     } });


Attribute/field names are case sensitive. If your feature class has an OID called "objectid", referring to "OBJECTID" won't work.

Hope this helps. I got it to work with this example http://developers.arcgis.com/en/javascript/samples/ed_simpletoolbar/. Probably needs some clean up and thorough testing prior to deployment.
0 Kudos
NathalieNeagle
Regular Contributor
Ben,

Thanks that seems like it will work.  I will give it a good testing before I deploy and see if I can clean anything.

Nathalie.
0 Kudos
deleted-user-3VgAsJKWtJe2
Deactivated User
The callback on applyEdits returns the objectId of the new feature.

In the add feature callback function update the attribute and send it back for an update.

//create "feature"
app.editLayer.applyEdits([feature], null, null, function (response) {
 console.log('feature added');
 
 feature.attributes.SOME_OTHER_FIELD = response.addResults[0].objectId;
 
 app.editLayer.applyEdits(null, [feature], null, function (response) {
 
         console.log('feature updated');
 
 }, function (error) {
  console.log(error)
 })
 
}, function (error) {
 console.log(error)
});


Thanks! This helped me out a lot with a project I am working on!
BenFousek
Deactivated User
Thanks! This helped me out a lot with a project I am working on!


You're welcome to the forum.
0 Kudos