1

Running a script tool where the final output needs to be joined to a feature class in a map and a definition query set to the table view after it is joined to the feature class.

However, when I make the table view nothing is added to the map although I have add outputs to geoprocesses checked in the project options and I am using arcpy.env.AddOutputsToMap = True.

import, arcpy, numpy, pandas

DB = r"C:\Polygon_Staging_User1.gdb"


arcpy.env.workspace = DB
arcpy.env.overwriteOutput = True
arcpy.env.addOutputsToMap = True

# Create parameter variables - user iput
Creator = arcpy.GetParameterAsText(0)
arcpy.AddMessage("Creator: " + Creator)
DocumentClass = arcpy.GetParameterAsText(1)
arcpy.AddMessage("Document Class: " + DocumentClass)
start_date = arcpy.GetParameterAsText(2) #required
arcpy.AddMessage("Start Date: " + str(start_date))
end_date = arcpy.GetParameterAsText(3) #required
arcpy.AddMessage("End Date: " + str(end_date))
Username = arcpy.GetParameterAsText(4) #required

user_ref_table = "{0}\{1}{2}".format(DB,Username,"_reftable")

....a bunch of numpy and pandas manipulations

# Write to array, recasting the to_records array with .astype()
arcpy.da.NumPyArrayToTable(df.to_records(index=False).astype(tup_dtypes),user_ref_table)

# make ArcGIS Pro project object of map that enduser is interfacing with
aprx = arcpy.mp.ArcGISProject("CURRENT")

# make map object to add table view to
m = aprx.listMaps()[0]

# make a table view so table is added to layout
recList = arcpy.management.MakeTableView(user_ref_table, "FileNet_Records")
arcpy.SetParameterAsText(5, recList)

# create variable for polygon feature class
scratchFC = r"C:\Polygon_Staging_User1.gdb\Surveyor_Map_Docs_Edit"

# Join Table View to polygon feature class
arcpy.management.JoinField(recList, 'UniqueId', scratchFC, 'UniqueId')

# Make map table view list
tvList = m.listTables()

# Set definition query - there should only be one table in list but unfortunately there is nothing as the table view is not getting added to map

tvList[0].definitionQuery = "\"UniqueId_1\" IS NULL"

Per Esri, the arcpy.da.NumPyArrayToTable() is incapable of adding the table to layout, hence the MakeTableView. But that's also not adding anything to the map and I'm getting an index error:

line 109, in <module>
IndexError: list index out of range

If I comment out everything down from arcpy.SetParameterAsText(recList, 5) The table view does get added to the map. I tried using time.sleep() at various intervals from 30 - 90 to no avail. When I commented out the subsequent steps arcpy.SetParameterAsText(recList, 5) I timed how long it took for the table view to appear on the map and it was 20 seconds although I think if there is an error downstream from time.sleep() python will exit instead of pausing.

5
  • 2
    Your script has no outputs, so there's nothing to add. Set an output parameter in tool properties, then populate that parameter.
    – user2856
    Commented Jun 29, 2022 at 1:43
  • Trying to make heads and tales of it. Added output parameter as derived in tool properties but not sure how set parameterastext. Would the index be 5 if I already have 0, 1, 2, 3, 4 as get parametersastext for inputs? The type for the output param is is set to TableView and is derived. Does 2nd argument in setparamter astext need to be the output tableview name? Sorry, documentation is not clear to me.
    – geoJshaun
    Commented Jun 29, 2022 at 4:23
  • @ user2856 updated script and post but still getting same index out of range error.
    – geoJshaun
    Commented Jun 29, 2022 at 17:15
  • Do the JoinField on the table first, then run MakeTableView and use the where_clause parameter instead of trying to set a definition query on map table view later, then at the end of the script use arcpy.SetParameter / arcpy.SetParameterAsText
    – user2856
    Commented Jun 29, 2022 at 20:22
  • It has to be a definition query for the workflow. I did move the join step up right after making tableview. I think the issue is script tools will run all commands before adding output. Therefore the tableview can't be there when tvList[0].definitionQuery = "\"UniqueId_1\" IS NULL" happens. Seems like there should be a setting like "Add output immediately"
    – geoJshaun
    Commented Jun 29, 2022 at 20:53

1 Answer 1

1

Yes outputs are added at the end.

Do the JoinField on the table first, then run MakeTableView and use the where_clause parameter instead of trying to set a definition query on map table view later, then at the end of the script use arcpy.SetParameter / arcpy.SetParameterAsText.

The where_clause is a definition query and will carry through to the map. A very simple example:

enter image description here

import arcpy

t = arcpy.GetParameterAsText(0)
tv = arcpy.management.MakeTableView(t, "TestView", "id = 0")[0]
arcpy.SetParameter(1, tv)

enter image description here

And here's a simple example that tries to replicate your workflow. The table view is added to the map, if I also edit the feature class and add a record, the corresponding record disappears from the table view.

t = "TestTable"
fc = "TestPoly"

tv = arcpy.management.AddJoin(t, "ID", fc, "ID")[0]
tv = arcpy.management.MakeTableView(tv, "TestView", f"{fc}.ID IS NULL")[0]
arcpy.SetParameter(0, tv)
2
  • Thank you. I need matching records from the join table to disappear from the table view through virtue of the definition query. If I make the join prior to table view then there will be no way for the table view to know a record has been completed in the join table. Basically, the table view is a reference attribute table for end users to add values from to a feature class (the join table). Once they finish attributation on a newly drawn record the join table will have a Unique ID that matches the table view and thus it will disappear from the table view queuing up the next record to draw.
    – geoJshaun
    Commented Jun 29, 2022 at 22:10
  • No. Join Field permanently add the fields from join table to the underlying table (even when you use a table view), there is no link maintained to original join table so your logic won't work. You can use Add Join instead to link to the join table, but you still need to do this before you add it to the map.
    – user2856
    Commented Jun 29, 2022 at 22:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.