0

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)

enter image description here

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?

1
  • 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 Jan 20 '20 at 14:55
2

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)
3
  • Thanks, that's helped and fixed my problem! – user156441 Jan 14 '20 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 Jan 21 '20 at 9:26
  • not sure, maybe show me the new code – FearlessCoward Feb 17 '20 at 14:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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