Store newly added child documents and access them in "before_save"

Hello,

I am trying to store the names of newly added child documents in an array to later use them in the “before_save” event of the parent doc. My code looks something like this:

let newChildren = [];
frappe.ui.form.on('ChildDoctype', {
   table_fieldname_in_parent_doc_add(frm, cdt, cdn){
      newChildren.push(cdn);
      frappe.msgprint(newChildren[0]); //prints name of first child added
   }
});

frappe.ui.form.on('ParentDoctype', {
   before_save(frm){
      frappe.msgprint(newChildren[0]); //prints an empty message
   }
});

However, the contents of the array seem to be reset to and empty object, because newChildren.length doesn’t return 0. I can’t seem to find a way to do this properly in the docs, so I’m turning to community members more experienced with Frappe than I am.

Thank you,

Bouilloire