Client script grabbing data from wrong child table entry

I have a script running on a doctype, that pulls data from a child table, and uses it to populate a select field in a second, separate child table.

In practice, this script appears to fail to run the first time an entry from the doctype is accessed, though it does not seem to raise any errors. However, when a second entry from the doctype is accessed, the script then populates the target select field with data from the previously accessed entry.

Here is a video of this occurring: 2024-07-10_11-07-50.mp4

Here is the client script that I am using:

frappe.ui.form.on("jobnum", {
    onload_post_render(frm){
	frm.set_df_property("po","options",frm.doc.phase.map((row) => row.data_rcui),"jobnum","phase");
    }
});

These are the versions that I am using:

ERPNext: v15.27.0 (version-15)
Frappe Framework: v15.29.1 (version-15)
Frappe HR: v15.22.0 (version-15)

As a side note, the script seems to work fine on my account, but malfunctions as described above on any other accounts I create, regardless of what permissions are set.

Did I mess something up in the client script? Is this a bug in Frappe or ERPNext, and if so, how can I work around it? Any help at all would be appreciated.

I think, your script isn’t working properly because it’s running too early, before all the data is fully loaded. Instead of using onload_post_render, try using the refresh event, which ensures everything is loaded.

Try it.

frappe.ui.form.on('jobnum', {
    refresh: function(frm) {
        if (frm.doc.phase && frm.doc.phase.length) {
            const options = frm.doc.phase.map(row => row.data_rcui);
            frm.fields_dict['second_child_table_name'].grid.update_docfield_property('po', 'options', options);
        }
    }
});

Changing the trigger to refresh had no effect.

Please check it.

As stated previously, setting the trigger to refresh had no effect.
However, switching the function for setting the options from set_df_property to update_docfield_property appears to have resolved the issue.