<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Setting expression and code block as a script tool parameter when calculating field in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/setting-expression-and-code-block-as-a-script-tool/m-p/1340077#M69008</link>
    <description>&lt;P&gt;To your original post, I'd suggest&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;POAlayer = arcpy.GetParameter(0)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;to return a layer object instead of a string, and see if that fixes it.&lt;/P&gt;</description>
    <pubDate>Fri, 20 Oct 2023 15:44:28 GMT</pubDate>
    <dc:creator>AlfredBaldenweck</dc:creator>
    <dc:date>2023-10-20T15:44:28Z</dc:date>
    <item>
      <title>Setting expression and code block as a script tool parameter when calculating field</title>
      <link>https://community.esri.com/t5/python-questions/setting-expression-and-code-block-as-a-script-tool/m-p/1335260#M68849</link>
      <description>&lt;P&gt;Hello&lt;/P&gt;&lt;P&gt;I am having trouble is my script tool to set the parameters for the expressions and the code block. Is this something that is possible? If it is, to check Im adding the paramters correctly what do I need to specify? (ie data type, direction)&amp;nbsp; Thank you so much for your help!&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import arcpy
POAlayer = arcpy.GetParameterAsText(0)
POAexpression = arcpy.GetParameterAsText(1)
POAcode = arcpy.GetParameterAsText(2)

# Calculate field

arcpy.management.CalculateField(POAlayer, "POA", POAexpression, "PYTHON3", POAcode)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 05 Oct 2023 04:45:52 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/setting-expression-and-code-block-as-a-script-tool/m-p/1335260#M68849</guid>
      <dc:creator>JasmineSpring</dc:creator>
      <dc:date>2023-10-05T04:45:52Z</dc:date>
    </item>
    <item>
      <title>Re: Setting expression and code block as a script tool parameter when calculating field</title>
      <link>https://community.esri.com/t5/python-questions/setting-expression-and-code-block-as-a-script-tool/m-p/1335409#M68851</link>
      <description>&lt;P&gt;I don't see why it wouldn't work. Seems easy enough to test as-is though. Can you clarify what you're having trouble with?&lt;/P&gt;</description>
      <pubDate>Thu, 05 Oct 2023 15:07:12 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/setting-expression-and-code-block-as-a-script-tool/m-p/1335409#M68851</guid>
      <dc:creator>BlakeTerhune</dc:creator>
      <dc:date>2023-10-05T15:07:12Z</dc:date>
    </item>
    <item>
      <title>Re: Setting expression and code block as a script tool parameter when calculating field</title>
      <link>https://community.esri.com/t5/python-questions/setting-expression-and-code-block-as-a-script-tool/m-p/1335661#M68872</link>
      <description>&lt;P&gt;Thanks for your help. Im having troubles to try to define the parameters in the tool to make it work right.&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="JasmineSpring_0-1696559740179.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/82451i56035ACAD3DAA34C/image-size/medium?v=v2&amp;amp;px=400" role="button" title="JasmineSpring_0-1696559740179.png" alt="JasmineSpring_0-1696559740179.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;I couldnt find the data type option that made it work.&lt;/P&gt;&lt;P&gt;thank you !&lt;/P&gt;</description>
      <pubDate>Fri, 06 Oct 2023 02:37:10 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/setting-expression-and-code-block-as-a-script-tool/m-p/1335661#M68872</guid>
      <dc:creator>JasmineSpring</dc:creator>
      <dc:date>2023-10-06T02:37:10Z</dc:date>
    </item>
    <item>
      <title>Re: Setting expression and code block as a script tool parameter when calculating field</title>
      <link>https://community.esri.com/t5/python-questions/setting-expression-and-code-block-as-a-script-tool/m-p/1335756#M68876</link>
      <description>&lt;P&gt;Does it work if you define&amp;nbsp;POAexpression and POAcode as String?&lt;/P&gt;</description>
      <pubDate>Fri, 06 Oct 2023 14:04:48 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/setting-expression-and-code-block-as-a-script-tool/m-p/1335756#M68876</guid>
      <dc:creator>BlakeTerhune</dc:creator>
      <dc:date>2023-10-06T14:04:48Z</dc:date>
    </item>
    <item>
      <title>Re: Setting expression and code block as a script tool parameter when calculating field</title>
      <link>https://community.esri.com/t5/python-questions/setting-expression-and-code-block-as-a-script-tool/m-p/1339910#M69002</link>
      <description>&lt;P&gt;Hi ,&lt;/P&gt;&lt;P&gt;I am trying another way to do this using&amp;nbsp; update cursor and field calculator unsuccessfully.&amp;nbsp; I am wanting to change the null values of selected fields in feature class and multiply these fields and subtract them from 1 with the results in a new field. Thank you for you help&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import arcpy

