How to set filter query for dynamic link type

How to set filter query for dynamic link type depending which type is selected on every row?

You can do something like this:

cur_frm.fields_dict.TABLE_NAME.grid.get_field('FIELD_NAME').get_query = function(doc, cdt, cdn) {
	if(locals[cdt][cdn].CONDITION_FIELD_NAME == "VALUE 1"){
		return {
			filters: [
				// YOUR CONDITION FOR VALUE 1
			]
		}
	}
	else if(locals[cdt][cdn].CONDITION_FIELD_NAME == "VALUE 2"){
		return {
			filters: [
				// YOUR CONDITION FOR VALUE 2
			]
		}
	}
	else{
		return {
			filters: [
				// YOUR CONDITION FOR OTHER VALUES
			]
		}
	}
}
1 Like

Thankyou

@Khadija hello khadija can you share you code

I had to apply it on the ‘Topic’ Doctype contents field of education module, so I did this way in the topic.js file

frappe.ui.form.on('Topic', {
refresh: function(frm) {

}
});

cur_frm.fields_dict.topic_content.grid.get_field('content').get_query = function(doc, cdt, cdn) {
if(locals[cdt][cdn].content_type == "Article"){
	return {
		query: "erpnext.erpnext.doctype.topic.topic.get_available_articles",
		filters: {
			'docname': doc.name,
		}
	}
}
else if(locals[cdt][cdn].content_type == "Video"){
	return {
		query: "erpnext.erpnext.doctype.topic.topic.get_available_videos",
		filters: {
			'docname': doc.name,
		}
	}
}
else if(locals[cdt][cdn].content_type == "Quiz"){
	return {
		query: "erpnext.erpnext.doctype.topic.topic.get_available_quiz",
		filters: {
			'docname': doc.name,
		}
	}
}
}

and the query was like this

@frappe.whitelist()
  def get_available_articles(doctype, txt, searchfield, start, page_len, filters):
if filters.get('docname'):
	return frappe.db.sql("""select name, title from `tabArticle` where name not in 
(select content from `tabTopic Content` where content_type = 'Article' and parent = %(docname)s) and (name like %(txt)s {match_cond} or title like %(txt)s {match_cond})
		order by
			if(locate(%(_txt)s, name), locate(%(_txt)s, name), 99999),
			idx desc,
			`tabArticle`.name asc
		limit {start}, {page_len}""".format(
			match_cond=get_match_cond(doctype),
			start=start,
			page_len=page_len), {
				"txt": "%{0}%".format(txt),
				"_txt": txt.replace('%', ''),
				"docname": filters['docname']
			})
1 Like