Update FeatureLayer using applyEdits()

This sample demonstrates how to use FeatureLayer.applyEdits() to create new features, update attributes of existing features, or delete existing features. This sample uses the FeatureTemplates widget to display templates from the editable feature layer. The widget listens for an end user to select a specific template in the widget. Its select event is fired and the resulting template information is returned. This is used in creating new features.

In addition to the FeatureTemplates widget, this sample also uses the FeatureForm widget to update attributes of new or existing features.

In this sample, the applyEdits function is called when a user either:

  • Clicks to create a new feature.
  • Selects a feature on the view and updates its attributes.
  • Selects a feature on the view and deletes it.
Use dark colors for code blocksCopy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// Call FeatureLayer.applyEdits() with specified params.
function applyEdits(params) {
  addFeatureBtnDiv.style.display = "none";
  addTemplatesDiv.style.display = "none";
  unselectFeature();

  featureLayer
    .applyEdits(params)
    .then((editsResult) => {
      // Get the objectId of the newly added feature.
      // Call selectFeature function to highlight the new feature.
      if (editsResult.addFeatureResults.length > 0) {
        const objectId = editsResult.addFeatureResults[0].objectId;
        selectFeature(objectId);
      }
    })
    .catch((error) => {
      console.log("error = ", error);
    });
}

FeatureLayer.applyEdits() resolves to an object containing edit results. If the edit failed, the edit result includes an error.

Your browser is no longer supported. Please upgrade your browser for the best experience. See our browser deprecation post for more details.