[solved] Queries regarding custom client side scripting

Hi,

I am trying to run a query on a child table field. The following are the details:

  1. Name of Child Table item_variant_restrictions
  2. Name of parent doctype item
  3. Name of field on which the query is to run attribute

Now the attribute field in the child table should show only those attributes which are:

  1. Attributes which are NON-NUMERIC (where numeric_values == 0)
  2. Attributes which are mentioned inside the table attributes of the Item form.

To accomplish this I have a custom client side script as below:

cur_frm.cscript.custom_onload = function () {
	if (cur_frm.doc.has_variants == 1) {
		cur_frm.set_query('attribute', 'item_variant_restrictions', function(){
			var attrs = []
			cur_frm.doc.attributes.forEach(function(row){
				attrs.push([row.item_variant_restrictions.attribute])
			});
		return {'filters': [['Item Attribute', 'attribute', 'in', attrs]]}
		});
	}
}

I am getting this error in the console

Uncaught TypeError: Cannot read property 'attribute' of undefined(anonymous function) @ VM698:331(anonymous function) @ VM698:330frappe.ui.form.ControlLink.frappe.ui.form.ControlData.extend.set_custom_query @ desk.min.js:7603$input.autocomplete.source @ desk.min.js:7593e.widget._search @ desk.min.js:11(anonymous function) @ desk.min.js:10e.widget.search @ desk.min.js:11(anonymous function) @ desk.min.js:10(anonymous function) @ desk.min.js:10n.extend.each @ jquery.min.js:2n.fn.n.each @ jquery.min.js:2e.widget.bridge.e.fn.(anonymous function) @ desk.min.js:10(anonymous function) @ desk.min.js:7587

Could I get a help on this script.

From the error console, it seems “attribute” in “item_variant_restrictions” is missing. Did you check for typo?

Yes, I did and the name of the field in table is indeed attribute but one thing which comes to my mind is that the name of the table field in Item doctype is item_variant_restrictions and also the name of the custom doctype is item_variant_restrictions

Would that be doing some problem?

I have changed the code as there was a problem with attrs.pus([row.item_variant_restriction,attribute])

So the new code is as below:

cur_frm.cscript.custom_onload = function () {
	if (cur_frm.doc.has_variants == 1) {
		cur_frm.set_query('attribute', 'item_variant_restrictions', function(){
			var attrs = []
			cur_frm.doc.attributes.forEach(function(row){
				attrs.push([row.attribute])
			});
		return {'filters': [['Item Attribute', 'attribute_name', 'in', attrs]]}
		});
	}
}

But this is giving me an error as:

Traceback (innermost last):
  File "/home/aditya/frappe-bench/apps/frappe/frappe/app.py", line 67, in application
    response = frappe.handler.handle()
  File "/home/aditya/frappe-bench/apps/frappe/frappe/handler.py", line 74, in handle
    execute_cmd(cmd)
  File "/home/aditya/frappe-bench/apps/frappe/frappe/handler.py", line 99, in execute_cmd
    ret = frappe.call(method, **frappe.form_dict)
  File "/home/aditya/frappe-bench/apps/frappe/frappe/__init__.py", line 758, in call
    return fn(*args, **newargs)
  File "/home/aditya/frappe-bench/apps/frappe/frappe/desk/search.py", line 13, in search_link
    search_widget(doctype, txt, query, searchfield=searchfield, page_len=page_len, filters=filters)
  File "/home/aditya/frappe-bench/apps/frappe/frappe/desk/search.py", line 88, in search_widget
    as_list=True)
  File "/home/aditya/frappe-bench/apps/frappe/frappe/desk/reportview.py", line 17, in execute
    return DatabaseQuery(doctype).execute(*args, **kwargs)
  File "/home/aditya/frappe-bench/apps/frappe/frappe/model/db_query.py", line 50, in execute
    return self.build_and_run()
  File "/home/aditya/frappe-bench/apps/frappe/frappe/model/db_query.py", line 53, in build_and_run
    args = self.prepare_args()
  File "/home/aditya/frappe-bench/apps/frappe/frappe/model/db_query.py", line 68, in prepare_args
    self.build_conditions()
  File "/home/aditya/frappe-bench/apps/frappe/frappe/model/db_query.py", line 191, in build_conditions
    self.build_filter_conditions(self.filters, self.conditions)
  File "/home/aditya/frappe-bench/apps/frappe/frappe/model/db_query.py", line 219, in build_filter_conditions
    opts = [frappe.db.escape(t.strip()) for t in opts]
 AttributeError: 'list' object has no attribute 'strip'

Nevermind I found the error attrs.pus([row.attribute]) should have been attrs.push(row.attribute)

1 Like