Filter a field based on another field ( like, filter employees based on department )

This query always comes up, how to filter one field based on another one. Like, you select Department first, then on the next field you want to display only employees in that department .

This example is on Issue doctype. There is a field for Department and another one for technicians ( technician field is a link field with Employee as option )

frappe.ui.form.on('Issue', {
    // The 'setup' event runs when the form is being set up.
    setup: function(frm) {
        // Sets a custom query for the 'technician' field.
        frm.set_query('technician', function() {
            // Checks if the 'department' field has a value.
            if (frm.doc.department) {
                // Returns the filter conditions for the query.
                return {
                    filters: {
                        'department': frm.doc.department // Filters the 'technician' field by the selected 'department'.

    // The 'department' event runs when the 'department' field is changed.
    department: function(frm) {
        // Clears the value of the 'technician' field when the 'department' field is changed.
        frm.set_value('technician', '');