Client Script Value as Filter

Dear Friends,

I work with a lot of files. These files are attached to different doctypes, such as Patient Encounter, Patient and a bunch of others from the healthcare module. It is easy to lose sight of all the files I have on hand for a specific patient. For this reason, I customized the File manager to have an additional field called “Patient2”. My files now look like this:

For the attached file (a screenshot in this case), the field Patient2 uses a client script that looks up the patient_name in the document HLC-SPT-00004 and displays its name in the document. This works well. However, when I want to filter based on Patient2, I always get “Not set”:

I’m assuming that the reason for this behaviour that the javascript is executed too late? (i.e. only when opening the file page and not at the stage of the file list).

Is it possible to get this working nonetheless?

Thank you in advance for your help.

Here the client script I used:

frappe.ui.form.on('File', {
    onload: function(frm) {
        let fileId = frm.doc.attached_to_name;
        let fileDoctype = frm.doc.attached_to_doctype;{
            method: 'frappe.client.get_value',
            args: {
                doctype: fileDoctype,
                filters: { name: fileId },
                fieldname: ['patient_name'] // Replace 'patient_name' with the actual fieldname in the target doctype.
            callback: function(response) {
                if (response.message) {
                    frm.set_value('patient_2', response.message.patient_name);