Filter link field base on other field

I am a little bit loss with this use case. There is a “transaction type” field which is select with an option ( rent and vacant ) and there is a field “room” which is a link to a room doc type. If the transaction type is Rent the room field drop down will be filtered ( room_status = ‘Vacant’). if the transaction type “Vacate” the room field will only display with room_status=‘Rented’. How can I do this in frappe?

I have solve this problem by creating a javascript

frappe.ui.form.on("Rental Transaction", "onload", function(frm) {
    cur_frm.set_query("room", function() {
        return {
            "filters": {
                "status": ((frm.doc.transaction_type == "Rent") ? 'Vacant' : 'Occupied')