Mandate child record based on a parent field

Hi Everyone,

I wonder if there is any way to make a child table record mandatory based on parent record.
Actually, my requirement is if user ticks is_purchase_item=Yes then user has to enter supplier and supplier part detail.

Regards
Ruchin Sharma

@ruchin78

for d in self.children:
    if d.is_purchase_item and not d.supplier:
        frappe.throw('Supplier required for row {0}'.format(d.idx))

You can try this code on your doctype.js file

cur_frm.cscript.is_purchase_item = function(doc) {
	cur_frm.set_df_property('fieldname_that_you_want_as_mandetory','reqd',doc.is_purchase_item,doc.name,'child_table_field_name')
};

Please replace
“fieldname_that_you_want_as_mandetory” with your field name
and “child_table_field_name” with your child table fild name

Hi @mainul
@max_morais_dmm
I have tried mainul code and it is not working whereas I don’t think the max code’s will work for me.
Actually I want to use it for Item Doctype,
while creating an item if user ticks is_purchase_item=Yes, then user has to put the supplier and supplier part no for that item.

Regards
Ruchin Sharma

@ruchin78, do you need be clear!

When do you mean!

If the user dont will fill the supplier part in that item, where the user will put it?

@max_morais_dmm
I aplogize for the inconvenience.
But, I really didn’t understand what you are trying to say?

Regards
Ruchin Sharma

@ruchin78, LOL, I’m in the same way with your comment!

I dont got what do you mean! Be clear on what do you be trying to do!

When the user mark the option is_purchase_item, where the user will fill the supplier, and supplier part?

Screenshots are wellcome!

Hi @max_morais_dmm
I apologize for the inconvenience caused, below is the screenshot for the same.

Regards
Ruchin Sharma

frappe.ui.form.on('Item', 'validate', function(frm, cdt, cdn){
  if (!frm.doc.supplier_items){
    validated = false;
    frappe.msgprint('Do you need fill Item Code for Suppliers for Purchase Items')
  }
});

@ruchin78

@max_morais_dmm
Thanks a lot it worked for me!