Select to view content in your preferred language

Adding a row to a table using JS 4.x

1998
32
Jump to solution
11-20-2024 08:53 AM
Mr_Kirkwood
Regular Contributor

I was using JS 3.x to add and update data. I am working on convert my applications to JS 4.x.  I had a script that used "dojo/Deferred". Here is the code:

function _applyAdds (featureLayer, propertiesArr)
{
    var defer;
    require (['esri/graphic', "dojo/Deferred"], function (Graphic, Deferred)
    {
        defer = new Deferred ();
        var graphicsArr = [];
        propertiesArr.forEach (function (properties)
        {
            graphicsArr.push (new Graphic (null, null, properties));
        });
        featureLayer.applyEdits (
                graphicsArr, null, null,
                function (a, u, d)
                {
                    console.log (a, u, d);
                    defer.resolve ();
                }, // callback
                function (error)
                {
                    console.log ('ERROR!', error);
                }// errorback
        );
    });
    return defer.promise;
}

//begin adding new employee assignment
function assignEmployee ()
{
    var addEmployee = {};
    addEmployee.SPACEKEY = document.getElementById ('EmployeeSpaceID').value;
    addEmployee.EMPLOYEEKEY = document.getElementById ('EmployeeID').value;
    console.log (addEmployee);
    _applyAdds (roomAssinmentTable, [addEmployee]).then (function ()
    {
        console.log ("Add Employee");
    });
}

 

How do i make this work in JS 4.x? 

Thank you in advance for any help on this. 

0 Kudos
32 Replies
JoelBennett
MVP Regular Contributor

If you want to set multiple features to have the same attribute values, you could do so with a function like this:

 

function _applyUpdates (featureLayer, attributes, objectIds)
{
    return new Promise(function(resolve, reject) {
        var query = featureLayer.createQuery();
        query.returnGeometry = true;
        query.objectIds = objectIds;

        featureLayer.queryFeatures().then(function(featureSet) {
            if (attributes.hasOwnProperty(featureLayer.objectIdField))
                delete attributes[featureLayer.objectIdField];

            featureSet.features.forEach(function(feature) {
                Object.assign(feature.attributes, attributes);
            });

            featureLayer.applyEdits({updateFeatures:featureSet.features}).then(
                function (results)
                {
                    console.log (results);
                    resolve();
                }, // callback
                function (error)
                {
                    console.log ('ERROR!', error);
                    reject(error);
                }// errorback
            );
        });
    });
}

 

 

You would then determine the records you want to update, and create an Array of their objectID values:

 

var objectIds = [1,2,3,4,5];

 

 

Then you could call the function:

 

_applyUpdates(roomsTable, editRoom, objectIds);

 

0 Kudos
Mr_Kirkwood
Regular Contributor

Joel,

I am getting this error: 

Uncaught (in promise) ReferenceError: features is not defined

at this line

