Fetching data from other child table is it possible?

Hi,

Can somebody help me please, this is the scenario, I have a parent doctype PNPA Sales and it has a child table Lot Payment Reference. Now I created a new doctype Commission Entry for the purpose of generating commissions coming from the transactions in Lot Payment Reference. This parent doctype Commission Entry has a child table called Commission Transaction, now I created a custom button “Generate Commission” so that when clicked it is supposed to loop in other child table “Lot Payment Reference” and fetch data from it and while looping creating a new record in the child table “Commission Transaction”. My question is, is it possible to fetch data from other child table or there will be problems in permissions? I asked chatGPT and this is the code it gave me but its not working:
frappe.ui.form.on(‘Commission Entry’, {
refresh: function(frm) {
if (frm.doc.docstatus === 1) { // Check if document status is “Submitted”
frm.add_custom_button(‘Generate Commission’, function() {

    // Prompt the user to enter the posting date using the calendar
    frappe.prompt([
      {
        label: 'For the period',
        fieldname: 'period',
        fieldtype: 'Date',
        reqd: 1
      },
      {
        label: 'Date Generated',
        fieldname: 'date_generated',
        fieldtype: 'Date',
        reqd: 1
      }
    ], function(values) {
      var periodDate = values.period;
      var dateGenerated = values.date_generated;

      // Fetch records from Lot Payment Reference where posting_date is equal to the selected periodDate
      frappe.get_list('Lot Payment Reference', {
        filters: [
          ['posting_date', '=', periodDate]
        ],
        fields: [
          'name', // Document name (Row ID)
          'or_no',
          'or_date',
          'amount_paid'
          // Add other required fields here
        ]
      }).then(function(result) {
        var commissionTransactions = [];

        // Loop through the fetched records and populate the commissionTransactions list
        result.forEach(function(row) {
          var transaction = {};
          transaction.pnpa_no = row.name;
          transaction.or_no = row.or_no;
          transaction.or_date = row.or_date;
          transaction.total_collection = row.amount_paid;

          // Add other fields from Lot Payment Reference as needed
          // e.g., transaction.pnpa_date = row.pnpa_date;

          commissionTransactions.push(transaction);
        });

        // Set the value of 'commission_transaction' field in the parent form
        frm.set_value('commission_transaction', commissionTransactions);

        // Save the form to persist the changes made in the child table
        cur_frm.save();

        frappe.msgprint("Processing done for Commission for the period " + periodDate + " Please click on the printer icon to proceed to printing.");

      }).catch(function(err) {
        frappe.msgprint('Error fetching data from Lot Payment Reference: ' + err);
      });

    });

  }).addClass('btn-primary'); // Add the CSS class "btn-primary" to the button
}

}
});

Thank you and hoping that somebody can enlighten me.

this is the complete code:
frappe.ui.form.on(‘Commission Entry’, {
refresh: function(frm) {
if (frm.doc.docstatus === 1) { // Check if document status is “Submitted”
frm.add_custom_button(‘Generate Commission’, function() {

    // Prompt the user to enter the posting date using the calendar
    frappe.prompt([
      {
        label: 'For the period',
        fieldname: 'period',
        fieldtype: 'Date',
        reqd: 1
      },
      {
        label: 'Date Generated',
        fieldname: 'date_generated',
        fieldtype: 'Date',
        reqd: 1
      }
    ], function(values) {
      var periodDate = values.period;
      var dateGenerated = values.date_generated;

      // Fetch records from Lot Payment Reference where posting_date is equal to the selected periodDate
      frappe.get_list('Lot Payment Reference', {
        filters: [
          ['posting_date', '=', periodDate]
        ],
        fields: [
          'name', // Document name (Row ID)
          'or_no',
          'or_date',
          'amount_paid'
          // Add other required fields here
        ]
      }).then(function(result) {
        var commissionTransactions = [];

        // Loop through the fetched records and populate the commissionTransactions list
        result.forEach(function(row) {
          var transaction = {};
          transaction.pnpa_no = row.name;
          transaction.or_no = row.or_no;
          transaction.or_date = row.or_date;
          transaction.total_collection = row.amount_paid;

          // Add other fields from Lot Payment Reference as needed
          // e.g., transaction.pnpa_date = row.pnpa_date;

          commissionTransactions.push(transaction);
        });

        // Set the value of 'commission_transaction' field in the parent form
        frm.set_value('commission_transaction', commissionTransactions);

        // Save the form to persist the changes made in the child table
        cur_frm.save();

        frappe.msgprint("Processing done for Commission for the period " + periodDate + " Please click on the printer icon to proceed to printing.");

      }).catch(function(err) {
        frappe.msgprint('Error fetching data from Lot Payment Reference: ' + err);
      });

    });

  }).addClass('btn-primary'); // Add the CSS class "btn-primary" to the button
}

}
});