Display different field in link field value other than name as primary

For example:

I changed the autoname function of the “Batch” DocType. So it isn’t the default batch_id. But my title_field is still the batch_id which can read something else than the name of the Batch.

Now when linking with a “Link Field” to a doc of DocType “Batch” I get suggestions and all with the doc’s name in the database and not the title_field batch_id.

As seen in this screenshot on the left is the column for batch_id and on the right is the actual name in the database.


I know that when I customize the “Batch” form I can add more search_fields but that’s not what I want. I want the text that is written bold here to be the batch_id and not the name. For the dropdown list and the “Link Field” itself.

Is that something that is currently achievable? In terms of adding code to my custom app?
Because I guess this is something that would have to be changed withing frappe. If so and if I would implement this and send a Pull Request with this, would that be something that will be accepted? If not, then I guess I have to find another way around this.

2 Likes

Try adding the title field in Search Fields via Customize Form (so it shows up in the autosuggest)

You can also write a formatter for Batch erpnext/utils.js at develop · frappe/erpnext · GitHub

Hi!

Thanks @rmehta I didn’t know about think feature!

The only thing that is a little bit annoying with the link_formatters is that you can not make reference to fields of the selected document, only to fields present in the current document. This force me to duplicate content or make a REST call for each displayed field.

I know there is no easy solution to this problem but it would be such a great step forward :wink:

Regards

PD: If you face the same problem when selecting the doc in a Link field you could try using GitHub - paurosello/linktitle: Frappe Link field override

Thanks @rmehta and @Pau_Rosello_Van_Scho!

I will try that! But it seems I can not alter the text that’s inside the <\strong> tag in the dropdown list, because the link_formatter just changes the selected field and not all.

Is there a filter for that?

I have not been able to solve that apart from showing more fields using the “Search Fields”.

I just found that when you manipulate the “Link Field”'s query with cur_frm.fields_dict.batch_no.get_query for example and then in your server-side function do return frappe.db.sql('select batch_id from tabBatch') it’s kind of working.
Because the first value after the “select” is always the one in the list written bold.

The only thing that’s not working is that when you do it like this and click the item in the list it isn’t set. I guess it’s the same like this:

I’m trying to understand this and write a function that uses get_query and link_formatters to achieve what I described in my first post. And to have it easily reusable for other fields.

I appreciate any additional information that can help me with this.

Continuing from this post because it is relevant and I just realized it was posted a year ago.

So I just tried getting this to work by altering the validation of the “Link Field” so it accepts the first item in df.search_fields but then the link from the “Link Field” points to a non-existing doc.

So I’m proposing now that the whole functioning of a “Link Field” needs to be rewritten to have better control as a developer over what exactly is displayed in the dropdown list and in the selected input field.

Go to customize form on doctype in linked field. On customize form you will get option of “Search Fields”, mention there the field you want to see in linked field Dropdown.

1 Like

Thanks but that’s not what I mean. In the dropdown there’s the “name” in black and bold on top and beneath are the additional “Search Fields” as you suggest. But I want to change the text that’s in black and bold to be the “batch_id” field like in my example.

@codeag use GitHub - techmaxsolucoes/title_links: Frappe Hack: Links using related title, instead of name, it will change the Link behavior for what you want

1 Like

I can not test if this is what I need because it seems the app isn’t compatible with the recent version of frappe/erpnext. The app throws an error on my desk with:

desk.min.js:15 Uncaught TypeError: callback is not a function(…)

Chrome DevTools formatted:

desk.min.js:formatted:122 Uncaught TypeError: callback is not a function(…)

@codeag this is because now, frappe.require is async!

I believe is a simple patch!

I just commented out frappe.require('assets/title_links/templates/list_item_main.html'); and now the js error is gone. Everything seems to work, at least for my custom fields where there’s just the default link dropdown in use.
For the ERPNext DocTypes like “Quotation” the dropdown list still isn’t showing the doc’s title.
But on selecting, the title is inserted into the input field. So this is working.

Could you provide any insights on this?

1 Like