Select to view content in your preferred language

applyEdits for updating data (JS4.4)

1907
12
Jump to solution
09-11-2017 07:13 AM
by Anonymous User
Not applicable

I am having a problem updating a feature layer data using applyEdits function.  I looked at the sample code in applyEdits API reference and Sandbox sample code, but I haven't been able to figure it out how to update the data.  I am only interested in updating existing data attributes.  I would very much appreciate your help.  

require([
  "esri/Map",
  "esri/views/SceneView",
  "esri/layers/FeatureLayer",
  "esri/geometry/Extent",
  'esri/geometry/ScreenPoint',
  'dojo/_base/lang',
  "dojo/on",
  "dojo/dom",
  "dojo/domReady!"
],
  function (
    Map, SceneView, FeatureLayer, Extent, ScreenPoint, lang, on, dom
  ) {
    var lyrUrl = 'https://services1.arcgis.com/aT1T0pU1ZdpuDk1t/arcgis/rest/services/CALO_survey_web/FeatureServer/0';

    var map = new Map({
      basemap: 'satellite'
    });

    var initialExtent = new Extent({
      xmin: -8520399.50094139,
      xmax: -8466523.75224205,
      ymin: 4109431.55147233,
      ymax: 4173688.01969221,
      spatialReference: 102100
    });

    var featureLyr = new FeatureLayer({
      url: lyrUrl
    });
    map.add(featureLyr);

    var sceneView = new SceneView({
      container: 'map',
      map: map,
      extent: initialExtent
    });

    var _onclick = function (evt) {
      var s = new ScreenPoint({
        x: evt.x,
        y: evt.y
      });

    // Updating feature layer data  
    sceneView.hitTest(s).then(picked => {
      console.log(picked);
      if (picked.results[0].graphic) {
        var editFeature = picked.results[0].graphic;
        editFeature.attributes.YearBuilt = '2000';
        console.log('after edit', editFeature);
        var edit = {
          updateFeatures: [editFeature]
        };
        var promise = featureLyr.applyEdits(edit);
        console.log(promise);
      }
    });
  }
  sceneView.on('click', _onclick);
Tags (1)
0 Kudos
1 Solution

Accepted Solutions
by Anonymous User
Not applicable

Hi Thomas,

Thank you so much for your help!!  I made it work.  The code "delete clonedFeature.geometry;"  didn't work so I created a new Graphic object and assigned "attributes".  It worked, but this is the way to update the data?  I think it would be very helpful if this information is added to API reference.  

var graphic = new Graphic();
graphic.attributes = editFeature.attributes;
var edit = {
   updateFeatures: [graphic]
};‍‍‍‍‍

Again, thank you so much!!

View solution in original post

0 Kudos
12 Replies
ThomasSolow
Frequent Contributor

Your code looks fine to me.  Try adding "outFields: ["*"]" to the FeatureLayer constructor:

var flayer = new FeatureLayer({
  url: <url>,
  outFields: ["*"]
});

The documentation for applyEdits mentions:

Values of non nullable fields must be provided when updating features.

Also, make sure YearBuilt expects a string.  It may expect a number.  I'm unsure how the service will handle a string when it expects a number, it may be able to coerce it, or it may just not work.

by Anonymous User
Not applicable

Hi Thomas,

Thank you so much for your reply.  I added outFields: ["*"] to my featureLyr object and checked my data has any non nullable fields and type of YearBuilt field.  I don't have any non nullable fields in my data (all are nullable: true) and YearBuilt type is 'string'.  It doesn't update the data.  I must be doing something wrong.  Would you be able to point out what else I can check?

0 Kudos
ThomasSolow
Frequent Contributor

I think the next thing to check would be the actual request that's being sent, which you can look at in dev tools.

Try opening up your browser's dev tools, switch to the networking tab, and click the feature to send the applyEdits request.  A new request should pop up in dev tools, take a look at that, and then take a look at the response when the server gets around to responding.  This should give you some hints about what's going wrong.

You can copy the request/response information here if you like and we can help you go over it.

0 Kudos
by Anonymous User
Not applicable

Hi Thomas,

I found an error

So I changed to "editFeature.attributes" then I got another error below.  Do I have to create a new Graphic object for the update?  If so, how?

// before
var edit = {
          updateFeatures: [editFeature]
        };

// after      
var edit = {
          updateFeatures: [editFeature.attributes]
        };

0 Kudos
ThomasSolow
Frequent Contributor

Hmm, interesting.  I would try this and see what happens:

var clonedFeature = editFeature.clone();
delete clonedFeature.geometry; 
var edit = {
  updateFeatures: [clonedFeature]
};‍‍‍‍‍‍‍‍‍‍

The first error suggests to me that you can't edit the geometry on the graphic in question.  Whether this is a limitation of applyEdits or a setting on your feature service, I'm not sure.  That sample will clone the graphic in question and delete the geometry property.

by Anonymous User
Not applicable

Hi Thomas,

Thank you so much for your help!!  I made it work.  The code "delete clonedFeature.geometry;"  didn't work so I created a new Graphic object and assigned "attributes".  It worked, but this is the way to update the data?  I think it would be very helpful if this information is added to API reference.  

var graphic = new Graphic();
graphic.attributes = editFeature.attributes;
var edit = {
   updateFeatures: [graphic]
};‍‍‍‍‍

Again, thank you so much!!

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Makiko,

   I think you are running into this because of your feature service setting not allowing geometry updates. If you change that option on your feature service you would not have this issue.

0 Kudos
KenBuja
MVP Esteemed Contributor

That would be a change from 3.x. I have a hosted feature service that only allows for attribute updates, but I am able to use applyEdits using the selected features from the feature layer without stripping the features of their geometry.

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Ken,


  I agree it is a change from 3.x. But not real surprising though.

0 Kudos