Child Table Attach field Permission Isssue

I Created A doctype Name Query wich Have a Child Table containing Fields “documents”, type link
“issue_date”, date
“expiry_date”, date
"attachment , while a user save document with attachments, its getting Error for Permissions, but with Attachment Sav is working fine and saving , aftre save client can upload attachment in draft mode , , aftre save client will submit (custom workflow action submit ==0 ) but im facing issue wile attach from client role and save i m getting error for this user not have permission to create new (random name) any sulotion for this , this hapning only while attach file with out attachment it is working fine

did you manage to solve this
i am also facing the similar issue

I solved
the permission Denied reason Child Table Save to unknow Document
and that image also linked to unknow documents

so you have to use a server script to save imediatly while open new record then child table will linked to Real parent

in my cas i made this

frappe.dom.set_style(`
  .mandatory-documents-glow{
    box-shadow: 0 0 0 3px rgba(255,165,0,0.35);
    border-radius: 6px;
    transition: box-shadow 0.2s ease;
  }
`);

function glow_documents_table(frm) {
  const f = frm.get_field("documents");
  if (!f || !f.wrapper) return;

  frappe.utils.scroll_to(f.wrapper);
  $(f.wrapper).addClass("mandatory-documents-glow");
  setTimeout(() => $(f.wrapper).removeClass("mandatory-documents-glow"), 4000);
}

function ensure_documents_row_and_focus(frm) {
  if (!frm.doc.documents || !frm.doc.documents.length) {
    frm.add_child("documents", {});
    frm.refresh_field("documents");
  }

  const field = frm.get_field("documents");
  if (!field || !field.grid) return;

  glow_documents_table(frm);

  setTimeout(() => {
    const grid = field.grid;
    const row = grid.grid_rows && grid.grid_rows[0];
    if (!row) return;

    const $inp = $(row.row).find('[data-fieldname="documents"] input');
    if ($inp.length) $inp.focus();
  }, 250);
}

async function fetch_current_user() {
  return new Promise((resolve) => {
    frappe.call({
      method: "frappe.client.get",
      args: { doctype: "User", name: frappe.session.user },
      callback: (r) => resolve(r.message || null),
      error: () => resolve(null),
    });
  });
}

frappe.ui.form.on("Query", {
  async onload(frm) {
    // Hide/lock documents table for new doc
    if (frm.is_new()) {
      frm.toggle_display("documents", false);
      frm.set_df_property("documents", "read_only", 1);
    }

    // Auto-fill client fields (only if empty)
    const user = await fetch_current_user();
    if (user) {
      // ⚠️ client_name is Link to "Client" in your JSON.
      // Best: store user's Client in a custom User field like `client` (Link to Client).
      // If you don't have it, this will try full_name (works only if your field is Data).
      if (!frm.doc.client_name) {
        if (user.client) {
          // preferred: User has a field `client` (Link to Client)
          frm.set_value("client_name", user.client);
        } else {
          // fallback (only valid if client_name is Data, not Link)
          // frm.set_value("client_name", user.full_name);
        }
      }

      if (!frm.doc.client_code && user.location) {
        frm.set_value("client_code", user.location);
      }
    }
  },

  refresh(frm) {
    if (!frm.is_new()) {
      frm.toggle_display("documents", true);
      frm.set_df_property("documents", "read_only", 0);
      frm.dashboard.clear_headline();
      return;
    }

    frm.toggle_display("documents", false);
    frm.set_df_property("documents", "read_only", 1);

    frm.dashboard.set_headline(`
      <div class="text-muted">
        Fill <b>Query Types</b> and <b>Raw Material</b>. The record will auto-save, then Documents will appear.
      </div>
    `);

    frm.events.try_autosave_and_show_documents(frm);
  },

  raw_material(frm) {
    frm.events.try_autosave_and_show_documents(frm);
  },

  query_types(frm) {
    frm.events.try_autosave_and_show_documents(frm);
  },

  try_autosave_and_show_documents(frm) {
    if (!frm.is_new()) return;
    if (frm.__sanha_autosaving || frm.__sanha_autosaved_once) return;

    if (!frm.doc.raw_material) return;
    if (!frm.doc.query_types) return;

    frm.__sanha_autosaving = true;

    frm.save()
      .then(() => {
        frm.__sanha_autosaved_once = true;

        frm.toggle_display("documents", true);
        frm.set_df_property("documents", "read_only", 0);
        frm.dashboard.clear_headline();

        // after save: add row + highlight + focus
        ensure_documents_row_and_focus(frm);

        frappe.show_alert({
          message: __("Record created. Add Documents and upload attachments."),
          indicator: "green",
        });
      })
      .finally(() => {
        frm.__sanha_autosaving = false;
      });
  },
});

1 Like