Select to view content in your preferred language

Esri Feature layer applyedits function in experience builder dev not actually changing any data

105
1
02-06-2025 08:41 AM
ahuey
by
New Contributor

I have this code here that when it runs it says that it is successfully updates all of the features. I am adding an attribute called 'active' and it is a string value.  But when I check the attributes in either my organizations portal or in arc pro, none of the changes appear. The Feature layer even shows that it has been updated in my content explorer. Any help with this would be very appreciated, thanks!

    al.forEach((layer: __esri.FeatureLayer) => {
      layer.queryFeatures({ where: '1=1', outFields: ['*'], returnGeometry: false }).then((result) => {
        console.log('Result', result.features)
        const feats = result.features

        feats.forEach((feature: __esri.Graphic) => {
          feature.attributes.active = 'active'
        })

        updateFeaturesActiveStatus(layer, feats)
      })
    })


const updateFeaturesActiveStatus = async (layer: __esri.FeatureLayer, features: __esri.Graphic[]) => {
    console.log("Feature updating", features);

    try {
      const editablLayer: __esri.FeatureLayer = getEditableFeatureLayer(layer)

      const res = await layer.applyEdits({ updateFeatures: features })
      console.log('Feature', features);


      if (res.updateFeatureResults.length > 0 && res.updateFeatureResults[0].error) {
        console.error("Update failed:", res.updateFeatureResults[0].error);
      } else {
        console.log("Feature updated successfully!", res.updateFeatureResults);

      }
    } catch (error) {
      console.error('Error updating feature active status:', error)
    }
  }

 

0 Kudos
1 Reply
TimWestern
MVP

So I don't know that I've tried updating a feature in a fashion like this, I've done add features, but some things you may check:

  1. Feature Layer Permissions:
    1. Check to ensure the feature layer is actually 'editable'
      1. Go to the ArcGIS Online or Portal item settings and confirm that "Enable Editing" is turned on. 
      2. I have run into issues trying to add, when trying to write to a layer, sometimes if the service is inside of a map, and not deployed as a separate service, I've not always seen it write correctly in some configurations to a feature service (reading works fine) 
      3. We've found that if you deploy a version that has editing outside the map and write there, if data comes from sam
    2. Check User permissions. (For the account running the script)
  2. Field name mismatch
    1. When writing, if the field names, types, and data do not match what the service requires it may error.
      1. To that end you may want to verify the scheme (make a call to the service and check the fields to see if all required fields are being passed in with the correct names)

 

console.log(layer.fields.map(f => f.name));

 

  • Confirm that "active" is an existing field in the feature layer schema. If the field does not exist or has a different casing (e.g., "Active" instead of "active"), the update may not work.
  • Properly Assigning the Feature Layer in applyEdits
    1. The function getEditableFeatureLayer(layer) should return the same instance as layer (or a layer with correct permissions). If getEditableFeatureLayer(layer) does something unexpected, it may prevent edits from being applied.
  • Check the applyEdits Response
    1. Specifically look at the .error and see if it has any additional information nested that might explain (for example,. it might tell you at least one field it is having issue with)


    2. I'm not sure what's in the Feature dat, but I think updates require the ObjectID of the object you intend to update (so that could be something to check also)

  • Possible race condition
    1. You might have methods you are calling which need to be awaited, and because they aren't it skips over it and doesn't actually finish (or doesn't share what it finished so you can reference it later on)



      Those are some things I'd check. I don't know if you've already checked all of those though.
0 Kudos