<?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 Select by Attributes (Date Range) from Hosted Survey123 Layer in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/select-by-attributes-date-range-from-hosted/m-p/1361507#M69426</link>
    <description>&lt;P&gt;Hello All!&lt;/P&gt;&lt;P&gt;I am trying to build a script and that takes several layers, and selects by attribute only records in those layers within a given date range.&amp;nbsp; &amp;nbsp;I am selecting from several hosted feature layers (point geometry) that are all within a single survey 123 hosted feature layer.&amp;nbsp; So there are parent records, and child+grandchild, and the date field they all share in common is called 'CreationDate' which is formatted as such: 12/6/2023 11:29:34 PM.&amp;nbsp; I am trying to run this as a script tool within ArcPro and a given .aprx.&amp;nbsp; Parameters are StartDate (Date, Input), EndDate (Date, Input), and SelectedLayers (Feature Layer, Input, Multiple Values Allowed)&lt;/P&gt;&lt;P&gt;When I run the script on a test copy of the data (same schema as real data I will eventually use), the script has print statements that indicate some records were selected and they are the ones I would expect.&amp;nbsp; However, that selection is not applied in the map.&amp;nbsp; When I open the table, it shows 0 records selected.&amp;nbsp; I do not understand why this is.&amp;nbsp; This is important because the next step of the script will be to use only those selected records within the give range to do a select by location process.&amp;nbsp; Any clue as to why the selection is not applied in the map but does show up in print statements as having happened (see screenshots)?&amp;nbsp; &amp;nbsp;I want the next step to be: take only the selected records in the six S123 layers, and select by location all records within 50ft of those that intersect another feature class.&amp;nbsp; The Script and Screenshots are provided below.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;import arcpy&lt;BR /&gt;from datetime import datetime&lt;/P&gt;&lt;P&gt;# Get the current ArcGIS Pro project&lt;BR /&gt;aprx_path = arcpy.mp.ArcGISProject("CURRENT").filePath&lt;/P&gt;&lt;P&gt;# Get input parameters from the user&lt;BR /&gt;start_date_str = arcpy.GetParameterAsText(0) # Start date as string (format: MM/DD/YYYY hh:mm:ss AM/PM)&lt;BR /&gt;end_date_str = arcpy.GetParameterAsText(1) # End date as string (format: MM/DD/YYYY hh:mm:ss AM/PM)&lt;BR /&gt;selected_layers = arcpy.GetParameterAsText(2).split(";") # Selected layers as a list&lt;/P&gt;&lt;P&gt;# Fixed date format for parsing&lt;BR /&gt;date_format = "%m/%d/%Y %I:%M:%S %p"&lt;/P&gt;&lt;P&gt;# Convert input date strings to datetime objects&lt;BR /&gt;start_date = datetime.strptime(start_date_str, date_format)&lt;BR /&gt;end_date = datetime.strptime(end_date_str, date_format)&lt;/P&gt;&lt;P&gt;# Open the ArcGIS project&lt;BR /&gt;aprx = arcpy.mp.ArcGISProject(aprx_path)&lt;/P&gt;&lt;P&gt;# Process selected layers&lt;BR /&gt;for layer_name in selected_layers:&lt;BR /&gt;# Access the map&lt;BR /&gt;maps = aprx.listMaps() # Get all maps in the project&lt;BR /&gt;&lt;BR /&gt;# Check if the layer is within a group&lt;BR /&gt;if "\\" in layer_name:&lt;BR /&gt;group_name, layer_name = layer_name.split("\\")&lt;BR /&gt;&lt;BR /&gt;# Strip leading and trailing spaces from the group name&lt;BR /&gt;group_name = group_name.strip()&lt;/P&gt;&lt;P&gt;# Try to find the group in any of the maps&lt;BR /&gt;group = None&lt;BR /&gt;for map_obj in maps:&lt;BR /&gt;try:&lt;BR /&gt;group = map_obj.listLayers(group_name)[0]&lt;BR /&gt;break # If group is found in any map, exit the loop&lt;BR /&gt;except IndexError:&lt;BR /&gt;pass # Ignore the error and continue searching in the next map&lt;BR /&gt;&lt;BR /&gt;if group is None:&lt;BR /&gt;arcpy.AddError(f"Group '{group_name}' not found in any map.")&lt;BR /&gt;continue # Skip to the next layer&lt;/P&gt;&lt;P&gt;# Get the layers within the group&lt;BR /&gt;layers = [lyr for lyr in group.listLayers() if lyr.name == layer_name]&lt;BR /&gt;else:&lt;BR /&gt;# If not within a group, search for the layer in any of the maps&lt;BR /&gt;layers = [lyr for map_obj in maps for lyr in map_obj.listLayers(layer_name)]&lt;/P&gt;&lt;P&gt;if not layers:&lt;BR /&gt;arcpy.AddError(f"Layer '{layer_name}' not found in any map.")&lt;BR /&gt;continue&lt;/P&gt;&lt;P&gt;layer = layers[0]&lt;/P&gt;&lt;P&gt;# Create a query to select features within the specified date range&lt;BR /&gt;date_query = f"CreationDate &amp;gt;= date '{start_date.strftime('%m/%d/%Y %I:%M:%S %p')}' AND " \&lt;BR /&gt;f"CreationDate &amp;lt;= date '{end_date.strftime('%m/%d/%Y %I:%M:%S %p')}'"&lt;/P&gt;&lt;P&gt;# Debugging output: Print layer name and date query&lt;BR /&gt;arcpy.AddMessage(f"Processing Layer: {layer.name}")&lt;BR /&gt;arcpy.AddMessage(f"Date Query: {date_query}")&lt;/P&gt;&lt;P&gt;# Select features based on the date query&lt;BR /&gt;arcpy.management.SelectLayerByAttribute(layer, "NEW_SELECTION", date_query)&lt;/P&gt;&lt;P&gt;# Debugging output: Print the count of selected features&lt;BR /&gt;result = arcpy.GetCount_management(layer)&lt;BR /&gt;count = int(result.getOutput(0))&lt;BR /&gt;arcpy.AddMessage(f"Selected {count} features.")&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="ScriptToolResult.png" style="width: 999px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/89210i0429D699FEE37E96/image-size/large?v=v2&amp;amp;px=999" role="button" title="ScriptToolResult.png" alt="ScriptToolResult.png" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
    <pubDate>Fri, 15 Dec 2023 17:22:06 GMT</pubDate>
    <dc:creator>GIS_Consultant_ASE</dc:creator>
    <dc:date>2023-12-15T17:22:06Z</dc:date>
    <item>
      <title>Select by Attributes (Date Range) from Hosted Survey123 Layer</title>
      <link>https://community.esri.com/t5/python-questions/select-by-attributes-date-range-from-hosted/m-p/1361507#M69426</link>
      <description>&lt;P&gt;Hello All!&lt;/P&gt;&lt;P&gt;I am trying to build a script and that takes several layers, and selects by attribute only records in those layers within a given date range.&amp;nbsp; &amp;nbsp;I am selecting from several hosted feature layers (point geometry) that are all within a single survey 123 hosted feature layer.&amp;nbsp; So there are parent records, and child+grandchild, and the date field they all share in common is called 'CreationDate' which is formatted as such: 12/6/2023 11:29:34 PM.&amp;nbsp; I am trying to run this as a script tool within ArcPro and a given .aprx.&amp;nbsp; Parameters are StartDate (Date, Input), EndDate (Date, Input), and SelectedLayers (Feature Layer, Input, Multiple Values Allowed)&lt;/P&gt;&lt;P&gt;When I run the script on a test copy of the data (same schema as real data I will eventually use), the script has print statements that indicate some records were selected and they are the ones I would expect.&amp;nbsp; However, that selection is not applied in the map.&amp;nbsp; When I open the table, it shows 0 records selected.&amp;nbsp; I do not understand why this is.&amp;nbsp; This is important because the next step of the script will be to use only those selected records within the give range to do a select by location process.&amp;nbsp; Any clue as to why the selection is not applied in the map but does show up in print statements as having happened (see screenshots)?&amp;nbsp; &amp;nbsp;I want the next step to be: take only the selected records in the six S123 layers, and select by location all records within 50ft of those that intersect another feature class.&amp;nbsp; The Script and Screenshots are provided below.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;import arcpy&lt;BR /&gt;from datetime import datetime&lt;/P&gt;&lt;P&gt;# Get the current ArcGIS Pro project&lt;BR /&gt;aprx_path = arcpy.mp.ArcGISProject("CURRENT").filePath&lt;/P&gt;&lt;P&gt;# Get input parameters from the user&lt;BR /&gt;start_date_str = arcpy.GetParameterAsText(0) # Start date as string (format: MM/DD/YYYY hh:mm:ss AM/PM)&lt;BR /&gt;end_date_str = arcpy.GetParameterAsText(1) # End date as string (format: MM/DD/YYYY hh:mm:ss AM/PM)&lt;BR /&gt;selected_layers = arcpy.GetParameterAsText(2).split(";") # Selected layers as a list&lt;/P&gt;&lt;P&gt;# Fixed date format for parsing&lt;BR /&gt;date_format = "%m/%d/%Y %I:%M:%S %p"&lt;/P&gt;&lt;P&gt;# Convert input date strings to datetime objects&lt;BR /&gt;start_date = datetime.strptime(start_date_str, date_format)&lt;BR /&gt;end_date = datetime.strptime(end_date_str, date_format)&lt;/P&gt;&lt;P&gt;# Open the ArcGIS project&lt;BR /&gt;aprx = arcpy.mp.ArcGISProject(aprx_path)&lt;/P&gt;&lt;P&gt;# Process selected layers&lt;BR /&gt;for layer_name in selected_layers:&lt;BR /&gt;# Access the map&lt;BR /&gt;maps = aprx.listMaps() # Get all maps in the project&lt;BR /&gt;&lt;BR /&gt;# Check if the layer is within a group&lt;BR /&gt;if "\\" in layer_name:&lt;BR /&gt;group_name, layer_name = layer_name.split("\\")&lt;BR /&gt;&lt;BR /&gt;# Strip leading and trailing spaces from the group name&lt;BR /&gt;group_name = group_name.strip()&lt;/P&gt;&lt;P&gt;# Try to find the group in any of the maps&lt;BR /&gt;group = None&lt;BR /&gt;for map_obj in maps:&lt;BR /&gt;try:&lt;BR /&gt;group = map_obj.listLayers(group_name)[0]&lt;BR /&gt;break # If group is found in any map, exit the loop&lt;BR /&gt;except IndexError:&lt;BR /&gt;pass # Ignore the error and continue searching in the next map&lt;BR /&gt;&lt;BR /&gt;if group is None:&lt;BR /&gt;arcpy.AddError(f"Group '{group_name}' not found in any map.")&lt;BR /&gt;continue # Skip to the next layer&lt;/P&gt;&lt;P&gt;# Get the layers within the group&lt;BR /&gt;layers = [lyr for lyr in group.listLayers() if lyr.name == layer_name]&lt;BR /&gt;else:&lt;BR /&gt;# If not within a group, search for the layer in any of the maps&lt;BR /&gt;layers = [lyr for map_obj in maps for lyr in map_obj.listLayers(layer_name)]&lt;/P&gt;&lt;P&gt;if not layers:&lt;BR /&gt;arcpy.AddError(f"Layer '{layer_name}' not found in any map.")&lt;BR /&gt;continue&lt;/P&gt;&lt;P&gt;layer = layers[0]&lt;/P&gt;&lt;P&gt;# Create a query to select features within the specified date range&lt;BR /&gt;date_query = f"CreationDate &amp;gt;= date '{start_date.strftime('%m/%d/%Y %I:%M:%S %p')}' AND " \&lt;BR /&gt;f"CreationDate &amp;lt;= date '{end_date.strftime('%m/%d/%Y %I:%M:%S %p')}'"&lt;/P&gt;&lt;P&gt;# Debugging output: Print layer name and date query&lt;BR /&gt;arcpy.AddMessage(f"Processing Layer: {layer.name}")&lt;BR /&gt;arcpy.AddMessage(f"Date Query: {date_query}")&lt;/P&gt;&lt;P&gt;# Select features based on the date query&lt;BR /&gt;arcpy.management.SelectLayerByAttribute(layer, "NEW_SELECTION", date_query)&lt;/P&gt;&lt;P&gt;# Debugging output: Print the count of selected features&lt;BR /&gt;result = arcpy.GetCount_management(layer)&lt;BR /&gt;count = int(result.getOutput(0))&lt;BR /&gt;arcpy.AddMessage(f"Selected {count} features.")&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="ScriptToolResult.png" style="width: 999px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/89210i0429D699FEE37E96/image-size/large?v=v2&amp;amp;px=999" role="button" title="ScriptToolResult.png" alt="ScriptToolResult.png" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 15 Dec 2023 17:22:06 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/select-by-attributes-date-range-from-hosted/m-p/1361507#M69426</guid>
      <dc:creator>GIS_Consultant_ASE</dc:creator>
      <dc:date>2023-12-15T17:22:06Z</dc:date>
    </item>
    <item>
      <title>Re: Select by Attributes (Date Range) from Hosted Survey123 Layer</title>
      <link>https://community.esri.com/t5/python-questions/select-by-attributes-date-range-from-hosted/m-p/1361545#M69427</link>
      <description>&lt;P&gt;It would help if you could insert your code going forward using the "Insert code sample" button.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;You open the Aprx before you make the selection.&amp;nbsp; Is the layer you are creating with your "select By attributes" actually getting added to the APRX?&amp;nbsp; The way I'm reading your code, you assign the first layer to your layer variable, and then feed that into your selectbyattribute tool.&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The reference says:&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;If the input is a feature class or dataset path, this tool will &lt;STRONG&gt;automatically create and return a new layer&lt;/STRONG&gt; with the result of the tool applied.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Are you sure that new layer is getting added to your APRX?&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Edit: If you were running this in the Python window in Pro, I think it would show your selection.&amp;nbsp; Since you're running it in a standalone Python script, I think you have to add your selection results to your APRX, maybe by creating a new layer as documented &lt;A href="https://pro.arcgis.com/en/pro-app/3.1/tool-reference/data-management/select-layer-by-attribute.htm" target="_self"&gt;here&lt;/A&gt;.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 15 Dec 2023 18:23:49 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/select-by-attributes-date-range-from-hosted/m-p/1361545#M69427</guid>
      <dc:creator>Kara_Shindle</dc:creator>
      <dc:date>2023-12-15T18:23:49Z</dc:date>
    </item>
    <item>
      <title>Re: Select by Attributes (Date Range) from Hosted Survey123 Layer</title>
      <link>https://community.esri.com/t5/python-questions/select-by-attributes-date-range-from-hosted/m-p/1361592#M69428</link>
      <description>&lt;P&gt;Thank you for the reply!&amp;nbsp; And I will add code in the appropriate place going forward&lt;span class="lia-unicode-emoji" title=":grinning_face_with_sweat:"&gt;😅&lt;/span&gt;&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So I think creating the new layer based on selection will make sense, particularly if I then need to use that selection as a basis for the next step.&amp;nbsp; However, I am not sure how that will work with a hosted feature layer based on Survey123.&amp;nbsp; I think I have correct permissions to do that, but it would be a lot of data to export I think.&amp;nbsp; Also, if I run it in the python window can the user still select a custom date range?&amp;nbsp; I wonder if going that route makes more sense.&amp;nbsp; As you can probably tell, I am new to this&lt;/P&gt;</description>
      <pubDate>Fri, 15 Dec 2023 19:31:53 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/select-by-attributes-date-range-from-hosted/m-p/1361592#M69428</guid>
      <dc:creator>GIS_Consultant_ASE</dc:creator>
      <dc:date>2023-12-15T19:31:53Z</dc:date>
    </item>
    <item>
      <title>Re: Select by Attributes (Date Range) from Hosted Survey123 Layer</title>
      <link>https://community.esri.com/t5/python-questions/select-by-attributes-date-range-from-hosted/m-p/1361969#M69431</link>
      <description>&lt;P&gt;the arcpy.GetParameterAsText is supposed to be used if you are making a tool out of this script.&amp;nbsp; Then it would map to a field in a tool dialog box that your user could input.&amp;nbsp; If you are using the Python window, I *think* your users would still have to manually update the date.&amp;nbsp; If you're going to deploy this for others, I'd suggest going the tool route.&lt;/P&gt;</description>
      <pubDate>Mon, 18 Dec 2023 13:15:12 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/select-by-attributes-date-range-from-hosted/m-p/1361969#M69431</guid>
      <dc:creator>Kara_Shindle</dc:creator>
      <dc:date>2023-12-18T13:15:12Z</dc:date>
    </item>
    <item>
      <title>Re: Select by Attributes (Date Range) from Hosted Survey123 Layer</title>
      <link>https://community.esri.com/t5/python-questions/select-by-attributes-date-range-from-hosted/m-p/1361977#M69432</link>
      <description>&lt;P&gt;Another thought:&lt;/P&gt;&lt;P&gt;If you are using the list of layers to perform a selection on, doesn't your&lt;STRONG&gt; layers = layers[0]&lt;/STRONG&gt; only select the first selection in your list?&amp;nbsp; My first thought would be that you need some sort of for loop to go through each layer in your list and apply your selection.&amp;nbsp; The sample below isn't perfect, but this is what I was thinking.&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;#for each layer in your selection
for layer in layers:
  #create a selection &amp;amp; write it to a temp layer
  selectedLayer = arcpy.management.SelectLayerByAttribute(in_layer_or_view, 
{selection_type}, {where_clause}, {invert_where_clause})
 
#Write selected features to a new feature class
 arcpy.management.CopyFeatures(selectedLayer, 'layer1')&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 18 Dec 2023 13:29:32 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/select-by-attributes-date-range-from-hosted/m-p/1361977#M69432</guid>
      <dc:creator>Kara_Shindle</dc:creator>
      <dc:date>2023-12-18T13:29:32Z</dc:date>
    </item>
  </channel>
</rss>

