How to run a custom script in quick entry

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