# Get the selected layer
layer = arcpy.GetParameterAsText(0)

# Create a list of field names starting with "poa"
poa_fields = [field.name for field in arcpy.ListFields(layer) if field.name.startswith("poa")]

for field in poa_fields:
  with arcpy.da.UpdateCursor(layer, poa_fields) as cursor:
    for row in cursor:
      if row[0] is None:
         row[0]="1"
         cursor.updateRow(row)

# Use the Field Calculator to calculate the product
expression = "1" if not poa_fields else " * ".join([f"!{field}!" for field in poa_fields])
arcpy.CalculateField_management(layer, "Product_poa", expression, "PYTHON")&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 26 Oct 2023 05:34:13 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/setting-expression-and-code-block-as-a-script-tool/m-p/1339910#M69002</guid>
      <dc:creator>JasmineSpring</dc:creator>
      <dc:date>2023-10-26T05:34:13Z</dc:date>
    </item>
    <item>
      <title>Re: Setting expression and code block as a script tool parameter when calculating field</title>
      <link>https://community.esri.com/t5/python-questions/setting-expression-and-code-block-as-a-script-tool/m-p/1340072#M69007</link>
      <description>&lt;P&gt;Please fix the indentation in this code.&lt;/P&gt;</description>
      <pubDate>Fri, 20 Oct 2023 15:40:15 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/setting-expression-and-code-block-as-a-script-tool/m-p/1340072#M69007</guid>
      <dc:creator>BlakeTerhune</dc:creator>
      <dc:date>2023-10-20T15:40:15Z</dc:date>
    </item>
    <item>
      <title>Re: Setting expression and code block as a script tool parameter when calculating field</title>
      <link>https://community.esri.com/t5/python-questions/setting-expression-and-code-block-as-a-script-tool/m-p/1340077#M69008</link>
      <description>&lt;P&gt;To your original post, I'd suggest&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;POAlayer = arcpy.GetParameter(0)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;to return a layer object instead of a string, and see if that fixes it.&lt;/P&gt;</description>
      <pubDate>Fri, 20 Oct 2023 15:44:28 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/setting-expression-and-code-block-as-a-script-tool/m-p/1340077#M69008</guid>
      <dc:creator>AlfredBaldenweck</dc:creator>
      <dc:date>2023-10-20T15:44:28Z</dc:date>
    </item>
    <item>
      <title>Re: Setting expression and code block as a script tool parameter when calculating field</title>
      <link>https://community.esri.com/t5/python-questions/setting-expression-and-code-block-as-a-script-tool/m-p/1341723#M69033</link>
      <description>&lt;P&gt;#JasmineSpring, one thing you need to do after using any cursor, is you need to del the cursor.&amp;nbsp; Your code should look something like this...&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import arcpy

# Get the selected layer
layer = arcpy.GetParameterAsText(0)

# Create a list of field names starting with "poa"
poa_fields = [field.name for field in arcpy.ListFields(layer) if field.name.startswith("poa")]

for field in poa_fields:
   with arcpy.da.UpdateCursor(layer, poa_fields) as cursor:
      for row in cursor:
         if row[0] is None:
            row[0]="1"
            cursor.updateRow(row)
del cursor

