How to access value of a field in Item and make a field in Purchase Receipt Mandatory?

Hello,

I have added a field to Item called Is Brightbar (is_brightbar). This is of type Check.

I have added a field to Purchase Receipt Item called Bar Thickness (bar_thickness). This is of type Float.

Now when an item is received in Purchase Receipt (Item) I want to check that if the Check box Is Brightbar of the selected Item is checked then the field Bar Thickness should become mandatory.

How to set this up in Purchase Receipt?

TIA

Yogi Yang

@YogiYang You can do this by editing Bar Thickness field and adding is_brightbar in the Permissions > Depends on field, as shown in the image below.

Hello,

Thanks for the suggestion.

I tried as per your suggestion but it does not work for me.

EPRNext does not show message that Bar Thickness is required as the time of Submit.

Any other suggestion.

TIA

Yogi Yang

@YogiYang Have you tried setting the Bar Thickness field as required after adding is_brightbar in the Permissions > Depends on field?
And if you want the field to be editable before submit, check the option Permissions > Allow on Submit.
I know how to do this in code but I haven’t tried doing it in form.

Hello,

I have set this at Form level but nothing is happening.

Can you guide me as to how to do this in code. I mean which file should be edited, and where it is located, etc.?

TIA

Yogi Yang

Hi,
I think this problem can be fix by using client script in few ways.

  1. check field property
    here is doc for how to change field property but I don’t how to change field property to child table.
    Client Script (frappeframework.com)

  2. By showing mandatory message using frappe.throw in client script as show below.
    but to make code easy to check you need to add field Is Brightbar in Purchase Receipt Item doctype and auto fetch value from Item.

frappe.ui.form.on('Purchase Receipt', 'validate', function(frm) {
    $.each(frm.doc.items,  function(i,  d) {
        if(d.is_brightbar==1 && d.bar_thickness==0){
            frappe.throw("Bar thickness is mandatory");
        }   
    });
});

I hope this will solve your problem.

Thank,

1 Like

@YogiYang In the json file of the doctype, in the Bar Thickness field you can add this:

"depends_on": "eval: doc.is_brightbar == 1",
"mandatory_depends_on": "eval: doc.is_brightbar == 1",
"reqd": 1

Or you can do as @Pheakdey_Tes1 suggested.
As for the first suggestion of @Pheakdey_Tes1, it can be done but it’s going to change the property for the whole table.

Or based on the idea of @Pheakdey_Tes1, you can create a script for the child table only.

function checkBarThickness(frm) {
    $.each(frm.doc.items, function(i, d) {
        if (d.is_brightbar == 1 && d.bar_thickness == 0) {
            frappe.throw("The bar thickness field in row no. (" + i + ") is mandatory.");
        }   
    });
}
frappe.ui.form.on(‘Purchase Receipt’, 'validate', checkBarThickness);
// child table
frappe.ui.form.on(‘Items’, 'items_add', checkBarThickness);

Sorry I’m unable to test the code myself.
I hope that I was helpful.

1 Like