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?

PolyGeo
65.5k29 gold badges115 silver badges349 bronze badges
asked Jan 14, 2020 at 10:20
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. Commented Jan 20, 2020 at 14:55

1 Answer 1

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

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.