Trigger Before Save Hook even when no change in document

Hi Everyone,
I am from V12 and trying V13.
In my V12 apps i often implement some logic (remote data fetching, update …) within the before_save controller hook. In V13 i see that this hook is not trigger if the doc did not change. Is there a way to disable this behaviour ?
What is the best practice in V13 to perform some logic after saving even when no change occurs ?

Thanks

1 Like

Someone to help please

Not out of the box, but it should be possible with a bit of coding. As I understand it, the change is all client side, so it should be fixable there.

The cur_frm variable has a method called dirty(), which if run should force a save even if no changes are made. You could use a custom script to catch the before_save event and then mark the form as dirty there. I’m on my phone right now and unable to test, but it should work.

1 Like

Hi, You’r right below is the corresponding function

	var save = function () {
		remove_empty_rows();

		$(frm.wrapper).addClass('validated-form');
		if ((action !== 'Save' || frm.is_dirty()) && check_mandatory()) {
			_call({
				method: "frappe.desk.form.save.savedocs",
				args: { doc: frm.doc, action: action },
				callback: function (r) {
					$(document).trigger("save", [frm.doc]);
					callback(r);
				},
				error: function (r) {
					callback(r);
				},
				btn: btn,
				freeze_message: freeze_message
			});
		} else {
			!frm.is_dirty() && frappe.show_alert({message: __("No changes in document"), indicator: "orange"});
			$(btn).prop("disabled", false);
		}
	};

The fact that frappe added this code in V13 (for performance optimisation i suppose) means that this is not a good practice to put some logic that do not depend on doc data within the hooks triggered when document is saved. My main question is: So what is the best practice. Let say i want to fetch some information that is in an other document when i save a document. Before, i was putting this fetching method within before_save hook but now how to do this ? What is the best way to do that in V13
Thanks

It’s hard to say what the best practice is without knowing the semantics of your workflow. What should trigger a remote update? Save seems like a less than ideal place to put remote updates, since it requires your user to know to hit save even though they haven’t changed anything.

Another option would be to create a standalone button to trigger the update, labeled appropriately.