How to trigger something when save or submit is clicked in a doctype?

  1. How to trigger something when save or submit is clicked in a doctype?
  2. How to create a new transaction doctype when saving another doctype, like creating a sales invoice as soon as sales order is created?

Any help is appreciated. Thanks in advance

@Sai_Sasank

Go through the following links
Controllers

hook event

In that case, write on_submit hook event for Sales Invoice to create Sales Order

Isn’t there any javascript callback for Save or Submit?[quote=“Sangram, post:2, topic:16616, full:true”]
@Sai_Sasank

Go through the following links
Controllers

hook event

In that case, write on_submit hook event for Sales Invoice to create Sales Order
[/quote]

@Sai_Sasank

you can also write Custom Script for that.
Custom script

e.g.

cur_frm.cscript.on_submit = function(doc, cdt, cdn) { // your script }

cur_frm.cscript.validate = function(doc, dt, dn) { // your script }

2.) "How to create a new transaction doctype when saving another doctype, like creating a sales invoice as soon as sales order is created?"

For the above point
I’m trying something like this

frappe.model.with_doctype("Doctype Name", function() {
  var doc = frappe.model.get_new_doc("Doctype Name");
  doc.field_name = frm.field_name;
  frappe.set_route("Form", "Doctype Name", doc.name);
}

But this is not creating a new doc. This is opening a draft which i’m supposed to save and submit.

How can i automate the save and submit as well

@Sai_Sasank

try savesubmit() method

Tried that as well but of no use. Now i’m trying to generate a new doc from server side.
The code is as follows

def create_cabin_35_doc(self):

	doc = frappe.new_doc("Payables")
	doc.update({"doc.sales_invoice" : self,
				"doc.customer" : self.customer,
				"doc.customer_name" : self.customer_name
	})
	doc.insert()

Here some fields are mandatory. I’m getting the following error

`MandatoryError: [Cabin 35 Payables, CP - 00004]: sales_invoice, customer, customer_name

@Sai_Sasank

if you want to save it without mandatory fields.
then use this,
doc.flags.ignore_mandatory = True

I want to save the mandatory fields as well. In the doc.update i’m providing the mandatory fields.

How can i provide the mandatory fields during doc creation itself.

same as you sending other fields.

"doc.mandatory_fieldname": value

@Sangram
I tried that as well. This code i’m adding in the on_submit of Sales invoice doctype

def create_cabin_35_doc(self):

	sales_invoice_doc = frappe.get_doc("Sales Invoice", self.name)
	doc = frappe.new_doc("Cabin 35 Payables")

	doc.sales_invoice = sales_invoice_doc,
	doc.customer = sales_invoice_doc.customer,
	doc.customer_name = sales_invoice_doc.customer_name
	doc.insert() 

But i’m getting an error like this
TypeError: coercing to Unicode: need string or buffer, SalesInvoice found

In the doc i have a sales_invoice field where it is linked to > Sales Invoice . I’m populating that column by the above code is it okay?

should be doc.sales_invoice = sales_invoice_doc.name

Getting the following error
AttributeError: ‘tuple’ object has no attribute ‘get’

if you write event in hook.py
then it should be as below

def create_cabin_35_doc(doc, method):
	doc = frappe.get_doc({
		"doctype": "Cabin 35 Payables",
		"sales_invoice" = doc.name,
		"customer" = doc.customer,
		"customer_name" = doc.customer_name
	})
	doc.insert(ignore_permissions = True)

Ohh thanks it is working.
What is the difference between adding a function through hooks and through the docname.py