Record duplication from a custom widget

Hi all,
the following is my current function to duplicate a record:

async function duplicateRecord(action, record) {
  if (!confirm(`Confirm <${action.label}>?`)) { return; }
  const cols = config.duplicateCols;
  const fields = Object.fromEntries(  
    Object.entries(record).filter(([col]) => cols.includes(col))
  );
  try {
    const res = await grist.selectedTable.create({fields: fields,});
    grist.setCursorPos({rowId: res.id});
  } catch (err) { throwErr(`Cannot execute <${action.label}>: ${err}`); }  
}

The config.duplicateCols array contains the columns that are going to be copied to the duplicated record.

When duplicating ref or ref list columns I invariably obtain a #KeyError or an #IndexError. I understand I should specify the id attribute.

A couple of questions:

  1. How can I recognize ref and ref list columns and set id programmatically?
  2. While at that, how can I filter out formula fields?
  3. Is there a way to reuse from the custom widget the Grist record duplication function?

Thank you very much in advance for your help.

PS I am very close to releasing my process flow management custom widget.

Just to clarify, this question is different from my previous one:

In that case I started using a formula column to obtain the ids of children records, and duplicate them.

This time I would like to code a generic record duplication function.

Okay, rising the white flag.
I am again going to use a RECORD(rec) formula field.

There seems to be no simple programmatical way to duplicate a any kind of record.

In my use case, I will go for a specialized function, copying field by field from the RECORD(rec) formula field.

I suggest improving the Grist plugin api. It would be wonderful to have an option to obtain record values that can be directly inserted into a new or updated record.

See:

Thank you once more for your great work.

Next, I am goint to use this code to recognize formula columns:

Hi!

I had the same requirement, but a little more troublesome: I needed to duplicate an hierarchy spread trough 5 tables.

The solution I found:

  • create a toggle field on the main table.
  • create a webhook in grist to call n8n flow.
  • in n8n, do the duplications by a combination of query and inserts by using the Grist node.
  • in n8n, I uncheck the toggle field.

Not the more elegant possible, but I needed something functional and OSS.

best regards