Need to display name field of linked doctype

Hi,

I have created a custom doctype - product and it has a field -product and naming series as PRO.#####. I have given it as a link field to another doctype. When i select product it is showing the series first as below,

img1

After selecting the option it is showing only the series, i need the product field to be shown,
img2

Kindly help on this.

Thanks in advance!

1 Like

Therefore you have to create a second field, e.g. “Product Name” (readonly and which fetch the name of the linked product)

Thanks for the info, should i give the product name field in product doctype

no in your custom doctype, right next to “Product”-Link field…there you want the information…

at the time, you have in your custom doctype a link field called product which links to “your_product_doctype”.
Now you need a second field (type “Data”) in your custom doctype wich fetch the “Product Name” from “your_product_doctype”. There for you have to add a new field something like that:

3 Likes

Thank you so much for the info, i tried and got the below output,

2019-10-16%2004-25-20

But i need the product name instead of id while selecting itself in product field as per our need, is it possible to do so…

So you want “Sample” instead of “PRO00009” in field “Product”?

Yes exactly…is it possible to do like that through any custom script. I tried link formatter method its not working…

sorry that isnt possible

but with the workaround (product_name field) you have the same informations…why is that not ok for you?

Because it’s clumsy, looks ugly and is not a standard behavior for any other DDL/combobox users have experienced in their lives :grinning:

@Jenisha
image
I made a hack that does this, if you’re interested.

I did not mean to say it’s good as it is :wink: :smiley:

I think there are so many interested in it (including me).

can you post your hack here? :smiley:

I did not mean to say it’s good as it is :wink:

Yeah, I get what you mean, I just shared my pain :grinning:

can you post your hack here?

Sure, I’m isolating a reproducible piece of code now, will update this post a bit later.

got it :smiley:

thanks a lot!

Sure i am too interested to know :grinning:

Put this in your Form’s .js file:

// return placeholder text if link_field value is falsy
let text_to_show = value => {
	return value ? value : '-';
};

// this function will be called from the Form's onload/onrefresh 
// to fetch the value from the DB and render the Link field title initially
let init_field_title = (frm, link_field_name) => {
	if (!frm.fields_dict[link_field_name].value)
		return;
	frappe.db.get_doc(
		frm.fields_dict[link_field_name].df.options,
		frm.fields_dict[link_field_name].value
	).then(result => {
		render_field_title(frm, link_field_name, text_to_show(result.title));
	});
};

// the render function itself, just to be DRY
let render_field_title = (frm, title, text) => {
	let field = frm.fields_dict[title];
	field.label_span.innerHTML = `${__(field._label)}&nbsp-&nbsp<b>${text}</b>`;
};

frappe.ui.form.on('Target Doctype Here', {
	refresh: frm => {
		init_field_title(frm, 'link_package');
	},

	// update the link field title on the selection change
	link_package: frm => {
		let field = 'link_package';
		frappe.db.get_doc(frm.fields_dict[field].df.options, frm.fields_dict[field].value).then(result => {
			render_field_title(frm, field, text_to_show(result.title));   // my Link fields have a display field named 'title'
		});
	},
});

This hack pounds at the database mercilessly if there’s a lot of Link fields on the form, unfortunately. A better (not really better, just less db-expensive) way would have been to monkey-patch the Awesomeplete object to intercept the key-value data, but reeeeeeeeeealy don’t want to go there :grinning:

I hope the devs will find some time to remedy this DDL oddity.

8 Likes

Thanks a lot for this code…will surely give it a try! :slightly_smiling_face:

I have a query
I have two doctypes one is Pryto projects and other is Site Visits_1 so I want to know how to get the details from Pryto projects and display it in Site Visits_1.

I need help to link two fields from two different doctypes

@joelios but with the workaround (product_name field) you have the same informations…why is that not ok for you?

That’s not Ok because if you change the value of the field in the source doctype it won’t change in the the linked doctype unless you go edit that document and resave.
I know it’s an old thread but it’s been 3 years and there’s no real solution for this yet. This could be easily done with a join SQL query, but the list view is a core component of Frappe and I’m not yet very familiar on how it’s built to make changes in the core functionalities.