Form triggers with small custom script

Good day all

Busy writing a small script to fetch field value from one doc to another.

The one works and the other doesn’t and I suspect its because of form-triggers.

First one works…
Docs involved - Sales Invoice + Customer
Add custom field in “Sales Invoice” called “account_manager”, to be populated from
the “account_manager” field in “Customer”

Added this script in Sales Invoice …

frappe.ui.form.on('Sales Invoice', {
	setup(frm) {

So, from the Customer document, I select to add “Sales Invoice” and it populates the
“account_manager” field in SI.

When I do something similar with …
“Job Card” and “Work Order” it doesn’t work.
Add custom field “sales_order” in “Job Card” and wanting to populate it with “sales_order”
from “Work Order”.

frappe.ui.form.on('Job Card', {
	refresh(frm) {

I do understand that there is a slightly different flow when the job-card is created from the

In fact if I select top right “Create Job Card” then my code works.
If I open the JC that was created from the WO, then it does not populate the “sales order” field.

Also, when I open the JC and I see that the sales_order field has not populated, then , when
I re-select the “work-order” field on the JC, it populates the sales_order field.

So it is clear that I am missing something about the triggers involved. I also noticed that the JC
is ready to be sub-mitted, which means it was already “saved”.

Would appreciate some help on this…

P.S> I tried some of the other triggers …setup, before_load, onload, refresh, onload_post_render.

Good day all

Isn’t there someone that can assist me with this.

Just trying to understand how these triggers work. I found this doc that shows some of the triggers
and when it happens …

The way I understand the “cur_frm.add_fetch” is that it executes the “fetch” if the field is


so account_manager will only be fetched if the “customer” field changes.

In the case of a document loading, that field does actually change when the doc is rendered.
So it should trigger the “add_fetch”. As explained it works for the 1st example given above and
only works some of times in the second example above.

Just trying to understand the difference, why it does not work sometimes ?


After a lot of googling …
Credit also to other people that posted code on the group, such as this one …

Still a bit in the dark about triggers, but there is light at the end of the tunnel.
I think what I am battling a bit is the terms used …
“load” and “render” what does that imply? During the “construction” of the page that
is viewed by the user , a few things are happening … starting with the doctype and also
considering any customisations that is in the system for that form … loading data … and ending up
with the page as viewed. And some of those processes are called “loading” and
“rendering” and to make sense of all this, is key.

Currently testing code that seem to be working well that triggers on “refresh”.
It does use , like the posting above, the “frappe.client.get_value” call, and not add_fetch.
It works both in the case of opening the “auto-created” job card and when you create
your own job card from the work-order.

With the “add_fetch” it always worked when the “work order” field was modified. But to auto-fetch
existing data , it seems that the best is to do one’s own fetching.I tried “frappe.db.get_value” as well
but couldn’t get that to work. Perhaps I must find some time to revisit that one , now that
“frappe.client.get_value” is working.

So “add_fetch” is more geared towards a field being modified …???

Here is a nice doc I found …


Making small progress.