[GIS] Delete Records from Feature Layer using ArcGIS API for JavaScript

arcgis-javascript-apiarcgis-server

I am trying to delete some records from a feature layer, but I kept getting the error of invalid parameters from the errback.

Below is my code:

function deleteRecords()
{
var incidentId = 2;
var queryTaskDeleteRecords= new esri.tasks.QueryTask(FEATURE_SERVER+"/"+LAYER_V_RECORDS);
var queryDeleteRecords = new esri.tasks.Query();
queryDeleteRecords.outSpatialReference = map.spatialReference; 
queryDeleteRecords.returnGeometry = true;
queryDeleteRecords.outFields = ["*"];
queryDeleteRecords.where = FIELD_V_RECORDS_INCIDENTID + " = " + incidentId ; 
queryTaskDeleteRecords.execute(queryValveRec,deleteRecordsResults); 

}

function deleteRecordsResults(results)
{

    var featureLayer= new esri.layers.FeatureLayer( FEATURE_SERVER+"/"+LAYER_V_RECORDS,{
        outFields:["*"]
    });

    for (var i = 0; i < results.features.length; i++)
    {

    featureLayer.applyEdits(null, null, [results.features[i]], function onComplete(adds, updates, deletes)
    {
         if (deletes.length > 0)
         {
            alert("deletes : " + JSON.stringify(deletes));
         }
    },
    function errCallback(err)
    {
        alert(err);
    });

    }

}

Is there sometime wrong with the querying of features and passing the features in one by one to applyEdits for deleting?

Best Answer

After searching through the forums, there was a queestion on deleting features from a feature layer and thus I came up with this

var id = 2;
var query = new esri.tasks.Query();
query.where = FIELD_ID + " = " + id  ; //the valve ops rec layer thus using the val ops rec incident id

var featureLayer = new esri.layers.FeatureLayer(FEATURE_SERVER +"/"+LAYER_V_REC,
{
    outFields:["*"]
});

featureLayer.selectFeatures(queryValveRec, esri.layers.FeatureLayer.SELECTION_NEW, 
function (features)
{
    for (var i = 0; i < features.length; i++)
    {
        featureLayer.applyEdits(null, null, [features[i]], function (adds, updates, deletes)
        {
            alert("adds : " + JSON.stringify(adds));
            alert("updates : " + JSON.stringify(updates));
            alert("deletes : " + JSON.stringify(deletes));
        }, 
        function (err)
        {
            alert(err);
        });
    }
});

and this method works rather than getting features then trying to send to applyEdits. I hope this helps someone cause it took me 2 days to find this insufferable solution.