Issues with conditional statement in custom PO form

I have a custom field of type Select in the Production Order form with two options. Depending on what I select, I can use a specific group of items to manufacture.
The problem is, how can I manage this? I try to use a conditional statement in the main JS file like this, but it does not work:

setup: function (
        if (frm.doc.item_type === "Type 1") {
                frm.set_query("production_item", function () {
                         return {
                                 query: "erpnext.controllers.queries.item_query",
                                 filters: {
                                         'is_stock_item': 1,
                                         'item_group': 'Group 1'
                                 }
                         }
                });
        } else if (frm.doc.item_type === "Type 2") {
                 frm.set_query("production_item", function () {
                         return {
                                 query: "erpnext.controllers.queries.item_query",
                                 filters: {
                                         'is_stock_item': 1,
                                         'item_group': 'Group 2'
                                 }
                         }
                 });
        }
}

The code should look something like below (not tested):

frappe.ui.form.on("Production Order", {
	setup: function(frm) {
		var item_group = "";
		if (frm.doc.item_type=="Type 1") {
			item_group = "Group 1";
		} else if (frm.doc.item_type=="Type 2") {
			item_group = "Group 2";
		}
		frm.set_query("production_item", function () {
			return {
				query: "erpnext.controllers.queries.item_query",
					filters: {
						'is_stock_item': 1,
						'item_group': item_group
					}
				}
		});
	}
})

And yes, don’t put the code in js file. Instead use “Custom Script” record inside the app.

Thanks @nabinhait for your response , but it doesn’t work in both JS file and Custom Script.

The idea is:
I have two groups of items: Raw material and Compounds.
When I make a Production Order, I have to select a type of item and, depending on what I selected, in the production_item field you only see the items that are in this group.

Above code should work. Is there any error in browser console?

After many tests, the situation is the same. I tried it in Custom Script and in the JS file and nothing. And the browser console has no errors.

I don’t know where the error may be.

Try to use onload instead of setup function.

Please put some logs to see if the function is being called.

What is the advantage / difference in putting the js script in a file vs the custom script area?

If you put the script inside the file (under erpnext app), then it can give conflict when update.

I tried to use onload function in both JS file and Custom Script and it still does not work.

It’s “funny” that, although I’m not using any filter and I do not see a filter in JS / PY files, the production_item field to select the item to be manufactured is being filtered by an item group by default, an item group that I created.

@schilgod, exactly, where can I view the logs that refer to the loading of that function?

try to put console.log(); in the function

I putted console.log() in the function and nothing happens. :disappointed_relieved:

please show your code…

print something in console, console.log(‘testing’);

@nabinhait code should work as it is,

Reload and try?

I put console.log(‘testing’); and only this is returned when I click on New button to create a Production Order:

 SyntaxError: Invalid or unexpected token
    at Class.setup (http://192.168.1.54:8080/assets/js/form.min.js?ver=1532940019.0:2693:18)
    at _f.Frm.setup (http://192.168.1.54:8080/assets/js/form.min.js?ver=1532940019.0:172:22)
    at _f.Frm.refresh (http://192.168.1.54:8080/assets/js/form.min.js?ver=1532940019.0:446:9)
    at Class.load (http://192.168.1.54:8080/assets/js/form.min.js?ver=1532940019.0:87:33)
    at http://192.168.1.54:8080/assets/js/form.min.js?ver=1532940019.0:82:7
    at Object.with_doc (http://192.168.1.54:8080/assets/js/desk.min.js?ver=1532940019.0:5505:4)
    at Class.show_doc (http://192.168.1.54:8080/assets/js/form.min.js?ver=1532940019.0:65:16)
    at http://192.168.1.54:8080/assets/js/form.min.js?ver=1532940019.0:24:8
    at Object.with_doctype (http://192.168.1.54:8080/assets/js/desk.min.js?ver=1532940019.0:5444:17)
    at Class.make (http://192.168.1.54:8080/assets/js/form.min.js?ver=1532940019.0:20:17)

very strange, the code should work as it is, unless you made some change to it.
Error seems something related to quotes

The only changes I made to the code were to change Type 1, Type 2, Group 1 and Group 2 by their real names, and I am sure there are no errors with types and groups.

in that case check quotes…

suggest you copy&paste your code to http://jshint.com, check the warning on the right hand side.

The code is the following:

frappe.ui.form.on("production_order", {
    setup: function (frm) {
        console.log('testing');

        var item_group = "";

        if (frm.doc.item_type == "Compund") {
	        item_group = "Compounds";
        } else
        if (frm.doc.item_type == "Raw material") {
	        item_group = "Raw material";
        }

        frm.set_value({
          "wip_warehouse": "WIP Warehouse",
          "fg_warehouse": item_group
        });

        frm.set_query("production_item", function () {
          return {
            query: "erpnext.controllers.queries.item_query",
            filters: {
              'is_stock_item': 1,
              'item_group': item_group
            }
          };
        });
      }
    });

And report by JSHint is the following:

Metrics
    There are 2 functions in this file.
    Function with the largest signature take 1 arguments, while the median is 0.5.
    Largest function has 7 statements in it, while the median is 4.
    The most complex function has a cyclomatic complexity value of 3 while the median is 2.

One undefined variable
    12	frappe