ModelBuilder and "if" condition

07-26-2012 09:27 AM
Occasional Contributor II
I've searched, I've tried, I've tested, and I just can't get it.  I need help setting up the following in ModelBuilder:

Once I've performed a Search By Attribute task, I need to test if any rows were selected.  If so, I need to go to task B.  If not, I need to go to task C.  In my model I have a Calculate Value task (from ModelBuilder > Insert > Model Only Tools) and in the code I have this code, which I found on the ArcGIS - Desktop forum:

def hasSelection(layer):
    desc = arcpy.Describe(layer)
    fidset = desc.FIDSet
    if fidset =="":
        return "False"
        # A seelction exists so OK to execute
        return "True"

And in the Expression, I have hasSelection("%New Valves Selected%").  In my model, I made task B dependent on the result of the Calculate Value task, which I believe means it must return True.  But how do I make Task C dependent on the Calculate Value task being False?  This is what I don't get.

In the end, what I'm trying to avoid is doing a field calculate over the entire table if there are no records selected.
0 Kudos
1 Reply
MVP Frequent Contributor

I've used the technique you are using to stop/start a script based upon a boolean output from a calculate value tool but I've not implemented branching with it, not even sure if you can?

Anyway you need to write a branching script and expose it as a tool to be able to do this. It's documented here for 9.3 and here for version 10. You'll be able to view their Python code.

0 Kudos