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.
JoinField
on the table first, then runMakeTableView
and use thewhere_clause
parameter instead of trying to set a definition query on map table view later, then at the end of the script usearcpy.SetParameter
/arcpy.SetParameterAsText