editing related records in a 1:M (one to many) relationship

6449
13
03-12-2012 02:22 PM
SebastianRoberts
Occasional Contributor III
Hello,
  I've seen the sample for Editing Related Records, but this sample just lets you change one value in the related table of a one to one relationship.  I would like to a user to be able to click on an existing point feature, be able to change attributes for that point feature, as well as have access to one or more related records.  The related features could be displayed in the attribute inspector, or in a side panel as a list.  In any case a user would need to be able to click on one of the existing related records to edit it, delete a related record, and add a new related record.  Both the feature class and related table are in SDE with an established relationship class, and are part of the mapservice I am referencing.
  Has anyone done something similar or have any ideas on how to get started?
Thanks,
Sebastian Roberts
13 Replies
SebastianRoberts
Occasional Contributor III
Well I am part way there.  I am able to query the related records and show them in a list.  Clicking on one of the items in the list should launch the attribute inspector such that I can edit the corresponding row in the related table.  I'd like to use the attribute inspector because I believe it will take care of validation and allow me to track and save edits.  As I understand it, the attribute inspector operates on selected features for a feature layer.  Therefore I added the related table as a feature layer and perform a selectFeatures on it.  When the selectFeatures callback is called, I can see in the debugger that my featureLayer has the correct feature (table row) selected.  However, the attribute inspector (which references the same featureLayer) has an empty array for its _selection attribute, and displays "no features selected".  The API reference says that you can use a table to create a featurelayer, but could there be an issue with using a feature layer for a standalone table and doing a selection on it?  Or perhaps can the attribute inspector not operate on a featureLayer that is a standalone table?  Below is my code, and attached are the debugger screen shots.

//the feature layer created in the init function
propMaintLayer = new esri.layers.FeatureLayer("https://myserver.com/ArcGIS/rest/services/internal/Sanitation_Inpsection/FeatureServer/1", { mode: esri.layers.FeatureLayer.MODE_SELECTION, outFields: ['OBJECTID','PointID','Date','MaintType']});



//and the function called when a related record is clicked
function selectRelated(objID){
   var flQuery = new esri.tasks.Query();
   flQuery.where = "OBJECTID = " + objID + "";
   flQuery.returnGeometry = "false";
   propMaintLayer.selectFeatures(flQuery,esri.layers.FeatureLayer.SELECTION_NEW,function (selection)
    {
     var layerInfos = [{'featureLayer':propMaintLayer,
        'showAttachments':false,
        'isEditable': true,
        'fieldInfos': [
        {'fieldName': 'OBJECTID', 'isEditable':false, 'tooltip': 'Object ID', 'label':'Object ID:'},
        {'fieldName': 'Date', 'isEditable':true,'tooltip': 'Inspection Date MM/DD/YYYY','label':'Date:'},
        {'fieldName': 'MaintType', 'isEditable':true, 'label':'Maintenance Type:'},
        {'fieldName': 'PointID', 'isEditable':false, 'tooltip': 'Foreign key', 'label':'Foreign Key:'},
        ]}];
     attInspector = new esri.dijit.AttributeInspector({
       layerInfos:layerInfos},
       dojo.create("div",{id:"relateDiv"},"liRelate" + objID,"last")
     );
    }
   ); 
  }
 
0 Kudos
SebastianRoberts
Occasional Contributor III
I am part way there, but still having difficulty opening a separate attribute inspector for the related table.  I'd like to use the attribute inspector because it can handle the valiation for me.  From the documentation, it looks like you should be able to open the attribute inspector in a div outside the map.  As I understand it, the attribute inspector requires a feature layer and selected features in that feature layer.  Therefore,  I query the related records using queryRelatedRecords, and then select them using the selectFeatures method on the featureLayer for the related records. When I create the attribute inspector for the related records I assign it to a new div, but still the related records show up in the attribute inspector associated with editor widget and the primary point feature layer.  The div that it is supposed to show up in says "No features selected" (screen shots of app and debugger values attached).  This doesn't work, because I need to add a save button to the attribute inspector for the related records.  Is it possible to have two separate attribute inspectors open at the same time?  Is there some other better way to do this?

