Link Field (Validate and Fetch)

I am having a Custom Doctype that has Link fields get’s filled using cur_frm.add_fetch method. That is actally working fine. But after the value is fetched, the other fields dependent on the link field is not getting updated. That is getting updated when I manually select the Link from the dropdown. How to make it automated from the script?

Try cur_frm.refresh_field("fieldname")

See, for example:

Not working!! No changes!!

Try both commands, like in above link.

This is my custom script. program and student both are Link fields.

cur_frm.add_fetch("program_enrollment", "student", "student");
cur_frm.add_fetch("program_enrollment", "program", "program");

frappe.ui.form.on("Student ID Card", "program", function(frm){
	frm.refresh_field("program");
	frm.refresh_fields();
});

frappe.ui.form.on("Student ID Card", "student", function(frm){
	frm.refresh_field("student");
	frm.refresh_fields();
});

So, I have a another field that actually fetches value from program, lets say program.code in the options, so it should work when the Link field gets updated.

No hope.

In the above image, when the Program Enrollment ID is updated, both the Student and Program get’s filled from the above code. The naming series is actually fetched from the program, because it is dynamic. But that is not happening, unless I type the program manually and select one from the dropdown.

Try calling on refresh rather than field update, like this:

ICYMI, make sure to clear cache using Ctrl + Shift + R

That should be something equivalent to cur_frm.refresh() but I think that works only in saved documents. My document is unsaved.

Here, refresh has no effect.

If u are using link field it’s better to call a python method returning result of a frappe.db.sql instead refreshing

See, it is the exact issue!! All I need is to reproduce what the dropdown does. That should fix it. Any idea how to do it?

As I wrote before I think you should use a frappe.call on field change and call a python method to return the other field values using frappe.db.sql passing the value of the changed field …

Some examples please??

I solved it.

There is a method called cur_frm.trigger_link_fields which has the code

_f.Frm.prototype.trigger_link_fields = function() {
    if (this.is_new() && this.doc.__run_link_triggers) {
        $.each(this.fields_dict, function(fieldname, field) {
            if (field.df.fieldtype == "Link" && this.doc[fieldname]) {
                field.set_value(this.doc[fieldname]);
            }
        });
        delete this.doc.__run_link_triggers;
    }
}

All I did is took the $.each routine and updated my code like this (assuming this is cur_frm)

cur_frm.add_fetch("program_enrollment", "student", "student");
cur_frm.add_fetch("program_enrollment", "program", "program");

frappe.ui.form.on("Student ID Card", "program", function(frm){
	$.each(cur_frm.fields_dict, function(fieldname, field) {
            if (field.df.fieldtype == "Link" && cur_frm.doc[fieldname]) {
                field.set_value(cur_frm.doc[fieldname]);
            }
        });
});

frappe.ui.form.on("Student ID Card", "student", function(frm){
	$.each(cur_frm.fields_dict, function(fieldname, field) {
            if (field.df.fieldtype == "Link" && cur_frm.doc[fieldname]) {
                field.set_value(cur_frm.doc[fieldname]);
            }
        });
});

It is working like a charm now.

2 Likes

Hi,
I have a cleaner solution.

frappe.ui.form.on("Student ID Card", "program_enrollment_id", function(frm){
    frm.add_fetch("program_enrollment", "student", "student");
    frm.add_fetch("program_enrollment", "program", "program");
    frm.set_value("program", frm.doc.program)
});

Let me know if it works.

Hi Sibidharan,

In my case, depending upon the series field, i need to fetch the Letter head link field automatically.

For example, If series is SINV, the letter head should be Letter1, if series is SRET, the letter head should be Letter2.

Please help me.

Thanks.

Can you help me with that:

I think it is a similar problem, I try to understand what you have done, but I can not…

No, it doesn’t work.