Add Custom Script to all DocTypes

Hi here,
I usually add my js client logic using a custom script for a single DocType. How I can add a custom script to ALL DocTypes? In other words: can I add a global script to an ERPNext application?
Thank you.

Yes, you can.
Add and build a js script with below codes in public js folder of your application :

$(document).on('app_ready', function() {
	$.each(["Opportunity", "Quotation", "Supplier Quotation", 
		"Sales Invoice", "Delivery Note",  "Sales Order",
		"Purchase Invoice", "Purchase Receipt", "Purchase Order"], function(i, doctype) {
		frappe.ui.form.on(doctype, "setup", function(frm) {
			// code for setup event
		});
		frappe.ui.form.on(doctype, "refresh", function(frm) {
                     // code for refresh event
		});
	});
});
6 Likes

Hi @sanjay thank you for your answer.
I’ve a production site with the following tree:

\ sites
\ sites \ assets
\ sites \ assets \ js
\ sites \ mysite
\ sites \ mysite \ public
\ sites \ mysite \ public \ files

I think to put your code inside a file into \ sites \ assets \ js directory, but I can see the index.html file to add this new script to load.
Thank you for your support.

If you have a custom application then you can add a custom js file in custom_app/public/js folder to avoid making changes in core code.

add build.json file with below code:
“js/myapp.min.js”: [
“public/js/myapp.js”,
]

in your custom application hook file add below code:
app_include_js = “/assets/js/myapp.min.js”

2 Likes

Thanks!

This is exactly what I want but how do I do it in the Cloud version? Any Custom Script needs to be assigned to a DocType, so how do I get it to run globally on every page?

It is not available in the cloud version (erpnext.com)

Well, darn.

Which app directory do you put your build.json file in?

@adam26d
frappe-bench/apps/my-app/my-app/public
For example:

The strategy is different for V14, because of the new build system. Migrating to Version 14 · frappe/frappe Wiki · GitHub

Example: check_run/check_run.bundle.js at version-14 · agritheory/check_run · GitHub

1 Like

Hello, this is for the form view. So is there any script for list view, applying it on all the list view of all the doctypes?

Thank you for the guidance Tyler.

I got it to work on v14