How to trigger creation of another doc when current doc is saved

Explanation: I have 2 doctypes:- Doctype1 and Doctype2. Lets say, when I create a doc in Doctype1, based on information filled in Doctype1, I also want to create a doc in Doctype2. I require a solution to be in server script.

Problem: I am aware that inside of file, I can use frappe.get_doc and pass the info for Doctype2 and then insert or save it with .insert() or .save().

But I want that Doctype2 creation only once Doctype1 is saved inside db.

Any help is much appreciated.

Hi @Ashish_Pimpre,

Please apply it.

doc2 = frappe.db.get_list('DocType 2',filters={'link_field_of_doctype1':},fields=['name'],as_list=True)
if not doc2:
	doctype_2 = frappe.get_doc(dict(
		doctype = 'DocType 2',
		field_1 = doc.doctype_1_fieldname_1,
		field_2 = doc.doctype_1_fieldname_2

Then reload and check it.

Thank You!

1 Like

@NCP Thanks for your reply.
As I mentioned earlier, I do know that I can use .get_doc and .save() to insert doctype2.
My problem is that…
Lets say I create a Doctype1, the execution goes something like this:

  1. Creating (saving of doctype1 triggers (as of now since content in this files are being executed doctype1 is not saved yet)
  2. inside – code encounters or .insert(), so it moves to
  3. executed everything inside and saves it
  4. then comes back inside and finally saves it also in db

So basically, trying to save doctype1 works in way that doctype2 saves in db first and then doctype1 save itslef in db. What I want is doctype1 should first save it in db and only then should move on to saving doctype2.