Record Not Getting Saved

Hi I have created following API function:

@frappe.whitelist()
def create_downtime(asset_id):
    try:
        workstation = frappe.get_value("Asset", asset_id, "workstation")
        factory = frappe.get_value('Workstation', workstation, 'factory')
        factory_head = frappe.get_value('Factory', factory, 'factory_head')
        downtime = frappe.new_doc("Downtime Log")
        downtime.created_date = nowdate()
        downtime.assigned_to = factory_head
        downtime.asset_name = asset_id
        downtime.start_date_time = now()
        downtime.status = "Open"
        downtime.insert(ignore_permissions=True)
        return downtime
    except Exception as e:
        frappe.log_error(frappe.get_traceback(), "Create Downtime Error")
        return {
            "status": "error",
            "message": str(e)
        }

When I invoke this function using Postman I get following response:

{
    "data": {
        "name": "DT-24-10-0009",
        "owner": "api@indusworks.in",
        "creation": "2024-10-24 14:23:59.068547",
        "modified": "2024-10-24 14:23:59.068547",
        "modified_by": "api@indusworks.in",
        "docstatus": 0,
        "idx": 0,
        "created_date": "2024-10-24",
        "assigned_to": "navneet@indusworks.in",
        "asset_name": "asset1",
        "start_date_time": "2024-10-24 14:23:59.068455",
        "duration": 0.005921,
        "status": "Open",
        "iteration_count": 1,
        "workstation_name": "Test Workstation 1",
        "doctype": "Downtime Log"
    }
}

However the record is not visible on desk.
When I run it again I get the same record with same name but it does not show up desk.

the code runs on my local machine/site but not on the site hosted on frappe cloud.

What can be the problems?

Try the adding below code after insert

frappe.db.commit()
1 Like

The Issue persists… I cleared the cache… ran bench migrate… but still it is the same problem…

I also created another site to test… but its the same issue…

Is the record created in the database?

Try using save instead of insert and also use commit

downtime.save(ignore_permissions=True)

Database Record Not getting created

Try this

Thanks… frappe.db.commit() solved the problem…

Earlier the records were getting saved without frappe.db.commit()…

Any idea why now it needs this?

https://frappeframework.com/docs/user/en/api/database#frappedbcommit

It’s generally advisable not to use frappe.db.commit() directly because it can bypass important validations and hooks that are triggered during the save or insert process. Instead, you should use:

  • doc.save(ignore_permissions=True): saves the document and respects the validations and hooks.
  • doc.insert(ignore_permissions=True): used to insert a new document while also ensuring validations are respected.

Using these methods helps maintain the integrity of the data and ensures that any necessary validations and triggers are executed.

1 Like

I tried doc.save & doc.insert to record the transaction in DB but it is not working for some reason…

I have another site on FC running same function with doc.save. Their I have not faced any problem.

Not sure what is happening…