Primary question: Is it truly required to use the SetParameter function to assign a variable to a derived output? My derived output is not updated in the map frame when SetParameter function is used in tandem with the "Use the selected records:" UI toggle button.
Hello, I am creating a python toolbox that has a Derived Output. The derived output is a selection made upon the Input Rows (feature layers or table views). However, I am noticing inconsistent behavior when I have a previous selection or Deffinition Query applied to my Input Rows when I use the "Use the selected records:" UI toggle.
When my selection or DQ is dissabled through the the "Use the selected records:" toggle, I am noticing that my python tool runs successfully but no new selection is added to my map frame. Any previous selection is cleared, but no new selection is added to the map. However, when I comment out the arcpy.SetParameter() line in my code the issue is resolved and I can create a new selection that overwrites any previous selection (which has been dissabled with the "Use the selected records:" toggle).
In the Notes at the bottom of the documentation section for script tool parameter Types you will see the second note says:
If the script tool has a derived output, you need to set the value of the derived output parameter in the script using the SetParameterAsText or SetParameter function.
However, in this case if I use the SetParameter function my derived output is not added to the map frame. How can I implement SetParameter in a way that allows the user to utilize the "Use the selected records:" toggle in GP tools? Or is this a bug with how SetParameter interacts with the toggle? I have attached the execution function for one of the tools in my toolbox below:
This is how I defined the derived output:
updatedRows = arcpy.Parameter(
displayName = "Updated Rows",
name = "out_layer_or_view",
datatype = ["GPFeatureLayer", "GPTableView"],
parameterType = "Derived",
direction = "Output"
)
updatedRows.parameterDependencies = [inputFeature.name]
updatedRows.schema.clone = True
See line 43 where I call SetParameter. If this line is removed, the "Use the selected records:" toggle can be dissabled and the new selection is displayed in the map frame. Am I using it wrong? Why isn't my new selection appearing in the map?
def execute(self, parameters, messages):
"""The source code of the tool."""
arcpy.env.workspace = parameters[0].valueAsText
inputFeatureAlias = parameters[0].valueAsText
input_file_path = arcpy.Describe(inputFeatureAlias).catalogPath
# Determine database type and find the file path for the uniquely named feature
desc = arcpy.Describe(input_file_path)
desc_workspace = desc.workspace
workspace_type = desc_workspace.workspaceFactoryProgID
if workspace_type == "esriDataSourcesGDB.FeatureServiceDBWorkspaceFactory": # A feature service
workspace_index = input_file_path.rfind("/")
else: # File, Mobile, or Enterprise Geodatabase and all other potential types
workspace_index = input_file_path.rfind("\\")
inputFeatureName = input_file_path[workspace_index + 1:]
inputFile = parameters[1].valueAsText
# Open JSON file and extract contents into selectionInfo
arcpy.AddIDMessage("INFORMATIVE", 86197, inputFile) #Opening: {inputFile}
file = open(inputFile, 'r')
selectionInfo = json.load(file)
file.close()
# Extract the value of various keys to a variable
try:
feature_class = selectionInfo["inputFeatureName"]
sql_expression = selectionInfo["expression"]
number_of_records = selectionInfo["selectedRecords"]
except Exception:
arcpy.AddIDMessage("ERROR", 80493) #Input config JSON cannot be parsed.
raise arcpy.ExecuteError
#Verify that input feature selected matches text file
if inputFeatureName != feature_class:
arcpy.AddIDMessage("ERROR", 270009, inputFeatureName, feature_class) #Input Feature or Table (inputFeatureName) does not match the inputFeatureName value (feature_class) in the file.
raise arcpy.ExecuteError
else:
pass
selection = arcpy.management.SelectLayerByAttribute(inputFeatureAlias, "NEW_SELECTION", sql_expression)
arcpy.SetParameter(parameters[2].name, selection) #Set the output parameter to the updated feature layer or tables
if selection[1] == number_of_records:
arcpy.AddIDMessage("INFORMATIVE", 270012, selection[1]) #Number of records selected: {selection[1]}
else:
arcpy.AddIDMessage("WARNING", 270008, selection[1], number_of_records) #The number of records reselected (selection[1]) does not match the number of records in the file (number_of_records).
arcpy.AddIDMessage("INFORMATIVE", 270012, selection[1]) #Number of records selected: {selection[1]}
return