How to Filter a list based on selection of another list item

I have created a master Brand Group in which two fields are there one is a linked of Brand Master and second one is a data field which denotes the name of Brand Group Master.

I need to customize the Item Form where I want Brand Group list should display the filtered data. First I will select Brand then on the basis of Brand selection, Brand Group drop down should show only those data which are belong to the selected Brand.

Issue is my Brand Group list is showing all data. I have added both fields Brand and Brand Group. Both fields are created as Link type field. Please suggest.

i think this should help you
https://kb.erpnext.com/kb/customize/managing-dynamic-link-fields

I tried by adding link field with options as doctype as suggested in Managing Dynamic Link Fields article. But it is showing me drop down of all document type. whereas I need a list of Brand Master, and on the selection of Brand Master i need to filter our Brand Group Design List.

Try using the following code

cur_frm.fields_dict[‘brand_group’].get_query = function(doc) {
return{
filters:[
[‘brand’, ‘=’, doc.brand]
]
}

3 Likes

Thank you for your help neilLasrado. Your solution has been worked for me. Just a comment - I just need to add missing closing parenthesis } at the end of the code.

@Navneet_Gupta Ma Bad… Discuss needs a syntax checker. Anyways good you figured it out.

Thank you very much

1 Like

Hi, this link is broken
Frappe Cloud

Someone can provide the examples or codes please.

1 Like

https://erpnext.com/docs/user/manual/en/customize-erpnext/articles/managing-dynamic-link-fields

this link is not opening. I have a similar challenge

Hi,

Check this: Dynamic Link Fields

HI There I’m finiding this after 4 years:)
I have a request pls.
I’m using erpnext v12, and facing same issue.
Am dumb, I admit, but pls pls pls, where should I add this code.
I tried to use it in custom scripts for my doctype,
I have the following structure: books, chapter and titles.
For every book I have many chapters. I want the same thing. List all chapter for a book, when s book is selected.
I have a doc type for books, one for chapters linked with books, and a thirst for titles, where I need to select a book (link field), then a chapter for the selected book, and a data field for title.
The issue is exactly the same… but can’t figure it out where should I add the script to make this work?
In custom scripts is not having any result. This is the code I used
frappe.ui.form.on(‘Titles’, {
refresh: function(frm) {
// your code here
cur_frm.fields_dict[‘chapter_title’].get_query = function(doc) {
return{
filters:[
[‘select_book’,‘=’, doc.select_book]
]
}
}
}
})

I also tried to add a titles.js to the server, under titles doctypes folder.
…but same result. The Chapter (chapter_title) field from the Titles DocType is not populated at all. and it should be a list.

Pls, just need some quidance
TIA

Ok, after lot’s pf coffee’s and lot of testing times. I’ve figure it out.
Will posts the missing details here to all of the following ppls who are looking for this solution.
The code it’s working indeed.
What has not beeing said, and it’s probably normal, but me being at the begining of all things, I didn’t knew, is:

  • KEEP THE BRAND GROUP, AS LINK FIELD ALSO AND LINK IT TO THE BRAND GROUPS
    The script mentioned here will act as a filter for this particulary field.

Thank you
Hope that will help someone after me.

I have same challenge too

read above your message

Hi @DenisaElena
So you’re not using the Dynamic Link Field?
What confuse me is the doctype data structure (I’m not a programmer).
What fields do the chapter and title have?

I imagine the table are like these:
DocType Book:

book_name
Book ABC
Book DEF

DocType Chapter:

book_name chapter_name
Book ABC Chapter 1
Book ABC Chapter 2
Book DEF Chapter 1
Book DEF Chapter 2

DocType Title:

book_name chapter_name title_name
Book ABC Chapter 1 Title abc1a
Book ABC Chapter 1 Title abc1b
Book ABC Chapter 1 Title abc1c
Book ABC Chapter 2 Title abc2a
Book ABC Chapter 2 Title abc2b
Book ABC Chapter 2 Title abc2c
Book DEF Chapter 1 Title def1a
Book DEF Chapter 1 Title def1b
Book DEF Chapter 1 Title def1c
Book DEF Chapter 2 Title def2a
Book DEF Chapter 2 Title def2b
Book DEF Chapter 2 Title def2c

What I try to do is a bit different but I want to understand your solution first.
What I do is to put the Books as parent doctype, and Chapter as child table in Books doctype (yes it is only 2 level structure).
So I want to have (in a webform) a filtered Chapter field (select field) after the field Books is selected (user can only select 1 Book and 1 Chapter in that book).

Thank in advance for any suggestions and input.