How to apply query filter on doctype link in web form?

I tried adding the query filter using
frappe.web_form.set_df_property(‘program’, ‘options’, formatted_options);
but this is not working. Does anyone worked on the similar scenario?

Hi @praveeen1214
use the set_query function

Pass the filters you want

frm.set_query(Fieldname, function () {
	return {
		filters: {
			enable: 1  //filters that required
		}
	};
})

If you need custom query function add the path to that function

frm.set_query(Fieldname, function () {
	return {
		query: "path to the query method",
		filters: {
			enable: 1  //filters that required
		}
	};
})

Reference

@Usama_Naveed @Gubbu77
Both scripts works in doctype forms I want to achieve this in webform, Is there any way?

1 Like

Hi @praveeen1214

Reference for apply filter in webform

@Usama_Naveed That helps, Thanks!!
In web forms basically doctype link dropdowns works as static dropdowns, So we need to first fetch filtered values from an API call then set these values using above mentioned function. Below is the complete code that worked for me.

 if (school) {
        // Fetch filtered options based on the pre-filled school
        frappe.call({
            method: "your_function_path",
            args: {
                company: school
            },
            callback: function(response) {
                let program_options = response.message || [];
                
                // Set the options dynamically for the 'program' field
                let formatted_options = program_options.map(option => ({
                    label: option, 
                    value: option
                }));

                frappe.web_form.fields_dict.program.set_data(formatted_options)
            }
        });
    }```
4 Likes