featureLayer.applyEdits(adds, null, null) Access Attributes.

2785
3
Jump to solution
10-17-2012 05:27 AM
TylerWaring
Occasional Contributor II
Greetings,

I have created an editing application and am able to add, update and delete features from a featureLayer. Whenever I create a new feature, I need to also update a number of different arrays which a number of components in my application are dependent upon. How do I access the attributes of my new features?

I am finding that after I create a feature and refresh the featureLayer, I am not able to access it's attributes by looping over the features in my featureLayer and calling the graphic.attributes.FIELDNAME property as I usually do. Though my new graphic is recognized as I loop over the features in my featureLayer, the graphic.attributes.FIELDNAME property resolves to null. The only way that I can access the attributes of the new feature is to refresh my entire application.

How do I access the attributes of a newly created feature without refreshing the entire application?

Thanks, Tyler
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
IvanBespalov
Occasional Contributor III
By default you have access to added feature object id - it is unique, so you can find added feature using it:
var graphic:Graphic = new Graphic(); // add attributes to graphic var adds:Array = new Array(graphic); // or = [graphic] featureLayer.applyEdits(adds, null, null, false,                                new AsyncResponder(onEditResult, onEditFault));  ...  protected function onEditResult(results:FeatureEditResults, token:Object = null):void {     for each (var addResult:FeatureEditResult in results.addResults)     {         if (addResult.success === false)         {             trace("Feature adding error: " + addResult.error.message.toString());         }         else         {             trace("Added feature object Id = " + addResult.objectId);         }     }      for each (var updateResult:FeatureEditResult in results.updateResults)     {         ...     } }  protected function onEditFault(fault:Fault, token:Object = null):void  {     trace(fault.faultString); }


you can use token to store any information about current request:
Additional information to associate with this request.

var graphic:Graphic = new Graphic(); // add attributes to graphic var adds:Array = new Array(graphic); // or = [graphic]  var requestToken:Object = graphic.attributes;  featureLayer.applyEdits(adds, null, null, false,                                new AsyncResponder(onEditResult, onEditFault, requestToken));  ...  protected function onEditResult(results:FeatureEditResults, token:Object = null):void {     for each (var addResult:FeatureEditResult in results.addResults)     {         if (addResult.success === false)         {             trace("Feature adding error: " + addResult.error.message.toString());         }         else         {             if (token)             {                 var addedGraphicAttributes:Object = token;                 for (var attributeName:Object in addedGraphicAttributes)                 {                     var attributeValue:Object = feature[attributeName];                     trace(">>> " + attributeName + " : " + attributeValue);                 }             }             else             {                 trace("Added feature object Id = " + addResult.objectId);             }         }     }      for each (var updateResult:FeatureEditResult in results.updateResults)     {         ...     } }  protected function onEditFault(fault:Fault, token:Object = null):void  {     trace(fault.faultString); }


Reference: FeatureEditResults, FeatureEditResult

View solution in original post

0 Kudos
3 Replies
IvanBespalov
Occasional Contributor III
By default you have access to added feature object id - it is unique, so you can find added feature using it:
var graphic:Graphic = new Graphic(); // add attributes to graphic var adds:Array = new Array(graphic); // or = [graphic] featureLayer.applyEdits(adds, null, null, false,                                new AsyncResponder(onEditResult, onEditFault));  ...  protected function onEditResult(results:FeatureEditResults, token:Object = null):void {     for each (var addResult:FeatureEditResult in results.addResults)     {         if (addResult.success === false)         {             trace("Feature adding error: " + addResult.error.message.toString());         }         else         {             trace("Added feature object Id = " + addResult.objectId);         }     }      for each (var updateResult:FeatureEditResult in results.updateResults)     {         ...     } }  protected function onEditFault(fault:Fault, token:Object = null):void  {     trace(fault.faultString); }


you can use token to store any information about current request:
Additional information to associate with this request.

var graphic:Graphic = new Graphic(); // add attributes to graphic var adds:Array = new Array(graphic); // or = [graphic]  var requestToken:Object = graphic.attributes;  featureLayer.applyEdits(adds, null, null, false,                                new AsyncResponder(onEditResult, onEditFault, requestToken));  ...  protected function onEditResult(results:FeatureEditResults, token:Object = null):void {     for each (var addResult:FeatureEditResult in results.addResults)     {         if (addResult.success === false)         {             trace("Feature adding error: " + addResult.error.message.toString());         }         else         {             if (token)             {                 var addedGraphicAttributes:Object = token;                 for (var attributeName:Object in addedGraphicAttributes)                 {                     var attributeValue:Object = feature[attributeName];                     trace(">>> " + attributeName + " : " + attributeValue);                 }             }             else             {                 trace("Added feature object Id = " + addResult.objectId);             }         }     }      for each (var updateResult:FeatureEditResult in results.updateResults)     {         ...     } }  protected function onEditFault(fault:Fault, token:Object = null):void  {     trace(fault.faultString); }


