Add_child not working in workflow action phase

I am currently using the following code to add rows to a child table when there is an update in the “no_of_items” field. this code is functioning as expected.

frappe.ui.form.on('DoctypeName', {
    no_of_items: function(frm) {
        // This function is triggered when the 'no_of_items' field is updated.

        let child_table = 'child_field_name';
        // Replace 'child_field_name' with the actual name of your child table.

        // ----- Some code -----
        // Add any custom logic or calculations related to 'no_of_items' here.

        // Add a new row to the child table with some example values.
        frm.add_child(child_table, {
            col1: value1,
            col2: value2,
            col3: value3,
            col4: value4,
            col5: value5
        });

        // Refresh the child table to display the newly added row.
        frm.refresh_field(child_table);
    }
});

In a different case, my goal is to add rows to the child table during the “after_workflow_action” phase. I am using below code. The code is not adding rows to the child table, and there are no error messages showing up.

frappe.ui.form.on('DoctypeName', {
    after_workflow_action: async function(frm) {
        // This function is triggered after a workflow action is performed.

        let child_table = 'child_field_name';
        // Replace 'child_field_name' with the actual name of your child table.

        if (frm.doc.workflow_state == "Approved") {
            // Only add a new row to the child table if the workflow state is "Approved".

            // Add a new row to the child table with some example values.
            frm.add_child(child_table, {
                col1: value1,
                col2: value2,
                col3: value3,
                col4: value4,
                col5: value5
            });
        }
    }
});

Any help would be appreciated.

Hi @ILMN_LG,

Please check the syntax.

frappe.ui.form.on('Your Doctype', {
  validate: function (frm) {
    // Check if the workflow state is Approved
    if (frm.doc.workflow_state === 'Approved') {
      // Access the child table and add a new row with desired values
      var childTable = frm.doc.child_table_fieldname; // Replace 'child_table_fieldname' with the actual fieldname of your child table

      // Add values to the child table using the 'add_child' method
      var newRow = frappe.model.add_child(frm.doc, 'Child Doctype', 'child_table_fieldname'); // Replace 'Child Doctype' with the name of the child table doctype

      // Set the values for the fields in the new row
      newRow.fieldname1 = 'Value 1';
      newRow.fieldname2 = 'Value 2';
      // Add more fields as needed

      // Refresh the table to show the new row
      refresh_field('child_table_fieldname');
    }
  }
});

If the above script not worked then

remove it

validate: function (frm) {

and add it.

after_workflow_action: function (frm) {

Then reload and check it.

Thank You!

Hi @NCP
Actually I am fetching the records from child table and inserting into the same child table with different parent field.

Tried using below code, but no luck.

after_workflow_action: async (frm) => {
	if (frm.doc.workflow_state == "Approved") {
		frappe.db.get_list('Child DocType', {
          fields: ['col1', 'col2', 'col3', 'col4', 'col5'],
          filters: {
            parent: frm.doc.name
          }

        }).then(records => {
        	records.forEach(record => {
        	    var childRow = frappe.model.add_child(frm.doc, 'Child Doctype', 'child_table_fieldname');
	            childRow.col1: value1,
                childRow.col2: value2,
                childRow.col3: value3,
                childRow.col4: value4,
                childRow.col5: value5
        	});
        	frm.refresh_field('child_table_fieldname');
        });
	}
}

Please try it.

frappe.ui.form.on('DoctypeName', {
    after_workflow_action: async function(frm) {
        // This function is triggered after a workflow action is performed.

        if (frm.doc.workflow_state == "Approved") {
            // Fetch records from the child table for the current document.
            const childTableName = 'Child DocType';
            const childTableFieldName = 'child_table_fieldname';
            
            const records = await frappe.db.get_list(childTableName, {
                fields: ['col1', 'col2', 'col3', 'col4', 'col5'],
                filters: {
                    parent: frm.doc.name
                }
            });

            // Loop through the fetched records and add them to the child table of the current document.
            records.forEach(record => {
                var childRow = frappe.model.add_child(frm.doc, childTableName, childTableFieldName);
                childRow.col1 = record.col1; // Assign values from the fetched record
                childRow.col2 = record.col2;
                childRow.col3 = record.col3;
                childRow.col4 = record.col4;
                childRow.col5 = record.col5;
            });

            // Save the changes to the server.
            await frm.save();

            // Refresh the child table to display the newly added rows.
            frm.refresh_field(childTableFieldName);
        }
    }
});

Hi @NCP,

Still same, records not inserting and no error as well.
I have verified the table names, field names are correct.