Help linking Doctypes

I just started to deal with this system. Please help sort it out.
I created two Doctipes.
In the Doctype1 I have fields - “name” with type “date” and “status” with “type select”.
“status” can be equal to open or close.
I have entries in doctype1.
when changing “status” to “close”, I need to automatically create a new entry in doctypes2 with the name from doctypes1.
How to do it better?

I using
added a condition from the training video there

doc_events = {“doctype1” : {“after_insert”: “myapp.fyle_python.insert_function”}}

and it worked.
But now a new entry is inserted in doctype2, provided that I create a new entry in doctype1.
What condition to write in hook
to work on a change in “status”

You can do this in your python file of doctype1, there is a validate() function which runs whenever we save the form so when you change the status and save the form and if status is “close” you can write query to insert a record in doctype 2, can also be done using on_update either inside the python file of doctype1 or in hooks, you will have to check if status is equal to close, search for examples by checking the code of hooks used in frappe or erpnext app

If you are using custom app then the best way is to do this in your python file of doctype1

I found in docs
#To use a controller hook, just define a class method with that name. For e.g
class Person(Document):
def validate(self):
if self.age > 60:
frappe.throw(‘Age must be less than 60’)

but what is a method frappe (instead “throw”) I must using for triggering my hook ?

I am unable to understand why you need hook in this situation? In validate function when you check if status == “close”, you have to create new record in doctype2
r = frappe.get_doc(“doctype”: “doctype2”, “field1”: “itsvalue”)
r.insert, this is the way to insert records programmatically.
And generate a msg frappe.msgprint() that new record is saved if needed, there is no need of hooks according to my understanding

Thanks for the answer.
I don’t understand how to use your offer without hooks.
How to record that an action should occur in response to an event?
I tried it for everyone. Example Recorded in

class doctype1 (Document):
def change_fun(self):
if self.status==‘close’:
r = frappe.get_doc({“doctype”: “doctype2”, “first_name”: “_exmp_name_dima”})
msg frappe.msgprint(“okey”)

Nothing happens.

class doctype1 (Document):
def change_fun():
r = frappe.get_doc({“doctype”: “_name_of_doctype2”, “first_name”: “exmp_name_dima”})

it is working,

but how to insert a condition into this code
if status == “close”:

solution to the problem.

doc_events = {“name_of_doctype1”: {“on_change”: “name_app.api.name_fun_from_api_file”}}

create file in catalog /apps/name_app/name_app
in file create function name_fun_from_api_file with necessary conditions

for example

def name_fun_from_api_file(doc, method):
if doc.status ==“close”: your conditions
else: your other conditions

1 Like