Create Task on Expiration Date in Doctype

Hello,
i want to create a Serverscript that checks if an “Expiration date” is due in a specific doctype and automatically creates an task. I created a server script schedule and i see it gets executed without error but no task gets created. I tried debugging the script but i cannot get outputs from the web console.

def create_tasks_before_expiration():
    today_date = today()
    documents = frappe.get_all("Licence", filters={"expiration_date": today_date})

    for document in documents:
        task = frappe.new_doc("Task")
        task.subject = "License is about to expire"
        task.description = "License is about to expire"
        task.status = "Open"
        task.insert()

    frappe.db.commit()

Hello,

Use this below code, change the doctype as you required.

def check_expiration(doc, method):
    today = frappe.utils.nowdate()
    if doc.expiration_date and doc.expiration_date <= today:
        task = frappe.get_doc({
            "doctype": "Task",
            "subject": f"Expiration Date Due for {doc.name}",
            "description": f"The expiration date for {doc.name} is due.",
            "reference_type": doc.doctype,
            "reference_name": doc.name
        })
        task.insert(ignore_permissions=True)
        frappe.msgprint("Task created successfully!")

Now , on hooks add on_update to the doctype you selected and set the method name to check_expiration .