How to Get the Master Table Field with Dynamic Link on Contract DocType

Hi,
In Contract DocType, I would like to fetch the party full name based on selected party_type and party_name as below:

So, every time I change the Party Name (party_name) field, I need to update the Party Full Name (party_full_name) value with related party name, whether it fetch from Customer, Supplier or Employee depends on selected Party Type (party_type).

Note: Party Full Name (party_full_name) is my custom read-only field

For that purpose, I try to add custom scripts on Contract Doctype as follow:

frappe.ui.form.on('Contract', {
 party_name: function(frm) {
        /* when party_type field is changed */
        if (frm.doc.party_type == 'Customer') {
            /* if party_type is 'Customer', fetch the field 'customer_name' of 
            the customer selected in 'party_name' and write it into the 
            current form's field 'party_full_name' */
            frm.add_fetch('party_name', 'customer_name', 'party_full_name');
        } else if (frm.doc.party_type == 'Supplier') {
            /* if party_type is 'Supplier', fetch the field 'supplier_name' of 
            the supplier selected in 'party_name' and write it into the 
            current form's field 'party_full_name' */
            frm.add_fetch('party_name', 'supplier_name', 'party_full_name');
        } else if (frm.doc.party_type == 'Employee') {
            /* if party_type is 'Employee', fetch the field 'full_name' of 
            the employee selected in 'party_name' and write it into the 
            current form's field 'party_full_name' */
            frm.add_fetch('party_name', 'full_name', 'party_full_name');
        }
    },
})

With this script, I have two strange behaviors. First, every time I change the Party Name, the value of Party Full Name is only flash with the correct value and then goes blank as seen below:

contract_anim01


Second, if I change the Party Type, I got following error, even I can confirm through the browser js debugger, that “if” conditions are correctly executed.

Is there anything wrong with my custom script?

TIA,
Roy

ERPNext: v12.10.1 (version-12)
Frappe Framework: v12.8.0 (version-12)

This is done in the field definition for party_full_name you can find a field that says fetch from there you can do party_name.full_name I’m guessing the reason is being erased is because the field is set to read-only . If you want to go this route, which I won’t recommend since You’re putting logic on the client side, which won’t work using the API.

But if you wan’t to do it that way you should toggle the read only before writing to the field, and then set it back.

Hi @RoyIrwan have you already solved this? I also did the same code and got the same issue on the add fetch.

It is really hard if the party full name is not visible on the document since not all users know the naming series assigned to a particular record.

this is the working version just tested.

frappe.ui.form.on('Contract', {
    party_name: function(frm) {
        if (!frm.doc.party_name){
            frm.set_value('party_full_name','');
        }
        else{
            frappe.model.with_doc(frm.doc.party_type, frm.doc.party_name, function () {
				var party = frappe.model.get_doc(frm.doc.party_type, frm.doc.party_name);
				var fieldname = '';
				
                if (frm.doc.party_type == 'Customer') {
                    fieldname = 'customer_name';
                } else if (frm.doc.party_type == 'Supplier') {
                    fieldname ='supplier_name';
                } else if (frm.doc.party_type == 'Employee') {
                    fieldname = 'employee_name';
                }
    			frm.set_value('party_full_name', party[fieldname]);
    			refresh_field('party_full_name');
			})
		}	
    },
    party_type: function(frm) {
        frm.set_value('party_name','');
    }    
})
2 Likes

Hi @szufisher thank you so much. I really appreciate your help. Your code worked for me. Thank you so much. :smile:

Hi @szufisher maybe you can help me also with how to fetch data on a parent doctype and encode it on the child table using a dynamic link.

I was trying to fetch the data on a custom field on the parent doctype of the Sales Invoice then encode it on the child table payment entry references.

This is same on how the Supplier Invoice No. works. When the dynamic link is a Purchase Invoice, a supplier invoice no. field will be visible.