Edit DataSource Records Programmatically

336
1
01-04-2024 03:36 PM
RyanCoodey
Occasional Contributor III

I am able to access and query a DataSource just fine from a custom widget. But I need to insert/update some records programmatically as well. When I try to use ds.updateRecord() I get an error "Editing source is not ready yet". Any ideas how to make it ready?

I confirmed the DataSource is coming from the Feature Service and the capabilities say "Query,Create,Update,Uploads,Editing"

        //Get update data source
        const ds = DataSourceManager.getInstance().getDataSource(props.useDataSources[1].dataSourceId) as FeatureLayerDataSource
        //Get record to update
        ds.query({
          where: "UserId='" + props.user.username + "'"
        }).then((result) => {
          if (result.records && result.records.length > 0)
          {
            //Get record data and change
            const record = result.records[0]
            const dat = record.getData()
            dat.TypeCode = "New Value"
            //Process update --ERROR HERE: Editing source is not ready yet
            ds.updateRecord(dat).then((success) => {
            })
          }
        })

Examples seem to show creating an editor widget. I am just wanting to call the service on the backend and do some edits though. In this case for a non-spatial table even. Worst case I suppose I could use the JS API or call the service endpoint directly, but assuming I am missing something with ExB objects.

Thanks for any help!

0 Kudos
1 Reply
YueyangLi
New Contributor II

Hi @RyanCoodey ,

The data source in ExB doesn't implement edit-related methods, including the `updateRecord`. You can do it using the applyEdits method of the ArcGIS Maps SDK for JavaScript.

const jsAPIFeatureLayer = await featureLayerDs.createJSAPILayerByDataSource()
const result = await jsAPIFeatureLayer.applyEdits(edits, options)

 

Hope it can help you.