Hi there,
I’m working on a python script with the following goal: organization and tracking of property leases, their tenants, and the corresponding rent schedules. The script automates the creation and management of items and subitems in Monday.com, reflecting lease and rent-related information in a structured manner. It provides a way to easily keep track of lease details, tenant information, and rent payment schedules within the Monday.com platform.
I’ve got the main item creation down with the following structure
the script creates items and prevents duplicate items from being created on subsequent run throughs of the script.
The Sub items are also being created however I’m having a difficult time preventing duplicate sub items from being created each time the script is run. Another aspect it updating existing sub items if changes are found
This is the sub item structure
I’ve got variables set up for item column mapping and subitem column mapping which are working
I have a function to check the column values of my sub items which works correctly
def get_subitem_column_values(parent_item_id):
query = f’‘’
{{
items(ids: {parent_item_id}) {{
subitems {{
id
column_values(ids: [“dropdown”, “numbers”, “date0”, “formula”, “text0”, “date6”]) {{
id
title
value
}}
}}
}}
}}
‘’’
I’ve got a function for creating or updating sub items, checking the value in the ChargeID (text0) column for a unique identifier
def create_or_update_subitem(parent_item_id, sub_item_data):
existing_subitem_id = get_subitem_id_by_charge_id(parent_item_id, sub_item_data[‘ChargeID’])
monday_api_url = "https://api.monday.com/v2"
headers = {
"Authorization": M_API_KEY,
"Content-Type": "application/json"
}
column_values = ', '.join(
[f'\\"{SUBITEM_COLUMN_MAPPING[key]}\\": \\"{value}\\"' for key, value in sub_item_data.items() if
value is not None])
if existing_subitem_id:
# Update existing subitem columns if needed
query = f"""
mutation {{
change_multiple_column_values(item_id: {existing_subitem_id}, board_id: {BOARD_ID}, column_values: {{ {column_values} }}) {{
id
}}
}}
"""
else:
# Create new subitem
query = f"""
mutation {{
create_subitem(parent_item_id: {parent_item_id}, item_name: "{sub_item_data['Name']}", column_values: "{{ {column_values} }}") {{
id
}}
}}
"""
data = {"query": query}
response = requests.post(monday_api_url, json=data, headers=headers)
if response.status_code == 200:
response_json = response.json()
sub_item_id = response_json.get("data", {}).get("create_subitem", {}).get("id")
if sub_item_id:
return sub_item_id
else:
print("Failed to get the ID of the created sub-item.")
print("Response content:")
print(response.content)
return None
else:
print(f"Failed to create or update sub-item. Status code: {response.status_code}")
print("Response content:")
print(response.content)
return None
Everything has come together pretty easily except for this last part that will prevent duplicate sub items from being created and updating existing sub items if anything needs to be updated
Any help or a point in the right direction would be extremely appreciated.
Thank you