What code is run when a doc is initially created?

Hello, what code is called as a new doc is created? I know ‘before_insert’ and ‘validate’ are called before saving. I want code to run when the user clicks the New button.

Is it Java or Python? What methods are called?

I am creating a custom app.

Thank you,

The doctype.js file is processed when creating a new document.

frappe.ui.form.on("Docman Document", {
	onload: function(frm) {
               //code here
        }
});

However, I have not figure out what gets triggered on the server side? Please advise. Thank you.

@zeroxcorbin, validate is the first event, in both sides

@max_morais_dmm Thank you for the reply.

None of the doctype events seem to be firing in my doctype.py file when a new doc is started.

from __future__ import unicode_literals
import frappe

from frappe import msgprint, _
from frappe.model.document import Document
from frappe.model.naming import make_autoname
from frappe.model.naming import get_default_naming_series


class DocmanDocument(Document):
	def onload(self):
		frappe.msgprint(_("Loading"))
		
		if self.get("__islocal"):
			frappe.msgprint(_("Is Local"))
		#setup dummy display until the doc is actually saved
			self.docman_revision = "000"
			self.docman_tab = "00"
		else:
			frappe.msgprint(_("Not Local"))
			
	def autoname(self):
		series = get_default_naming_series(self.doctype)
		if series == None:
			return
		self.docman_serialnum = make_autoname(series)
		self.name = "DWG_" + self.docman_serialnum
		
	def before_insert(self):
		frappe.msgprint(_("Inserting"))
		
	def validate(self):
		frappe.msgprint(_("Validating"))
		
	def on_update(self):
		frappe.msgprint(_("Updating"))
		
	def on_submit(self):
		frappe.msgprint(_("Submiting"))
		
	def on_cancel(self):
		frappe.msgprint(_("Canceling"))
		
	def on_trash(self):
		frappe.msgprint(_("Trashing"))

I do not get the message when the New button is clicked.

@zeroxcorbin if you share your logic we can help you!

onload run onload, so, is before everything, including the page rendering!

@max_morais_dmm I updated the post above with the entire doctype.py

Here is a screenshot of the new button I mentioned.

and this is when the doctype.py code does not seem to run.

The doctype.py code runs when the save button is clicked but not before.

From what I have noticed. Thank you.

The on_load does not seem to work in the above code.

if self.get("__islocal"):
	frappe.msgprint(_("Is Local"))
#setup dummy display until the doc is actually saved
	self.docman_revision = "000"
	self.docman_tab = "00"
else:

This code does not set docman_revision when the New button is pressed. Also, none of the messages from the other events are displaying.

This all works when viewing a saved document.

Thanks again,

@zeroxcorbin sorry, where do you is putting your logic? Can you tell me the path? I believe that do you are doing it wrong!

I put the code in the file the sytems created in my app directory.

/home/frappe/frappe-bench/apps/document_manager/document_manager/document_manager/doctype/docman_document/docman_document.py

Sorry for the long names. I will shorten the later.

@zeroxcorbin use validate onload is not for this purpose!

@zeroxcorbin, In current system there is no trigger available in server side which called onload of new document. You can use frappe.call in onload method from your js code, which work as below

frappe.ui.form.on(“Docman Document”, {
onload: function(frm) {
if(frm.doc.__islocal){
frappe.call({method: server_path, args: args})
}
}
});

Thanks, Rohit

1 Like

@zeroxcorbin, in the client side and in the Python controller samples:

frappe.ui.form.on("DocType", "validate", function(frm, cdt, cdn){
    if (frm.doc.__islocal){
        // do something
    } else {
        // do something else
    }
});
class Controller(Document):
    def validate(self):
        if self.get('__islocal'):
            # do something
        else:
            # do else
1 Like

@zeroxcorbin, in the client side and in the Python controller samples:

frappe.ui.form.on("DocType", "validate", function(frm, cdt, cdn){
    if (frm.doc.__islocal){
        // do something
    } else {
        // do something else
    }
});
class Controller(Document):
    def validate(self):
        if self.get('__islocal'):
            # do something
        else:
            # do else
2 Likes

@rohit_w Thank you very much.

That is what I needed to know.

@max_morais_dmm Thank you for the information and the video series link. Very informative.

@zeroxcorbin I have to run scripts after page is rendered in list view. Which trigger should I use?