Call data from database and edit it with Python

Hello world,

Im new to erpnext and my first assignment is to edit data in the database.

What im trying to do is make a button and a call method, which i already have. Making the method im unsure of.

Essentially what this method should do is do a sql and find out if the docs status is new then update another doctype values. The doctypes im working with is issue and a custom doctype. When issue doctype is new then custom doctype fields values should change as well.

How do you start with this? I checked the code of other python files but am still confused about the syntax.

Please is there anyone that could possibly help?

Hey @Noob09, glad to have you here. :slight_smile:

Your workflow sounds vague, honestly. Maybe a step-by-step process breakdown would help in figuring out what you need exactly.

There is an is_new method that lets you identify new documents, but without more context, I can’t say if it’s the right solution.

Thank you for getting back.
Sorry im new so I’m still figuring things out. I Don’t need a button anymore what i need is a table that fetches data.

I created a child table called issue employees.
So in issue there should b a table where you select employees. When you save issue with employees. In employee the status of employee should Change from “available” to “assigned”.

Ah, I think I understand your problem now.

So,

  • when you save an Issue (the Save action will automatically trigger the validate method in issue.py),
  • you want to loop through the Issue Employees table (the self instance object in issue.py will contain all the information for the Issue you just saved, including the child table, so you can just loop through it using self.<insert child_table_fieldname>)
    • for example, if the table’s fieldname in Issue is employees, then you would use self.employees as the iterator.
  • get the Employee record, if any (you can do that using frappe.get_doc(doctype, docname))
    • for example, employee = frappe.get_doc("Employee", <insert employee_id>)
  • and then set the status of the Employee record (by either using the object you created using the get_doc in the previous step, or using frappe.db.set_value(doctype, docname, fieldname, value).
    • for example, either employee.status = "Available" or frappe.db.set_value("Employee", <employee_id>, "status", "Available")

You could also find a lot of these references by just searching through the code, but I hope this covers your questions.

1 Like

A good tool for interactively exploring Frappe is bench console which gives you repl-like access to the environment.

1 Like