Fields are going when refresh is done

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');
            });
        });
    }

Please check the vide.

Hi @NCP,

The same thing I have done. Actually I am populating the fields as you did, I will explain in your code. When New Invoice Data is refreshed the auto populated fields are not displaying. while I am coming from Custom Button, It is getting displayed, but after refresh it is not displaying.

That is my issue.

It’s simple logic that if you refresh then the data is gone. If the client does not apply there, for that you have to apply the fetch from condition on the doctype where it is redirected, otherwise the client script should be applied, then the data can come again.

Did you save the form after clicking the button?

I mean it will obviously refresh the data if you don’t save it.
And since it will take you to the new-doctype url, it will reset everything.

Actually For the first time when we are opening through the button and refreshing, the fields are going, After save there is no problem as I mentioned.

Yes… That is how it works. Make sure you save before refreshing the form. Because the button mimics creating a new form. So if you don’t save it, the data is gone.