Reference: FeatureEditResults, FeatureEditResult
0 Kudos
TylerWaring
Occasional Contributor II
Thanks for the informaiton Ivan. This is exactly what I needed to get the attributes at the time of creation. THis is very helpful.

I also need to be able to query my newly created records in a standalone table so that I can create a report. However, I am finding that when I create a new record I am unable to query that feature later on. If I restart the application, I can query the records and createn the report just fine. How do I refresh the data for the client without restarting my application? 

I am creating my table records like this:
const recordAttributes:Object = {
    fkey: selectedNeighborhood,
    DATE: new Date(myDateChooser.selectedDate.fullYear, myDateChooser.selectedDate.month, myDateChooser.selectedDate.date, 0, 0, 0, 0),
    DATE_STRING:new Date(myDateChooser.selectedDate.fullYear, myDateChooser.selectedDate.month, myDateChooser.selectedDate.date, 0, 0, 0, 0).toDateString(),
     ACTIVITY: strActivity,
     COMMENT: strComment
   };
   
   const record:Graphic = new Graphic(null, null, recordAttributes);
   activitiesTable.addEventListener(FeatureLayerEvent.EDITS_COMPLETE, activitiesTable_editsCompleteHandler);
   
private function activitiesTable_editsCompleteHandler(event:FeatureLayerEvent):void
   {
    
    activitiesTable.removeEventListener(FeatureLayerEvent.EDITS_COMPLETE, activitiesTable_editsCompleteHandler);
    activitiesTable.refresh();
    updateNeighborhoodActivities(selectedNeighborhood);
   }



Later, when I try to create the report, I run the following query to get the records to create a report:

private function testButtonClick(event:MouseEvent):void
   {
    activitiesTable.refresh();
    var query:Query = new Query();
    query.returnGeometry = false;
    query.outFields = activitiesTable.outFields;
    query.where = "FKEY = '" + strName + "'";
    var qTask:QueryTask = new QueryTask(activitiesTable.url);
    qTask.showBusyCursor = true;
    qTask.useAMF = false;
    qTask.execute(query, new AsyncResponder(testOnResult, testOnFault));
    function testOnResult(featureSet:FeatureSet, token:Object = null):void
    {
     for each (var graphic:Graphic in featureSet.features)
     {
      trace(graphic.attributes.FKEY);
      trace(graphic.attributes.OBJECTID);
     }
    }
    function testOnFault(info:Object, token:Object = null):void
    {
     Alert.show(info.toString(), "Text Query Problem");
    }
    
   }


However, when I click the test button, none of the records that I created during runtime are returned. If I reload the application and click the test button, all of the records are returned.

How do I update the data for the clientso that my report reflects all of the data? I need to report on data that was creatind during runtime as well as all of the data that has already been created.

Thanks, Tyler
0 Kudos
IvanBespalov
Occasional Contributor III
1 - mx : DateChooser
property   selectedDate
Date selected in the DateChooser control. If the incoming Date object has any   time values, they are zeroed out.

so:
var date1:Date = new Date(myDateChooser.selectedDate.fullYear, myDateChooser.selectedDate.month, myDateChooser.selectedDate.date, 0, 0, 0, 0);
var date2:Date = myDateChooser.selectedDate;
// as a result date1 equals date2


2 - There is a lot of same questions in this forum, where developers can't access just added features - some time they resolve setting property disableClientCaching=true to FeatureLayer, some time problem in other condition - "where clause" ...

The better way, IMHO, handle layer listeners:
// called layer.refresh(); -> listen it's update start and update end handlers -> trace to console update results
// called layer.applyEdits(); -> listen it's edit fault and edit result handlers -> trace to console
// called layer.queryFeatures(); -> listen it's query fault and query result handler -> trace to console
...
as a result you have full vison about you layer -
"What are you waiting calling layer.refresh()? VS What really happens when called layer.refresh()?"
...
Your browser network monitoring tools are also helpful - "F12" key in IE, "F12" key in Chrome, alot of addons for FF ... - as a result you can see all your requests to server and it's responses ...

Good luck ...
0 Kudos