featureLayer.applyEdits({updateFeatures:features}).then(

0 Kudos
JoelBennett
MVP Regular Contributor

My bad, line 16 above should be:

 

featureLayer.applyEdits({updateFeatures:featureSet.features}).then(

 

 

I'll change it in that post too.

0 Kudos
Mr_Kirkwood
Regular Contributor

Thanks! However, i am getting a new error. I cxan edit a single record so not sure what is going on? 

baseQueries.js:120 ERROR! e {name: 'request:server', details: {…}, message: 'Unable to load https://uwoperations.uwyo.edu/hostg…EditLayers/FeatureServer/9/applyEdits status: 500'}

 

0 Kudos
JoelBennett
MVP Regular Contributor

Are you able to log into ArcGIS Server Manager for that instance?  If so, the logs may contain more information.

0 Kudos
RobertKirkwood
Frequent Contributor

The logs report this:

"Error executing tool. ReportCacheStatus : ERROR 999999: Something unexpected caused the tool to fail. Contact Esri Technical Support (http://esriurl.com/support) to Report a Bug, and refer to the error help for potential solutions or workarounds. ArcGIS Server requires a service type (ParentType) and a server object extension type (Type). Failed to execute (ReportCacheStatus)." - System/ReportingTools.GPServer

I will see if i can track down this error. Seems odd it works on a single record. Console reports it is happening at this line:

featureLayer.applyEdits({updateFeatures:featureSet.features}).then(

next line it fails is:

featureLayer.queryFeatures().then(function(featureSet) {

then:

return new Promise(function(resolve, reject) {

and lastly:

_applyUpdates(roomsTable, batchEditRoom, objectIds).then (function ()

 

 

 

 

0 Kudos
Mr_Kirkwood
Regular Contributor

The logs report this:

"Error executing tool. ReportCacheStatus : ERROR 999999: Something unexpected caused the tool to fail. Contact Esri Technical Support (http://esriurl.com/support) to Report a Bug, and refer to the error help for potential solutions or workarounds. ArcGIS Server requires a service type (ParentType) and a server object extension type (Type). Failed to execute (ReportCacheStatus)." - System/ReportingTools.GPServer

I will see if i can track down this error. Seems odd it works on a single record. Console reports it is happening at this line:

featureLayer.applyEdits({updateFeatures:featureSet.features}).then(

next line it fails is:

featureLayer.queryFeatures().then(function(featureSet) {

then:

return new Promise(function(resolve, reject) {

and lastly:

_applyUpdates(roomsTable, batchEditRoom, objectIds).then (function ()

 

0 Kudos
JoelBennett
MVP Regular Contributor

A few things to check:

1) Make sure this data is going to the right layer in the service.

2) Make sure all the fields are named correctly and exist in the service.  Field names are case sensitive.

3) The OBJECTID field shouldn't be included in the attributes you capture from the form.  Lines 9 and 10 in the code I wrote are supposed to strip it out if it's present.  I suppose the server could generate an error if multiple update records were supplied with the same objectID values.  For reference, since it's on a different page now, here's the code where you capture the values from the form:

 

    editRoom.OBJECTID = Number (roomObjectID)
    editRoom.ADA = document.getElementById ('roomDetails-ADA').value;
    editRoom.CAPACITY = document.getElementById ('roomDetails-CAPACITY').value;
    editRoom.SPACETYPECAT1KEY = document.getElementById ('roomDetails-SPACETYPECAT1KEY').value;
    editRoom.SPACETYPECAT2KEY = document.getElementById ('roomDetails-SPACETYPECAT2KEY').value;
    editRoom.SPACEUSECAT1KEY = document.getElementById ('roomDetails-SPACEUSECAT1KEY').value;
    editRoom.SPACEUSECAT2KEY = document.getElementById ('roomDetails-SPACEUSECAT2KEY').value;
    editRoom.DESCRIPTION = document.getElementById ('roomDetails-DESCRIPTION').value;
    editRoom.CAPACITY = document.getElementById ('roomDetails-CAPACITY').value;
    editRoom.ASSIGNABLE = document.getElementById ('roomDetails-ASSIGNABLE').value;
    editRoom.NOTES = document.getElementById ('roomDetails-NOTES').value;
    editRoom.USABLE = document.getElementById ('roomDetails-USABLE').value;

 

 

0 Kudos
Mr_Kirkwood
Regular Contributor

Joel,

let me see if this is what you mean here is the Batch Edit function that i use to pass to the _applyUpdates:

 

 

function batchEditRoomData ()
{
    var objectIds = roomEditData;
    var batchEditRoom = {}; 
    batchEditRoom.NOTES = document.getElementById ('editroomDetails-NOTES').value;
     _applyUpdates(roomsTable, batchEditRoom, objectIds).then (function ()
    {
        console.log ("Batch Edit Room");
    });
    
}

 

 

I grab the OBJECTIDs from the table of  the selected records . I still seem to be getting an error . I am only using the NOTES field for this test, by the way.

editBatch_A.PNG

 

editBatch_B.PNG

 

0 Kudos
JoelBennett
MVP Regular Contributor

That looks fine as far as I can tell.  You might want to check out the Network Tab of the developer tools and look into the details of the request sent to applyEdits.  Particularly, check out the payload to make sure everything looks proper in there.

0 Kudos