# Use the Field Calculator to calculate the product
expression = "1" if not poa_fields else " * ".join([f"!{field}!" for field in poa_fields])
arcpy.CalculateField_management(layer, "Product_poa", expression, "PYTHON")&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;(see line 15).&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 25 Oct 2023 22:24:53 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/setting-expression-and-code-block-as-a-script-tool/m-p/1341723#M69033</guid>
      <dc:creator>Shauna-RaeBrown</dc:creator>
      <dc:date>2023-10-25T22:24:53Z</dc:date>
    </item>
    <item>
      <title>Re: Setting expression and code block as a script tool parameter when calculating field</title>
      <link>https://community.esri.com/t5/python-questions/setting-expression-and-code-block-as-a-script-tool/m-p/1341796#M69038</link>
      <description>&lt;P&gt;I used the parameter for the input layer as a layer. Thank you !&lt;/P&gt;</description>
      <pubDate>Thu, 26 Oct 2023 05:32:17 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/setting-expression-and-code-block-as-a-script-tool/m-p/1341796#M69038</guid>
      <dc:creator>JasmineSpring</dc:creator>
      <dc:date>2023-10-26T05:32:17Z</dc:date>
    </item>
    <item>
      <title>Re: Setting expression and code block as a script tool parameter when calculating field</title>
      <link>https://community.esri.com/t5/python-questions/setting-expression-and-code-block-as-a-script-tool/m-p/1341799#M69039</link>
      <description>&lt;P&gt;Thank you for your reply&lt;/P&gt;&lt;P&gt;I altered as above and the tool runs but the field doesnt calculate the product_poa field. Maybe the error is in the expression? I would like the field in the list to multiply and then be subtracted from 1.&lt;/P&gt;&lt;P&gt;eg . 1 - (field *field*field)&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The fields being taken from the list.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you very much&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 26 Oct 2023 05:51:11 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/setting-expression-and-code-block-as-a-script-tool/m-p/1341799#M69039</guid>
      <dc:creator>JasmineSpring</dc:creator>
      <dc:date>2023-10-26T05:51:11Z</dc:date>
    </item>
    <item>
      <title>Re: Setting expression and code block as a script tool parameter when calculating field</title>
      <link>https://community.esri.com/t5/python-questions/setting-expression-and-code-block-as-a-script-tool/m-p/1341800#M69040</link>
      <description>&lt;P&gt;sorry. Is that ok now? thanks for your help&lt;/P&gt;</description>
      <pubDate>Thu, 26 Oct 2023 05:52:43 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/setting-expression-and-code-block-as-a-script-tool/m-p/1341800#M69040</guid>
      <dc:creator>JasmineSpring</dc:creator>
      <dc:date>2023-10-26T05:52:43Z</dc:date>
    </item>
    <item>
      <title>Re: Setting expression and code block as a script tool parameter when calculating field</title>
      <link>https://community.esri.com/t5/python-questions/setting-expression-and-code-block-as-a-script-tool/m-p/1342100#M69048</link>
      <description>&lt;P&gt;For the calculate field to multiply all the fields together, try this. I'm assuming you're using ArcGIS Pro. This won't work in ArcMap.&lt;/P&gt;&lt;LI-CODE lang="python"&gt;# Use the Field Calculator to calculate the product
poa_fields_expr = ",".join([f"!{f.name}!" for f in poa_fields])
expr = f"my_calc_func({poa_fields_expr})"
# *args allows any number of arguments to be passed into the function.
code = '''def my_calc_func(*args):
    result = 1
    for poa_value in args:
        result = result * poa_value
    return 1 - result
'''
arcpy.management.CalculateField(
    in_table=layer,
    field="Product_poa",
    expression=expr,
    expression_type="PYTHON3",
    code_block=code
)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;As for the other part of your code sample wehre you are setting the first field to 1, I recommend using calculate field because it will be consistent with using calculate field for multiplying the fields and you can explicitly name the field you're calculating rather than implicitly expecting the first field to be the one you want to calc.&lt;/P&gt;&lt;LI-CODE lang="python"&gt;# Set the default value for "the field name here"
arcpy.management.CalculateField(
    in_table=layer,
    field="the field name here",
    expression="1"
)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Which brings me to a final point: I see you are setting that field to a string "1" instead of an integer 1. If your other poa fields are strings, you will need to convert them to a number in your code. Here is a final code (untested) that also forces the field values to be a number.&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import arcpy

# Get the selected layer
layer = arcpy.GetParameterAsText(0)

# Set the default value for "the field name here"
arcpy.management.CalculateField(
    in_table=layer,
    field="the field name here",
    expression="1"
)

# Format the list of fields for calculate field.
poa_fields_expr = [f"!{f.name}!" for f in arcpy.ListFields(layer, wild_card="poa*")]

# Build expression and code block to calculate the Product_poa field.
expr = f"my_calc_func({poa_fields_expr})"
# *args allows any number of arguments to be passed into the function.
code = '''def my_calc_func(*args):
    result = 1
    for poa_value in args:
        result = result * float(poa_value)
    return 1 - result
'''
arcpy.management.CalculateField(
    in_table=layer,
    field="Product_poa",
    expression=expr,
    expression_type="PYTHON3",
    code_block=code
)&lt;/LI-CODE&gt;</description>
      <pubDate>Thu, 26 Oct 2023 16:06:03 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/setting-expression-and-code-block-as-a-script-tool/m-p/1342100#M69048</guid>
      <dc:creator>BlakeTerhune</dc:creator>
      <dc:date>2023-10-26T16:06:03Z</dc:date>
    </item>
    <item>
      <title>Re: Setting expression and code block as a script tool parameter when calculating field</title>
      <link>https://community.esri.com/t5/python-questions/setting-expression-and-code-block-as-a-script-tool/m-p/1342456#M69052</link>
      <description>&lt;P&gt;Thank you so much !&amp;nbsp;&amp;nbsp;I am trying to test the calculation with the null values already = 1 as below. I get the error NameError: name 'poa_fields_expr ' is not defined&lt;/P&gt;&lt;LI-CODE lang="c"&gt;import arcpy

