ERPNext Foundation ERPNext Cloud Chat Blog Discuss Frappé* Donate

Cascading /Nested Add Fetch


#1

Issue : Want to fetch Sales Person Email id.

Doctype : Sales Person has field Employee
Doctype : Employee has field prefered_email

Now in Sales Order, i have added custom field “Sales Person”, now based on Sales Person selection , would like to fetch employee and from employee would like to retrieve prefered_email.

Sales Person – > Employee --> prefered_email

Is it possible (have you tried) to do two/second level add fetch, if yes please provide custom client script ?


#2

No need for custom script for the first two fields. Now, as you know each Sales Person has employee mentioned in it.

Assuming your fields are named sales_person, employee, prefered_email:

In Customize Form, as you have mentioned Sales Person in Options of sales_person, similarly mention sales_person.employee in Options of employee so the linked employee will be auto-filled.

For the prefered_email you may have to use a custom script. But still try if something like sales_person.employee.prefered_email works or not. I think it should because Employee mentioned in Sales Person too is a linked document.


#3

Thanks for the idea, but it didn’t worked i tried below settings.

For Employee if the field type is Link , Option column should have the linked doctype name , so below is not possible


#4

Ah yes if it is link this won’t work. Then you ought to use custom script.


#5

@jignesh_shah try below script to fetch employee id from Sales Person & for prefered_email set option as employee.prefered_email

Note : Set employee’s field type as a Link.

frappe.ui.form.on("Doctype", "sales_person", function(frm) {
    frappe.call({
        "method": "frappe.client.get",
        "args": {
             "doctype": "Sales Person",
             "name": frm.doc.sales_person
        },
        "callback": function(response) {
             var sales_person = response.message;
             if (sales_person) {
                  frm.set_value('employee', sales_person.employee);
             } else {
                  frappe.msgprint("Sales Person not found");
            }
        }
    }); 
});

#6

Thanks for your hep.

Sorry it didn’t worked. On Selection of Sales person , Employee and Email id are not fetched.

After selecting Employee , prefered_email is fetched , which is because of employee.prefered_email in the Option

Tried as below
Doctype

Custom Client script as suggested by you.

Result
image


#7

What version of Frappe and ERPNext are you on?

In the DocType expand the Employee field and see if there’s an input for fetch_from? If so, then write the sales_person.employee in fetch_from and not in options.


#8

ERPNext: v10.1.52 (master)

Frappe Framework: v10.1.47 (master)

fetch_from not available .


#9

try this i write for user_id field you can change whatever field you want to fetch.

frappe.ui.form.on("Sales Order", "sales_person",function(frm,cdt, cdn) 
    	{
		
		if(frm.doc.sales_person)
		{
				frappe.call({
				"method": "frappe.client.get",
				args: {
					doctype: "Sales Person",
					name: frm.doc.sales_person
				},
				callback: function (data) {
					if(data.message.employee)
					{
				frappe.call({
				"method": "frappe.client.get",
				args: {
					doctype: "Employee",
					name:data.message.employee
				},
				callback: function (data) {
					frm.set_value("p_email", data.message.user_id);

				}
});
					
					}
				}

});
			

	
		
		}

	   
	});

#10

Thanks All. it is working.

can set the values for the desired fields as below
frm.set_value(“target_field”,“source_field”)

frm.set_value(“employee”, data.message.employee);
frm.set_value(“prefered_email”, data.message.prefered_email);


#11