I am having a doctype named ECP and some fields in it. Now I am auto populating that fields to another doctype named ECN. auto populating is done by creating a field in ECN and through script. the scenario is ECN can’t be created directly, Through ECP only it is created by custom button. When button is clicked fields are getting auto populated to ECN, but while refresh the auto populated fields are going.
I haven’t used link for auto populate fields. ECN and ECP has different field names and they are linked through script.
Is there any solution?
the code is as below
//Code
refresh: function(frm) {
// Check if the current user is the ECN owner and show the ‘Create ECN’ button accordingly
if ((frm.doc.status === ‘Approved’ || frm.doc.status === ‘Rejected’) && frm.doc.ecn_owner_mail_id === frappe.session.user) {
// Show the “Create Fimer ECN” button
frm.add_custom_button(__(“Create ECN”), () => {
frappe.new_doc(“ECN”, {}, (ecn) => {
ecn.ecp_number = frm.doc.name;
ecn.ecn_owner_mail = frm.doc.ecn_owner_mail_id;
ecn.ecp_initiator_mail = frm.doc.ecp_initiator_mail_id;
ecn.reporting_manager_mail = frm.doc.repo_to;
//Auto populate material_selection table to Fimer ECN
// Loop through each material item in the material_selection
frm.doc.material_selection.forEach((material_item) => {
let ecn_item = frappe.model.add_child(ecn, ‘ecn_material_selection’);
ecn_item.material = material_item.material;
ecn_item.material_description = material_item.material_description;
// Set additional fields as null or empty
ecn_item.old_rev_no = null;
ecn_item.new_rev_no = null;
ecn_item.current_quantity = null;
ecn_item.new_quantity = null;
ecn_item.remarks = null;
ecn_item.kit_affected = null;
});
// Refresh the form to show the newly added child table rows
frm.refresh_field('material_selection');
});
});
}