Issue with the Custom Script

Hi Everyone,
Below is the custom script that I have used for BOM I wanted that if an item is not approved (as per custom workflow) it should not allow user to select the same in BOM.

It is working fine, but it works only once. Once it shows error and after that if user selects any other item it does’t get populated with all the details of the item. Even I have tried by putting a message but found that that the entire block works only once.

Any idea on this?

frappe.ui.form.on("BOM", "item", function(frm) {
cur_frm.add_fetch('item','workflow_state','item_workflow_state')
if(frm.doc.workflow_state!="Approved") 
{
msgprint("Test");
cur_frm.set_value("item", "");
cur_frm.set_value("item_name", "");
refresh_field("item");
frappe.throw("You cannot select an item which is not approved!");
}
});

Regards
Ruchin Sharma

@ruchin78

why r u not using filter on item field having criteria workflow_state=“Approved”

Just my view…

@jyotinb
I don’t have any idea about that, can you please help?

Regards
Ruchin Sharma

frappe.ui.form.on(“BOM”, “onload”, function(frm){
cur_frm.set_query(“item”, function(){
return {
“filters”: [
[“workflow_state”, “=”, “Approved”]
]
}
});
});

above code may work

@jyotinb

Thanks a lot it, worked for me.

Regards
Ruchin Sharma

Hi Everyone,

I appreciate if you can tell me the way to put the same filter on Child Table like BOM Item.

I tried it, but it didn’t work:

frappe.ui.form.on("BOM Item", "onload", function(frm){
cur_frm.set_query("item_code", function(){
return {
"filters": [
["workflow_state", "=", "Approved"]
]
}
});
});
``

@ruchin78 , Refer following link, hope it will help you -

Hi @priya_s

Thanks for your support but it didn’t work for me.

frappe.ui.form.on("BOM", "onload", function(frm){
cur_frm.set_query("item_code", "items",  function(cdt, cdn) {
var c_doc= locals[cdt][cdn];

return { 
"filters": ["workflow_state", "=", d_doc.item_code] 
}
});
});

I tried this too

frappe.ui.form.on("BOM", "onload", function(frm){
cur_frm.set_query("item_code", "items",  function(cdt, cdn) {
var c_doc= locals[cdt][cdn];

return { 
"filters":["workflow_state", "=", "Approved"]
}
});
});

I appreciate if you can let me know what I missed here
Regards
Ruchin Sharma

@priya_s

Thanks for your help, by changing some parameters it is working for me.

Below is my code:

frappe.ui.form.on("BOM", "onload", function(frm){
cur_frm.set_query("item", function(){
return {
"filters": [
["workflow_state", "=", "Approved"]
]
}
});

cur_frm.set_query("item_code", "items",  function (doc, cdt, cdn) {
var c_doc= locals[cdt][cdn];

return { 
"filters": [
["Item","workflow_state", "=", "Approved"]
] 
}
});
});

But the issue is when I use the same code in Material Request it doesn’t work. Can anyone tell me the reason for the same?

frappe.ui.form.on("Material Request", "onload", function(frm){

cur_frm.set_query("item_code", "items",  function (doc, cdt, cdn) {
var c_doc= locals[cdt][cdn];

return { 
"filters": [
["Item","workflow_state", "=", "Approved"]
] 
}
});
});

Regards
Ruchin Sharma

Something is strange code is working for Stock Entry, BOM and Quality Inspection whereas the code is not working for Material Request and Purchase Order.

Does anybody have any idea about that below is my working/non-working code.

Stock Entry (Working):

frappe.ui.form.on("Stock Entry", "onload", function(frm){

cur_frm.set_query("item_code", "items",  function (doc, cdt, cdn) {
return { 
"filters": [
["Item","workflow_state", "=", "Approved"]
] 
}
});
});

BOM (Working):

frappe.ui.form.on("BOM", "onload", function(frm){
cur_frm.set_query("item", function(){
return {
"filters": [
["workflow_state", "=", "Approved"]
]
}
});

cur_frm.set_query("item_code", "items",  function (doc, cdt, cdn) {
var c_doc= locals[cdt][cdn];

return { 
"filters": [
["Item","workflow_state", "=", "Approved"]
] 
}
});
});

Quality Inspection (Working):

frappe.ui.form.on("Quality Inspection", "onload", function(frm){
cur_frm.set_query("item_code", function(){
return {
"filters": [
["workflow_state", "=", "Approved"]
]
}
});

});

Purchase Order (Not Working):

frappe.ui.form.on("Purchase Order", "onload", function(frm){

cur_frm.set_query("item_code", "items",  function (doc, cdt, cdn) {
return { 
"filters": [
["Item","workflow_state", "=", "Approved"]
] 
}
});
});

Material Request (Not Working):

frappe.ui.form.on("Material Request", "onload", function(frm){

cur_frm.set_query("item_code", "items",  function (doc, cdt, cdn) {
return { 
"filters": [
["Item","workflow_state", "=", "Approved"]
] 
}
});
});

if(this.frm.fields_dict[“items”].grid.get_field(‘item_code’)) {
this.frm.set_query(“item_code”, “items”, function() {
return {
query: “erpnext.controllers.queries.item_query”,
filters: {‘is_sales_item’: 1}
}
});
}

above code in selling\sales_common.js is executed so your filter not working.
please comment above code in sales_common.js and all works.

Questions for Team members / Developers :

  1. Is this correct way to comment above code?
  2. As i feel if it is modified it gives git error during upgrade, what is best way of doing this type of customization which will not conflict with periodic upgrade?

I hope it is not recommended. There must be an alternate solution for that. I appreciate if someone jump into it and help us.

@ruchin78
please inform me whenver u got solution as i am not happy with this type of patching…

Hi Everyone,

I tried this too but it also didn’t work:

frappe.ui.form.on("Material Request", "onload", function(frm) {
cur_frm.fields_dict['items'].grid.get_field('item_code').get_query = function(doc, cdt, cdn) {
	return {
		filters:[
			['Item', 'workflow_state', '=', 'Approved']
		]
	}
}
});

I appreciate, if someone can put some light on it.

Regards
Ruchin Sharma

@ruchin78, use refresh instead of onload

@max_morais_dmm
Thanks a lot, it worked for me.

@max_morais_dmm Is on fire with answers today… :smile: Thanking You Max.

1 Like

@jyotinb I hope you got the answer.

yes thanks