Update a REST Service table from a Widget

178
4
Jump to solution
04-16-2020 01:37 PM
JustinBridwell2
Occasional Contributor II

Suppose I have a WAB widget  (version 1.2) called TaskManager. Everytime a user clicks a button (triggering a click event) I want to route it to a function that will update a specific field called 'lastupdated' in a REST Service (FEATURE Service) table called SuprojectTypeTable, which looks like this in my widget config,json:

"subprojectTaskTrackingURL": "https://www.mywabapp.com/server/rest/services/.../FeatureServer",

"subprojectTypeTable": {
  "index": "5",
  "oid": "objectid",
  "subprojectName": "subproject",
  "projectType": "projecttype",
  "lastUpdated": "lastupdated"
},

This function will take one parameter, a subprojectName, so that it can update the specific row in the table that I want. It will also call a simple function that will get, format, and return the current date. This will be the value I want to plug into the 'lastupdated' field:

currentDate: function(){
var d =newDate();
var month = d.getMonth()+1;
var day = d.getDate();
var currentD =((''+ month).length <2?'0':'')+ month +
'/'+((''+ day).length <2?'0':'')+ day +'/'+ d.getFullYear();
return currentD },

The REST Service can use apply edits and update features. 

updateSubprojObj: function (s_proj) {

    var new_date = thisWidget.currentDate();

    var subproj = dijit.byId("subprojectSelect");

    this.subprojectTypeTable.applyEdits(subproj, null, null, null, new_date, function (editResult) {
        console.log(editResult);
    }, function (editError) {
        console.log(editError);
        throw "Issue updating record in database."
    }).then(function () {
        console.log('edit applied, updated item returned to grid')
    });

},

The above does not work of course. What would be the best way to do this?

1 Solution

Accepted Solutions
JustinBridwell2
Occasional Contributor II

Robert - Argghhh, I overlooked a really important detail. objs.lastupdated has to have an updated date to "update". It also had to be in epoch date format. I added the below with `objs.lastupdated = Date.now();` and it works fine now. Thanks for the assist:

...

if (typeof proj_obj !== undefined) {
    for (var j = 0; j < proj_obj.length; j++) {
        if (s_proj == proj_type_obj.subproject) {
            var objs = proj_type_obj
            objs.lastupdated = Date.now();
            p_obj = objs;
        }
    }
}

....

View solution in original post

0 Kudos
4 Replies
RobertScheitlin__GISP
MVP Esteemed Contributor

Justin,

  You need to look at what the applyEdits method is expecting (not what you are trying right now).

FeatureLayer | API Reference | ArcGIS API for JavaScript 3.32 

Manin thing it is expecting an object with an updates property containing an array of Graphics (not attributes, an actual graphic class with no geometry).

JustinBridwell2
Occasional Contributor II

Hey Robert - I think I almost have this working. Its not throwing an error but it's not updating in the REST Service table. Here's the function I made that gets triggered in the updateItem (item) function when I call 

thisWidget.updateSubprojObj(proj_type_obj, current_subproject);

The parameter `proj_type_obj` is an literally an array object containing the ObjectID (oid), subprojectName, subptojectType, and lastUpdated values for all items in the SubprojectType Table. subproject is just a dijit.byId("subprojectSelect") value that corresponds to the current subproject that I want to update:

updateSubprojObj: function (proj_obj, s_proj) {
    var p_obj = null;
    this.subprojectTypeTable = new FeatureLayer(subprojectTypeURL, {
        outFields: ["*"]
    });

    if (typeof proj_obj !== undefined) {
        for (var j = 0; j < proj_obj.length; j++) {
            if (s_proj == proj_type_obj.subproject) {
                var objs = proj_type_obj
                p_obj = objs;
            }
        }
    }
    let updatedItem = Object.assign({}, p_obj);
    var updatedFeatureJson = updatedItem;
    var updatedFeature = new Graphic(null, null, updatedFeatureJson, null);
    this.subprojectTypeTable.applyEdits(null, [updatedFeature], null, function (editResult) {
        console.log(editResult);
    }, function (editError) {
        console.log(editError);
        throw "Issue updating record in database."
    }).then(function () {
        console.log('edit applied, updated item returned to grid')
    });
},

Note: I didn't end up using the currentDate function to convert the date because it ended up requiring the epoch data format. Do you see anything wrong with this? The graphic array, updatedFeature looks like this when its getting plugged in:

0 Kudos
RobertScheitlin__GISP
MVP Esteemed Contributor

Justin,

   Are there any error in your ArcGIS Server logs? Are you providing all required fields for this feature in the update object?

0 Kudos
JustinBridwell2
Occasional Contributor II

Robert - Argghhh, I overlooked a really important detail. objs.lastupdated has to have an updated date to "update". It also had to be in epoch date format. I added the below with `objs.lastupdated = Date.now();` and it works fine now. Thanks for the assist:

...

if (typeof proj_obj !== undefined) {
    for (var j = 0; j < proj_obj.length; j++) {
        if (s_proj == proj_type_obj.subproject) {
            var objs = proj_type_obj
            objs.lastupdated = Date.now();
            p_obj = objs;
        }
    }
}

....

View solution in original post

0 Kudos