Server Side Script Trigger

Hi,
Is there any way to trigger a server side script based on the field like in client side scripts? I’ll explain.

frappe.ui.form.on("Doctype",{
	fielda:(frm) {
		frm.set_value("fieldb", "Script Running");
        refresh_field("fieldb");
	}
});

Whenever fielda is changed, this code snippet will save field B in fieldb. I want to achieve the same functionality with a server side script, with a field as a trigger instead of a doctype event. Is that possible? if so, how? Thanks

1 Like

I “think” this should get you close. Assume the DocType is named Foo

Python file = ../erpnext/<name of module>/doctype/foo/foo.py

class Foo(Document):
    def on_update(self):
        # Get an object of the document's previous values:
        doc_before_save = self.get_doc_before_save()
        if not doc_before_save:
            return
        # Compare the value of 'fielda' before versus after:
        if doc_before_save.fielda != self.fielda:
            # The value of 'fielda' is different; so update fieldb
            self.fieldb = 'Script Running'
            self.save()
    ...

Not working :pensive: My doctype is named Invoice PM and I made this changes to its .py file:

class InvoicePM(Document):
    def on_update(self):
        doc_before_save = self.get_doc_before_save()
        if not doc_before_save:
            return
        if doc_before_save.fielda != self.fielda:
            self.fieldb = 'Script Running'
            self.save() 

This doesn’t work, The value of fieldb is not affected in any way.

Strange.

Here’s an exact copy & paste from what I just tested with Customer DocType:

	def on_update(self):
		self.validate_name_with_customer_group()
		self.create_primary_contact()
		self.create_primary_address()

		if self.flags.old_lead != self.lead_name:
			self.update_lead_status()

		if self.flags.is_new_doc:
			self.create_lead_address_contact()

		self.update_customer_groups()

		# BEGIN TEST CODE
		doc_before_save = self.get_doc_before_save()
		if not doc_before_save:
			return
		if doc_before_save.account_manager != self.account_manager:
			self.customer_name = self.customer_name + '-1'
			self.save()
		# END TEST CODE

Every time I update the Account Manager, another ‘-1’ is added to the end of the Customer’s name.

There’s a slight issue with this, It didnt work with my custom doctype so I created a new erpnext site and made tha changes to the customer.py file. The first time I changed the account manager and saved the form, it added -1 to the customer’s name. My main concern to do this was to set a field as a trigger, meaning whenever the field is changed, the script runs, rather than before_save or after_save or other triggers. I still have to save the form for the script to run.

The server doesn’t know when the field is changed. Not until you Save.

Client-side scripting is the only way.

On the server side script, use “Before Save” event check if the field has changed -
Refer to Server Side Documentation

and use
if doc.get_doc_before_save.your_field != doc.your_field :

1 Like

do you solve this problem ?
i have same issue