I want to replace the field using built-in function replace_field

Now the problem i am facing is it is replacing the field but not putting/rendering that field exactly where the previous field was located in the form.
Here is a code:

var render_dynamic_field = function(frm,fieldtype, options, fieldname) {
var dynamic_field = frappe.ui.form.make_control({
df: {
“fieldtype”: fieldtype,
“fieldname”: fieldname,
“options”: options || ‘’,
“label”: ‘New’
},
parent: frm.fields_dict.new.wrapper,
only_input: false
});
dynamic_field.make_input();
frm.layout.replace_field(‘new’, dynamic_field.df);
console.log(frm.fields_dict);
};

Hi @sami_kalhoro ,

The frm.fields_list is used to determine the index of the “new” field in the form layout. Then, the splice method is used to replace the “new” field with the dynamic field at the same index. Finally, the refresh_section method is called on the form layout to update the form’s fields. By using these modifications, the dynamic field should be rendered in the same position where the previous field (“new”) was located in the form

var render_dynamic_field = function(frm, fieldtype, options, fieldname) {
  var dynamic_field = frappe.ui.form.make_control({
    df: {
      "fieldtype": fieldtype,
      "fieldname": fieldname,
      "options": options || '',
      "label": 'New'
    },
    parent: frm.fields_dict.new.wrapper,
    only_input: false
  });

  dynamic_field.make_input();
  
  var field_index = frm.fields_list.indexOf('new');
  
  frm.fields_list.splice(field_index, 1, dynamic_field);
  
  frm.layout.refresh_section('fields');

  console.log(frm.fields_dict);
};

Hope this will help you out.
Thank you.

Hey @VINOTH,
Thank you for your response but i am getting error when using indexOf() the error is typeError: Cannot read properties of undefined (reading ‘indexOf’).