Model Builder - Calculate Field tool error

491
2
Jump to solution
10-03-2023 08:54 AM
Labels (2)
HollyGraham
New Contributor

Hi, I created a model to run a series of tools, my last step is taking an output table called "screensoutput" with a an empty field within called "inspected" and I want to calculate the "inspected" field. To calculate the "inspected" field, I'm using the screensoutput table (which is a list of assets), and referencing it against "TableR" which contains inspection records for the assets. I want to know what assets haven't been inspected.

Im running the calculate field tool with screensoutput as my input table, selecting the field "inspected" that I want to populate, and the code I'm using is asking to put a 1 where there is a match, and 0 where there is no match when compared to the TableR (list of inspections). The key field in both is screen_id (which is the asset ID)

I keep getting a syntax error. And i think it doesnt like my table name, but I'm sure I've got it right, the table is an output of my model in a previous step, so unless thats becoming an issue?

HollyGraham_0-1696348033380.pngHollyGraham_1-1696348061274.pngHollyGraham_2-1696348106162.png

 

Any ideas how to fix? this is the final step to get the results and I just cant figure it out. It feels like it should be fairly straight forward. 

 

 

0 Kudos
1 Solution

Accepted Solutions
JohannesLindner
MVP Frequent Contributor

The expression field expects a simple Python expression. Put your function into the Code Block field.

# Inspected =
check_screen_id(!SCREEN_ID!)  # change to your screen id field name, keep the exclamation marks

# Code Block
def check_screen_id(screen_id):
    TableR = "..."
    with arcpy.da.SearchCursor(TableR, ["screen_id"]) as cursor:
        for row in cursor:
            if row[0] == screen_id:
                return 1
    return 0

Have a great day!
Johannes

View solution in original post

2 Replies
JohannesLindner
MVP Frequent Contributor

The expression field expects a simple Python expression. Put your function into the Code Block field.

# Inspected =
check_screen_id(!SCREEN_ID!)  # change to your screen id field name, keep the exclamation marks

# Code Block
def check_screen_id(screen_id):
    TableR = "..."
    with arcpy.da.SearchCursor(TableR, ["screen_id"]) as cursor:
        for row in cursor:
            if row[0] == screen_id:
                return 1
    return 0

Have a great day!
Johannes
HollyGraham
New Contributor
Johannes - thankyou so much!!
0 Kudos