Grid row toggle_enable based on a condition doesn't work as expected

Hi Everyone,

This is my first post and do kindly excuse me for any mistakes.

I have been struggling to find a solution for this, really appreciate if you could help me please. Basically i am trying to enable/disable editing two fields namely invoice_quantity and invoice_amount in grid table based on a parent field value(Select field with options as “Item Based”/“Milestone Based”/“Piece Rate Work Based(PRW)”) as mentioned below. But the problem is it works for the first time, when i select another option from the parent select field, same settings of previous selection are still applied instead of getting overwritten by the new settings. And the strange thing is new changes are applied inside when i click on grid table’s edit(arrow mark on the right) but the same not applied on grid row.

For example, if i select parent field option “Item Based” then invoice_amount in grid table should not be editable for the end user, when i select parent field option “Milestone Based” then invoice_quantity in grid table should not be editable. Below is the code which i am calling from onload and refresh events of the parent doctype.

var toggle_items_based_on_boq_type = function(frm){
		frm.fields_dict.project_invoice_boq.grid.toggle_enable("invoice_quantity", false);
		frm.fields_dict.project_invoice_boq.grid.toggle_enable("invoice_amount", false);
		
		if(frm.doc.boq_type=="Item Based"){
			frm.fields_dict.project_invoice_boq.grid.toggle_enable("invoice_quantity", true);
			//frm.fields_dict.project_invoice_boq.grid.set_column_disp("invoice_quantity", true);			
		} 
		else if(frm.doc.boq_type=="Milestone Based"){
			frm.fields_dict.project_invoice_boq.grid.toggle_enable("invoice_amount", true);
		}
		else if(frm.doc.boq_type=="Piece Rate Work Based(PRW)"){
			frm.fields_dict.project_invoice_boq.grid.toggle_enable("invoice_quantity", true);
		}
		
		refresh_field("project_invoice_boq");
}

Thank you for your time

Where is your call to toggle_items_based_on_boq_type ?

you can check example from asset.js for it.

https://github.com/frappe/erpnext/blob/develop/erpnext/accounts/doctype/asset/asset.js#L154-L159

Hi @Sangram,

Thank you for the reply.

I am calling toggle_items_based_on_boq_type from onload and refresh events.

Check given example and try accordingly.

Hi @Sangram,

Thank you for the quick response.

Yes i am trying the solution you just suggested, i shall update you once done.

Hi @Sangram,

Sorry for the delay.

I tried as you suggested but still is same issue, find below the code i tried.

var toggle_items_based_on_boq_type = function(frm){

		frm.toggle_enable("project_invoice_boq", true);
		
		frm.fields_dict["project_invoice_boq"].grid.toggle_enable("invoice_quantity", false);
		frm.fields_dict["project_invoice_boq"].grid.toggle_enable("invoice_amount", false);
		
		if(frm.doc.boq_type=="Item Based"){
			console.log("Item Based");
			frm.fields_dict["project_invoice_boq"].grid.toggle_enable("invoice_quantity", true);
		} 
		else if(frm.doc.boq_type=="Milestone Based"){
			console.log("Milestone Based");
			frm.fields_dict["project_invoice_boq"].grid.toggle_enable("invoice_amount", true);
		}
		else if(frm.doc.boq_type=="Piece Rate Work Based(PRW)"){
			console.log("Piece Rate Work Based(PRW)");
			frm.fields_dict["project_invoice_boq"].grid.toggle_enable("invoice_quantity", true);
		}
		
		frm.refresh_field("project_invoice_boq");
}

Only difference with what you suggested is from where i call the function toggle_items_based_on_boq_type, instead of calling the function on option change for frm.doc.boq_type i am calling on refresh and onload, i have to do this because the value is actually coming from a mapped document using get_mapped_doc.

try this,

toggle_items_based_on_boq_type: function(frm) {
	var invoice_amount_editable = frm.doc.boq_type==="Milestone Based" ? true : false;
	var invoice_quantity_editable = in_list(["Item Based", 
		"Piece Rate Work Based(PRW)"], frm.doc.boq_type) ? true : false
	
	frm.fields_dict["project_invoice_boq"].grid.toggle_enable("invoice_quantity",
		invoice_quantity_editable);
	frm.fields_dict["project_invoice_boq"].grid.toggle_enable("invoice_amount",
		invoice_amount_editable);
}

Add console to check invoice_amount_editable, invoice_quantity_editable values.

Hi @Sangram,

Same issue still persists, and i get the proper values for invoice_amount_editable, invoice_quantity_editable though.

localStorage cleared
desk.min.js:9 Cleared App Cache.
desk.min.js:12 localStorage cleared
VM911:16 setup
VM911:21 onload
VM911:183 frm.doc.boq_type: Item Based
VM911:184 invoice_amount_editable: false
VM911:185 invoice_quantity_editable: true
VM911:186 ============================
VM911:33 refresh
VM911:21 onload
VM911:183 frm.doc.boq_type: Milestone Based
VM911:184 invoice_amount_editable: true
VM911:185 invoice_quantity_editable: false
VM911:186 ============================
VM911:33 refresh

What issue ?

Hi @Sangram,

First i tried boq_type as “Item Based”, and it worked fine i.e., invoice_quantity field is enabled and invoice_amount field is disabled as desired. And the variable values are

invoice_quantity_editable: true
invoice_amount_editable: false

And for the second try, i tried boq_type as “Milestone Based” and here comes the problem, invoice_quantity field is still enabled instead of disabled and invoice_amount field is still disabled instead of getting enabled. But when i edit the row, the changes are taken effect inside “Edit row” i.e., invoice_quantity is disabled and invoice_amount is enabled. And the variable values are

invoice_quantity_editable: false
invoice_amount_editable: true

Call this function on change of boq_type. I tried on my side, working fine.

Hi @Sangram,

I tried calling the function on change of boq_type too, but it never gets called as boq_type is a readonly field in this doctype(“Project Invoice”) and gets the values populated from master doctype “BOQ” via get_mapped_doc.

Just uploaded this video(without audio) for your understanding - YouTube