Javascript API: applyEdits - when does the globalID get created?

5074
16
Jump to solution
07-27-2016 11:53 AM
BatesRambow
New Contributor III

Javascript API Version 3.16

I have an issue with the applyEdits method on a Feature Layer when creating new features.  I'm trying to get the GlobalID that is automatically generated.  The REST response shows that the globalID did successfully get created but my attempts to get it via javascript keep resulting in 'undefined'.  I'm wondering if there's a timing issue with when the globalID actually gets created?

I've tried this with two approaches, neither successful:

First approach

    app.projectLayer.applyEdits([projectPoly], null, null).then(function(response) {

      app.map.graphics.remove(projectPoly);
      app.projectLayer.refresh();
    });

    app.projectLayer.on('edits-complete', function(result){

      var projectObjId = result.adds[0].objectId;

      var projects = app.projectLayer.graphics;

      for (var i = 0, len = projects.length; i < len; i++) {

        console.log(projects.attributes.GlobalID);

      }

Second approach

    app.projectLayer.applyEdits([projectPoly], null, null).then(function(response) {
      var projectObjId = response[0].objectId;
      var projects = app.projectLayer.graphics;
      for (var i = 0, len = projects.length; i < len; i++) {
        if(projects.attributes.OBJECTID == projectObjId){
          console.log(projects.attributes.GlobalID);
        }
      }
      app.map.graphics.remove(projectPoly);
      app.projectLayer.refresh();

    });

In both of these cases the console.log statement returns undefined.  However if i set it to console.log(projects[i-1].attributes.GlobalID);, I get the GlobalID of the next-to-last feature, as expected.  Why is it returning undefined for the newly created feature, especially when the REST response shows that the GlobalID was successfully created?

16 Replies
BatesRambow
New Contributor III

Possibly on the right track here.  I am not using ES6 syntax so can't use the "=>" operator, but this is what I have:

    function doAppEdits(projectPoly, callback){
      app.projectLayer.applyEdits([projectPoly], null, null).then(function(response){
        if(response[0].objectId){
          callback(response[0].objectId);
        }
        callback("NONE");
      });
    }

    var doneApplyEdits = doAppEdits(projectPoly, function(callback){
      var projectGlobalID = app.projectLayer.graphics.map(function(project){
        console.log(callback);
        if(project.attributes.OBJECTID == callback){
          return project.attributes.GlobalID;
        }
      });
      console.log("globalID is: ", projectGlobalID);
    });

Unfortunately, it's not quite there.  This is what I get in the console:

I appreciate your continued interest, but I may have to move on to something else and leave this issue open for another time.

0 Kudos
KellyHutchins
Esri Frequent Contributor

Here's an example showing how to get the attributes for the newly created feature. Is this what you are trying to accomplish?

JS Bin - Collaborative JavaScript Debugging

0 Kudos
BatesRambow
New Contributor III

Thanks, but I don't have a problem getting the attributes other than the GlobalID, and your example does not demonstrate this with GlobalID.  I can already get exactly what your code returns with my code above.

If I run your code with my data, this is the console output:

Still no GlobalID, even though the Feature Service has that field and all other existing features have GlobalIds.  Hence the question - when does the GlobalID get created?

Here's the response from the REST service for this same action:

{"addResults":[{"objectId":4025,"globalId":"{E4FB8847-BECB-44C9-9C24-FFF2976996E1}","success":true}],"updateResults":[],"deleteResults":[]}

The GlobalID exists, so why isn't it showing up with the other attributes? If you can show me an example with GlobalID, that would be great.

0 Kudos
KellyHutchins
Esri Frequent Contributor

I don't have a test service with globalId enabled to verify but does the globalId ever show up in your attributes when queried via the JSAPI. For example if we leave editing out of the picture and you just query your feature layer for all the features is the globalID attribute returned?

0 Kudos
BatesRambow
New Contributor III

Yes, it is.  If I return an array of all the existing features in the layer, outside of an editing operation, the returned attributes include GlobalID.  This is also demonstrated in one of my posts above, where I show that the object in the 'projects' array includes the global id, but when i drill down and return a single object, after editing, the GlobalID attribute does not exist.

Example, just returning the array of feature objects by returning app.projectLayer.graphics, outside of the edit operation.  Notice the inclusion of GlobalIDs

At this point, my conclusion is that I either don't fully grasp the async timing of the edit operation and I need to take a different approach to ensure that what I'm trying to do occurs long after the edits are made, or there is a bug in the API.

0 Kudos
BatesRambow
New Contributor III

Well, I figured my problem out.  Instead of doing things on the 'edits-complete' event, I used the 'updates-end' event and made sure to refresh the layer so the update event would trigger.  This gives me the access to the GlobalID I needed.  In the end it was a pretty easy fix, I just needed a better understanding of the events available on FeatureLayer.  Thanks to all for the help!

EvelynHernandez
Occasional Contributor III

We were close, haha.

Good

0 Kudos