Child Table fetching Custom doc to Custom doc?

Hi Community !!

I have created one doctype called “Authority Approval” there one childtable called “Authority Approval Table” with fieldname “authority_table”. Also i have created one child table called “WPR Authority” in Weekly Progress Report" with fieldname “wpr_authority_table”. In both childtable, i have same fields authority, authority_type, status, remarks. Now, i need a function for, the child table information of Authority Approval doctype should be fetch to Weekly Progress Report Doctype childtable based on project i select in the Weekly Progress Report “project” field. The Authority Approval doctype also having project field.

I attached code below.its not fetching. Suggest me a solution.

Server API method ‘auth’

def fetch_authority_data(project):
    authority_data = []
    
    # Fetch Authority Approval based on the selected project
    approvals = frappe.get_all('Authority Approval', filters={'project': project}, fields=['name'])
    
    for approval in approvals:
        # Fetch related entries from Authority Approval Table
        entries = frappe.get_all('Authority Approval Table', filters={'parent': approval.name},
                                  fields=['authority', 'authority_type', 'status', 'remarks'])
        for entry in entries:
            authority_data.append(entry)
    
    return authority_data

client script:

frappe.ui.form.on('Weekly Progress Report', {
    project: function(frm) {
        if (frm.doc.project) {
            // Clear existing entries in WPR Authority child table
            frm.clear_table('wpr_authority_table');
            frm.refresh_field('wpr_authority_table');

            // Call the server-side function to fetch authority data
            frappe.call({
                method: 'auth', 
                args: {
                    project: frm.doc.project
                },
                callback: function(r) {
                    if (r.message) {
                        r.message.forEach(entry => {
                            // Add each entry to the WPR Authority child table
                            const new_entry = frm.add_child('wpr_authority_table');
                            new_entry.authority = entry.authority;
                            new_entry.authority_type = entry.authority_type;
                            new_entry.status = entry.status;
                            new_entry.remarks = entry.remarks;
                        });
                        frm.refresh_field('wpr_authority_table'); // Refresh the child table to show new entries
                    }
                }
            });
        } else {
            // Clear the child table if no project is selected
            frm.clear_table('wpr_authority_table');
            frm.refresh_field('wpr_authority_table');
        }
    }
});

In my opinion, you are calling the wrong method; you should call the fetch_authority_data method.

Not working.

Server Side Code:

Client Side Code:

frappe.ui.form.on('Weekly Progress Report', {
    project: function(frm) {
        if (frm.doc.project) {
            // Clear existing entries in WPR Authority child table
            frm.clear_table('wpr_authority_table');
            frm.refresh_field('wpr_authority_table');

            // Call the server-side function to fetch authority data
            frappe.call({
                method: 'fetch_authority_data', 
                args: {
                    project: frm.doc.project
                },
                callback: function(r) {
                    if (r.message) {
                        r.message.forEach(entry => {
                            // Add each entry to the WPR Authority child table
                            const new_entry = frm.add_child('wpr_authority_table');
                            new_entry.authority = entry.authority;
                            new_entry.authority_type = entry.authority_type;
                            new_entry.status = entry.status;
                            new_entry.remarks = entry.remarks;
                        });
                        frm.refresh_field('wpr_authority_table'); // Refresh the child table to show new entries
                    }
                }
            });
        } else {
            // Clear the child table if no project is selected
            frm.clear_table('wpr_authority_table');
            frm.refresh_field('wpr_authority_table');
        }
    }
});

Here no need to define the function (def fetch_authority_data(project)) is not required, you can directly add the code.
If you want access a parameter through api use: frappe.form_dict.message
To Return the data frappe.response[‘message’] = “ok”

eg:

# respond to API

if frappe.form_dict.message == "ping":
	frappe.response['message'] = "pong"
else:
	frappe.response['message'] = "ok"

like this…

frappe.ui.form.on('Weekly Progress Report', {
    project: function(frm) {
        if (frm.doc.project) {
            // Clear existing entries in WPR Authority child table
            frm.clear_table('wpr_authority_table');
            frm.refresh_field('wpr_authority_table');

            // Call the server-side function to fetch authority data
            frappe.call({
                method: 'fetch_authority_data', 
                args: {
                    project: frm.doc.project
                },
                callback: function(r) {
                    if (r.message) {
                        r.message.forEach(entry => {
                            // Add each entry to the WPR Authority child table
                            const new_entry = frm.add_child('wpr_authority_table');
                            new_entry.authority = entry.authority;
                            new_entry.authority_type = entry.authority_type;
                            new_entry.status = entry.status;
                            new_entry.remarks = entry.remarks;
                        });
                        frm.refresh_field('wpr_authority_table'); // Refresh the child table to show new entries
                    }
                }
            });
        } else {
            // Clear the child table if no project is selected
            frm.clear_table('wpr_authority_table');
            frm.refresh_field('wpr_authority_table');
        }
    }
});

but still not fetching.

@NCP brother can you suggest me a solution.