When during the form life-cycle does frappe.require execute the script?

Hello

I’m trying to move as much as possible of a Form Script from the WUI Script window into a script file and then execute it with frappe.require().

Using the User doctype, it’s working as expected.

Here’s the script.js file

console.log(frappe);
frappe.ui.form.on('User', {
	refresh: function(frm) {
		console.log("refresh:");
	},
	validate: function(frm) {
		console.log("validate:");
		console.log(frm);
	}
})

I then call this file in the WUI Form Script window with this

frappe.require(["assets/script.js"]);

Screenshot%20from%202022-04-09%2012-37-39

The initial refresh is triggered when the form loads along with any subsequent validation events

Screenshot%20from%202022-04-09%2013-21-18

However for the initial refresh event on a Custom doctype it does not trigger, only the subsequent validation and refresh after clicking the Save button.

Screenshot%20from%202022-04-09%2013-11-02

Why is this the case? And more importantly, is using frappe.require the correct way of moving all the Form script logic to a file?

why dont you use app_include_js or doctype_include_js and add the custom js files to your app ( assuming you have a custom app )

Thanks @neerajvkn

That’s the problem, it’s not a custom app. I’m simply customising the default app by adding a few additional doctypes and a bit of client-side code.

my suggestion would be to create a custom app and add your files there, that way you will have more control over it. like version control / portability of your scripts.

1 Like