# Get the selected layer
layer = arcpy.GetParameterAsText(0)

# Create a list of field names starting with "poa"
poa_fields = [field.name for field in arcpy.ListFields(layer) if field.name.startswith("poa")]


 # Use the Field Calculator to calculate the product
expression =  " * ".join([f"!{field}!" for field in poa_fields])
expr = f"my_calc_func({poa_fields_expr})"

# *args allows any number of arguments to be passed into the function.
code = '''def my_calc_func(*args):
    result = 1
    for poa_value in args:
        result = result * poa_value
    return 1 - result
'''
arcpy.management.CalculateField(layer,"Product_poa",expr,"PYTHON3",code)&lt;/LI-CODE&gt;</description>
      <pubDate>Fri, 27 Oct 2023 01:44:46 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/setting-expression-and-code-block-as-a-script-tool/m-p/1342456#M69052</guid>
      <dc:creator>JasmineSpring</dc:creator>
      <dc:date>2023-10-27T01:44:46Z</dc:date>
    </item>
    <item>
      <title>Re: Setting expression and code block as a script tool parameter when calculating field</title>
      <link>https://community.esri.com/t5/python-questions/setting-expression-and-code-block-as-a-script-tool/m-p/1342657#M69055</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/642359"&gt;@JasmineSpring&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;&lt;P&gt;Thank you so much !&amp;nbsp;&amp;nbsp;I am trying to test the calculation with the null values already = 1 as below. I get the error NameError: name 'poa_fields_expr ' is not defined&lt;/P&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;The variable on line 11 needs to be renamed.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import arcpy

# Get the selected layer
layer = arcpy.GetParameterAsText(0)

# Create a list of field names starting with "poa"
poa_fields = [field.name for field in arcpy.ListFields(layer) if field.name.startswith("poa")]


 # Use the Field Calculator to calculate the product
poa_fields_expr =  " * ".join([f"!{field}!" for field in poa_fields])
expr = f"my_calc_func({poa_fields_expr})"

# *args allows any number of arguments to be passed into the function.
code = '''def my_calc_func(*args):
    result = 1
    for poa_value in args:
        result = result * poa_value
    return 1 - result
'''
arcpy.management.CalculateField(layer,"Product_poa",expr,"PYTHON3",code)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 27 Oct 2023 14:18:02 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/setting-expression-and-code-block-as-a-script-tool/m-p/1342657#M69055</guid>
      <dc:creator>BlakeTerhune</dc:creator>
      <dc:date>2023-10-27T14:18:02Z</dc:date>
    </item>
    <item>
      <title>Re: Setting expression and code block as a script tool parameter when calculating field</title>
      <link>https://community.esri.com/t5/python-questions/setting-expression-and-code-block-as-a-script-tool/m-p/1343635#M69082</link>
      <description>&lt;P&gt;Thank you Blake ! Very much appreciated. That works perfectly. Below is the final solution&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import arcpy

# Get the selected layer
layer = arcpy.GetParameterAsText(0)

# Create a list of field names starting with "poa"
poa_fields = [field.name for field in arcpy.ListFields(layer) if field.name.startswith("poa")]

for field in poa_fields:
    with arcpy.da.UpdateCursor(layer, poa_fields) as cursor:
        for row in cursor:
         row = tuple([1 if v is None else v for v in row])
         cursor.updateRow(row) 
   

 # Use the Field Calculator to calculate the product
poa_fields_expr =  " * ".join([f"!{field}!" for field in poa_fields])
expr = f"my_calc_func({poa_fields_expr})"

# *args allows any number of arguments to be passed into the function.
code = '''def my_calc_func(*args):
    result = 1
    for poa_value in args:
        result = result * poa_value
    return 1 - result
'''
arcpy.management.CalculateField(
    in_table=layer,
    field="Product_poa",
    expression=expr,
    expression_type="PYTHON3",
    code_block=code
)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 31 Oct 2023 00:58:12 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/setting-expression-and-code-block-as-a-script-tool/m-p/1343635#M69082</guid>
      <dc:creator>JasmineSpring</dc:creator>
      <dc:date>2023-10-31T00:58:12Z</dc:date>
    </item>
  </channel>
</rss>

