Acquire ObjectId value of inserted row

1456
5
Jump to solution
01-30-2018 09:06 AM
JamesCrandall
MVP Frequent Contributor

Using arcpy.da.InsertCursor, what is the best way to capture the ObjectId value of the row that was just inserted?

with arcpy.da.SearchCursor(finalFC, fieldnames, 'OBJECTID = {}'.format(oid)) as fscur:
       for fsrow in fscur:
           with arcpy.da.InsertCursor(scratchFC, fieldnames) as icur:
               icur.insertRow(fsrow)
               #I need to capture the OBJECTID value of the row that was just inserted in the line above
       del icur
1 Solution

Accepted Solutions
JoshuaBixby
MVP Esteemed Contributor

The ArcPy Data Access Insert Cursor returns the OID after inserting a new record:

with arcpy.da.SearchCursor(finalFC, fieldnames, 'OBJECTID = {}'.format(oid)) as fscur:
       for fsrow in fscur:
           with arcpy.da.InsertCursor(scratchFC, fieldnames) as icur:
               ioid = icur.insertRow(fsrow)
               print(ioid)
       del icur

View solution in original post

5 Replies
RandyBurton
MVP Regular Contributor

You might try:

MaxOID = next(arcpy.da.SearchCursor(scratchFC, ['OBJECTID'], sql_clause=(None,'ORDER BY OBJECTID DESC')))
print MaxOID[0] # use first element‍‍‍‍‍‍‍‍
JamesCrandall
MVP Frequent Contributor

Thanks Randy,

I'm a little concerned about relying upon maxOID, only because ultimately this will be published as a GP service and I want to be sure I'm only working with the specific geometry per request.  I hacked together an approach that simply writes the feature to an in_memory FC, run an update cursor on that, then use it to perform the final insert on the destination FC.

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

The ArcPy Data Access Insert Cursor returns the OID after inserting a new record:

with arcpy.da.SearchCursor(finalFC, fieldnames, 'OBJECTID = {}'.format(oid)) as fscur:
       for fsrow in fscur:
           with arcpy.da.InsertCursor(scratchFC, fieldnames) as icur:
               ioid = icur.insertRow(fsrow)
               print(ioid)
       del icur
JamesCrandall
MVP Frequent Contributor

Thank you! 

...again.

This saves me from dealing with %scratchworkspace% / in_memory workspace mess dealing with intermediate data -- it gets messy when that stuff happens as a gp service.  It also removes a bunch of convoluted complexity that is just not needed.

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

Just as an FYI, this is documented behavior in InsertCursor—Help | ArcGIS Desktop  (although most people tend to overlook the return value part)

Methods

insertRow (row)
ParameterExplanationData Type
row
[row,...]

A list or tuple of values. The order of values must be in the same order as specified when creating the cursor.

When updating fields, if the incoming values match the type of field, the values will be cast as necessary. For example, a value of 1.0 to a string field will be added as "1.0", and a value of "25" added to a float field will be added as 25.0.

tuple

Return Value

Data TypeExplanation
Integer

insertRow returns the objectid of the new row.