How to use "frm.set_df_property" in child table?

I have a child table that looks like this :

{
       :     :     :
 "fields": [
  {
   "fieldname": "naming_series",
   "fieldtype": "Select",
   "label": " Naming Series",
   "options": "RTN-MOV-.########"
  },
       :     :     :
  {
   "allow_in_quick_entry": 1,
   "fieldname": "from_stock",
   "fieldtype": "Link",
   "in_list_view": 1,
   "label": "From Stock",
   "options": "Warehouse",
   "reqd": 1
  },
       :     :     :
  {
   "allow_in_quick_entry": 1,
   "fieldname": "direction",
   "fieldtype": "Select",
   "in_list_view": 1,
   "label": "Direction",
   "options": "Stock --> Cust\nCust --> Stock\nStock --> Stock\nCust --> Cust"
  },


In the parent form I have:

frappe.ui.form.on("My Child", {
  direction: (frm, cdt, cdn) => {
    const d = locals[cdt][cdn];
    frm.set_df_property('from_stock', 'reqd', 0);
    frm.set_value('from_stock', warehouse);
    d.set_value('from_stock', warehouse);
  }
});

If I select a new option in direction the handler is invoked.

My goal is to set/unset the ‘reqd’ attribute of ‘from_stock’, but I cannot see how to get the object in order to manipulate it’s attribute values.

The line…

    frm.set_value('from_stock', warehouse);

… throws an error “Field from_stock not found.”

The line…

    d.set_value('from_stock', warehouse);

… throws an error “TypeError: d.set_value is not a function.”

My questions are:

  1. How can I alter the ‘reqd’ attribute of the ‘from_stock’ field of the child table?

  2. More generally, How do I get the child form in order to manipulate its attributes?

@MartinHBramwell This may help you

Hey @MartinHBramwell

Hi,

Try following Custom Script

 cur_frm.fields_dict.child_table_name.grid.toggle_reqd
    ("child_table_fieldname", condition

to make field required.

for setting a value in a child table you can use

cur_frm.doc.child_table_name.child_table_fieldname = value

You could try this…
It worked for me.

frappe.model.set_df_property("from_stock", "reqd", 1);

How does it know which row in the child table should be altered?

@ROHAN_JAIN1, @abrarpv97, @Francisco_Buendia

I am grateful for your various suggestions and desire to help out.

I am finding that set_df_property does work, but it sets it for all rows in the table, not individual rows.

Would you know if that is expected behaviour, or am I doing it wrong?

It’s beginning to look as though I have to turn off mandatory for every attribute where it is conditional. and use a validation routine to block saving the record.

Adapt it for your needs, obviously, but using both, works for individual row…

frappe.ui.form.on('Material Request Item', {
	category_item: function(frm, cdt, cdn) {
		const row = locals[cdt][cdn];
		var current_row = frm.fields_dict["items"].grid.grid_rows_by_docname[row.name];
		current_row.toggle_reqd("employee", (row.category_item === "Accessories"));
	},
});

cur_frm.cscript.validate = function(doc, cdt, cdn) {
	if(doc.with_items){
		$.each(doc["items"] || [], function(i, item) {
			if (item.category_item === "Accessories" && !item.employee){
				frappe.msgprint(__("Row#{0} Please enter employee.", [item.idx]));
				frappe.validated = false;
			}
		});
	}
};
4 Likes