How to restrict Leave Type to particular Department in Leave Application form?

Hi there!

I created a new Custom Field named Department and added it to Leave Type. I linked this field to the Department DocType in options. Now I can select all the departments that I have created.

I created a new Leave Type named Marriage, chose the Department as Software Development, and saved it.

I created a new Employee and chose her Department as Software Development. When I tried to complete a new Leave Application for this employee, her department field was auto-filled. But I’m unable to choose the Leave Type that I created earlier.

A few hours later, I discovered that the employee should be added to the Leave Allocation list as new leaves should be allocated for her. In this Leave Allocation form, I chose the employee’s name and her Department was auto-filled. Under Leave Type, I’m able to select all the leave types. But I want to select only the Leave Types available for that particular Department.

How do I restrict the Leave Types to particular Departments in the Leave Allocation form?

Should I write a custom script for the client side? Can somebody guide me on this?

Hi:

You can restrict query with a custom script like this:

frappe.ui.form.on("Leave application", "onload", function(frm) {
    frm.set_query("leave_type", function() {
        return {
            "filters": {
                "department": frm.doc.department
            }
        };
    });
});

Note that field names can be different …
Hope this helps.

Thanks!

I need to modify this script a bit. The form name should be Leave Allocation and not Leave Application. The function here works onload. But the department field won’t populate unless you choose an employee. So what should be the operation here instead of on load?

Try this

frappe.ui.form.on("Leave allocation", "employee", function(frm) {
    frm.set_query("leave_type", function() {
        return {
            "filters": {
                "department": frm.doc.department
            }
        };
    });
});

Query be established after any employee change.
Hope this helps.

Tried this. But it didn’t work well. The Leave Type field still shows all the leave types available. It should instead only show the leave types available for that particular department which is a custom field I introduced to Leave Type DocType.

It’s working on my side … probably fieldnames don’t matches.

What’s the internal fieldname of your custom “Department” field on Leave Type doctype? If you are working with develop version maybe it’s named as “custom_department”, so script would be:

frappe.ui.form.on("Leave allocation", "employee", function(frm) {
    frm.set_query("leave_type", function() {
        return {
            "filters": {
                "custom_department": frm.doc.department
            }
        };
    });
});

I am sorry it didn’t work for me. Am I missing something? I reloaded the page and cleared the cache before testing it out.

I have developer mode enabled. The internal field name for the custom Department field in the Leave Type DocType is department. Where can I verify this?

Hi:

Edit your doctype and look at fields list.

Other way … press Alt and hover your mouse pointer over the field. You will see the internal field name.

Thanks a lot! It worked now. My simple mistake was leaving the DocType name as “Leave allocation”. I renamed it to the proper name “Leave Allocation”. It took a while to figure out the reason, but it ultimately helped.

1 Like