How to run a custom script in quick entry

I’m trying to run a custom script in a quick entry dialog, but nothing works so far, any Ideas?

4 Likes

Hi @chabad360,

good question. None of the normal form triggers such as refresh, onload or setup will be triggered on the quick entry form.

Have you tried to build a .js file in a custom app and then hook this into the doctype?

Actually custom script load when you have load complete form of the doc type . but quick entry works on list of that doctype so that won’t be able to compute operation written in custom script of that doctype.

I would try stuff like this (and more), if not for the fact that I’m on the hosted instance, so I can’t do stuff like that.

Quick entry is load using frappe/frappe/public/js/frappe/form/quick_entry.js

There is an init_callback parameters that got call after dialog is rendered (on line 123) but I still don’t know how to add those.

I’ve tried adding

frappe.ui.form.on('Test', {
	after_insert: function(frm) {
		console.log('HelloWorld')
	},
});

Which won’t works.

So I’ve learnt something new, if you are making the quick entry dialog pragmatically, then you can use frappe.quick_entry to interface with the quick entry dialog. I think its the same thing as frm when it comes to handling it, but I don’t know for sure.

Through our other forum post, we we figured out that the ERPNext system places the code for all their quick_entry dialogues at this location :

/home/frappe/frappe-bench/apps/erpnext/erpnext/public/js/utils

However, I am wondering where/how do we add quick_entry dialogue files for custom app we built?

Here is what I used:



/*
This code overwrites the new_doc function of the link fields. I think that something similar can be done for "every" quickentry,
probably doing the same that is done here to the frappe.new_doc method, saving the script in the folder public/js and
including the javascript in the app_include_js hook like this:

app_include_js = ["/assets/APP_NAME/js/JS_NAME.js"]

And in the js file, something like:

frappe.old_doc = frappe.new_doc

frappe.new_doc = function(doctype, opts, init_callback) {
	if(doctype == "customdoctype"){
		frappe.old_doc("CUSTOMDOCTYPE", opts, function(dialog) {
			// ETC ETC
		})
	}
}

Anyways here is the link new_doc overwrite

*/


if (!frappe.ui.form.ControlLink.prototype.old_doc){
	frappe.ui.form.ControlLink.prototype.old_doc = frappe.ui.form.ControlLink.prototype.new_doc;
}

frappe.ui.form.ControlLink.prototype.new_doc = function () { 

	if(this.doctype == "DOCTYPE_WITH_LINK_FIELD"){

		frappe.ui.form.QuickEntryForm.prototype.old_is_quick = frappe.ui.form.QuickEntryForm.prototype.is_quick_entry;

		// Force quick entry
		frappe.ui.form.QuickEntryForm.prototype.is_quick_entry = function() {
			return true;
		}

		me = this
		var auto_complete = {"my_field": me.get_value()} // The values are going to be setted in the quick entry
		frappe.new_doc("DOCTYPE_FROM_THE_LINK_FIELD", auto_complete, function(dialog) {
			// If you don't do this, after the quick entry is saved, the complete form is opened
			frappe.quick_entry.after_insert = function(doc){ /*Do somethin*/ return}
		    
		    /*
			SCRIPT FOR THE QUICK ENTRY
			You can use dialog as the quick entry
			with it you can use get_field and jQuery things.

			For example:

			dialog.get_field("custom_field").$input.on("change", function(e){
				// LOGIC LOGIC LOGIC
			});

			You can use set_query too. Example:

			dialog.get_field("custom_field").get_query = function(doc,cdt,cdn) {
				return {
					filters:[
						// FILETERS...
					]
				}
			}

		    */

		    frappe.ui.form.QuickEntryForm.prototype.is_quick_entry = frappe.ui.form.QuickEntryForm.prototype.old_is_quick;
		});
		
	} else {
		this.old_doc();
	} 
}
2 Likes

Thank you so much for this, we will try this out.

1 Like