//global variable for the related record feature layer
relatedRecordMaintLayer = new esri.layers.FeatureLayer("https://myserver.net/ArcGIS/rest/services/internal/Sanitation_Inpsection/FeatureServer/1", {
    mode: esri.layers.FeatureLayer.MODE_SELECTION, //QUERY_SELECTION is working as well
    outFields: ['OBJECTID','PointID','Date','MaintType']
   });

.......

//I am able to display the key field for the related record in a table, and this is the function to show the attribute inspector for the related record that is clicked on in the table.
                   function showRelated(objID){
    var flQuery = new esri.tasks.Query();
    flQuery.where = "OBJECTID = " + objID + "";
    flQuery.returnGeometry = "false";
    propMaintLayer.selectFeatures(flQuery,esri.layers.FeatureLayer.SELECTION_NEW,function (selection)
     {
      var layerInf = [{'featureLayer':propMaintLayer,
         'showAttachments':false,
         'isEditable': true,
         'fieldInfos': [
         {'fieldName': 'OBJECTID', 'isEditable':false, 'tooltip': 'Object ID', 'label':'Object ID:'},
         {'fieldName': 'Date', 'isEditable':true,'tooltip': 'Inspection Date MM/DD/YYYY','label':'Date:'},
         {'fieldName': 'MaintType', 'isEditable':true, 'label':'Maintenance Type:'},
         {'fieldName': 'PointID', 'isEditable':false, 'tooltip': 'Foreign key', 'label':'Foreign Key:'},
         ]}];
      attInspector = new esri.dijit.AttributeInspector({
        layerInfos:layerInf
        },
        dojo.create("div",{id:"relateDiv"},"liRelate" + objID,"last")
      );
     }
    );
                        }
                 }
0 Kudos
DurpheyClifton
New Contributor II
Sebastian,

Did you have any luck with this concept?  I'm looking to have others add new overflow report information to a related table.  Your post here is the closest thing I've seen to accomplishing this task...

Thanks for any help you can provide!

-Durphey
0 Kudos
SebastianRoberts
Occasional Contributor III
Durphey,
  I did get this to work, but it took a while, and I ended up creating my own logic for the update, add, delete functionality of the related records.  I'm sure there is a better way to do it, but attached is javascript and html for the web app.
Best of luck,
Sebastian
0 Kudos
DavidBuehler
Occasional Contributor III

Sebastian,

I have a few questions as I am super new to the Javascript world. I am working on a project where the user needs to find an incident by either searching for a request id or a logger's name, then log follow up records to an incident, and once that incident is resolved change the status on the feature to closed. From what I gather above and figuring out what javascript I can, your code is meant to search for something, update a feature's attributes, as well as create new related records.  Is that correct?

Your code is the closest I have come to seeing something like what I want to do.  How hard would it be to modify what you already wrote to fit the scenario I listed above?  Basically one feature class with one related table for records, with the ability to edit the feature class and add new related records. While having the ability to search for the incident.

Suggestions would be most helpful.

Thank you

David

0 Kudos
SebastianRoberts
Occasional Contributor III

David,

   At the time that I created that web application, there was no functionality to edit related records.  Now the javascript API has a sample for querying and editing related records.  Also, you might be able to use Collector if it meets your needs.  Here is a blog post on related records for Collector.  You might even be able to use the Web Appbuilder, but I'm not sure if the edit widget supports related records yet.  In any case, I would use one those three solutions, rather than trying to wade through my rather messy javascript which is based on a very old version of the API.

Regards,

Sebastian Roberts

0 Kudos
IvanBrown
New Contributor III
I will need to add/modify/delete records of a related table over the web also.  How is this working for you all.  I would like to know before I dive into the JavaScript API.  I haven't used the JavaScript API yet.  I was using Flex before.
0 Kudos
PaulLohr
Occasional Contributor III
This is very good, Sebastian. Thanks for sharing it.

It sounds like your (1) of the 1:M was a feature class while the (M) was a table without geometry?
0 Kudos
SebastianRoberts
Occasional Contributor III
Paul,
  Yes you are correct, we are editing a point feature class that describes a sanitation resource, and a related table that describes service calls to this resource.
Sebastian
0 Kudos