I have a model which needs to only produce 1 final report but is dependent on whether a variable is provided.So there are 2 different flows following on from that selection
Something like this, a policy number can be provided or not, if it is 1 report is generated (output2) if its not(output)
A python script has been saved as a geoprocessing tasks with the 2 outputs to feed into different chains of the model.
My script within the processing tool is:
import arcpy
input_data = arcpy.GetParameterAsText(0)
policynumber = arcpy.GetParameterAsText(1)
if policynumber == '':
output = arcpy.SelectLayerByAttribute_management(input_data, 'NEW_SELECTION')
else:
output2 = arcpy.SelectLayerByAttribute_management(input_data, 'NEW_SELECTION', "PolicyNumber = '{}'".format(policynumber))
arcpy.SetParameterAsText(2, output)
arcpy.SetParameterAsText(3, output2)
I am getting the error that 'output' is not defined.
Is this because I've built the second output into the IF statement?
How can I fix it?
Is there a better/easier way to generate a different report dependent on whether a selection has occurred?
-
So, I've completed the full model, with separate workflows dependent on the IF statement. However it only seems to work with a policy number, despite setting that as an optional variable. If I don't enter a policy number, I want it to run on the full input file but it seems to just create a blank output.user156442– user1564422020年01月20日 14:55:39 +00:00Commented Jan 20, 2020 at 14:55
1 Answer 1
It looks like you only define one output, depending on the IF. And then you try to use both, regardless.
So maybe use:
output = arcpy.SelectLayerByAttribute_management(input_data, 'NEW_SELECTION')
output2 = arcpy.SelectLayerByAttribute_management(input_data, 'NEW_SELECTION', "PolicyNumber = '{}'".format(policynumber))
if policynumber == '':
arcpy.SetParameterAsText(2, output)
else:
arcpy.SetParameterAsText(3, output2)
-
Thanks, that's helped and fixed my problem!user156441– user1564412020年01月14日 16:19:10 +00:00Commented Jan 14, 2020 at 16:19
-
So, I've completed the full model, with separate workflows dependent on the IF statement. However it only seems to work with a policy number, despite setting that as an optional variable. If I don't enter a policy number, I want it to run on the full input file but it seems to just create a blank output. Do you have any suggestions?user156441– user1564412020年01月21日 09:26:58 +00:00Commented Jan 21, 2020 at 9:26
-
not sure, maybe show me the new codeFearlessCoward– FearlessCoward2020年02月17日 14:58:47 +00:00Commented Feb 17, 2020 at 14:58
Explore related questions
See similar questions with these tags.