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:
How can I recognize ref and ref list columns and set id
programmatically?
While at that, how can I filter out formula fields?
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:
I am developing a custom widget with a button that should duplicate a record. The record contains some reference fields and some reference list fields.
The record returned by grist.onRecord(record) {} is something like:
{
"id": 14,
"Name": "Andrea",
"City": "Mi",
"CalcId": 14,
"Books": [
"Pride and prejudice",
"Animal farm"
],
"Books2": [
"La vita nuova"
],
"Ref": "Convivio",
"A": 14,
"Addresses_City": [
"Milano",
"Roma"
]
}
Note that Books, Books2,…
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:
opened 06:54AM - 07 Feb 25 UTC
### Describe the problem to be solved
Following this discussion (@berhalak @pau… lfitz):
https://community.getgrist.com/t/how-to-obtain-the-id-of-referenced-records-inside-a-custom-widget/8213
I open this issue as a placeholder for some suggestions of enhancement of grist-plugin-api.
Disclaimer: I am just learning the power of custom widgets, please, be patient if I do not fully get them...
It seems to me, that grist-plugin-api is currently conceived to read data from records and tables for the main purpose of displaying it.
But the users are using the custom widget functionality for developing more complex tools, that introduce workflow logic and actively act on records and related tables.
### Describe the solution you would like
Maybe only very simple enhancements of the [`grist-plugin-api`](https://support.getgrist.com/code/modules/grist_plugin_api/) are enough.
Eg. adding additional options to [`FetchSelectedOptions`](https://support.getgrist.com/code/interfaces/grist_plugin_api.FetchSelectedOptions/) that have impact on: onRecords, onRecord, fetchSelectedRecord, fetchSelectedTable. For example, something similar to what is currently available in the [`RECORD` function](https://support.getgrist.com/functions/#record_2).
I imagine a "duplicate and edit" scenario where the custom widget gets the selected record _with all its references_, modifies it and add it back to the same table without errors. It would be very useful to obtain a record in the exact format that can be readily inserted back into the same table as-it-is (eg. with referenced records ids in a list format, datetime format, ...).
Thank you!
PS While we are at it, I suggest adding simple examples to the `grist-plugin-api` doc. The `Inspect API` custom plugin was of great help for me as a novice.
Thank you once more for your great work.
Next, I am goint to use this code to recognize formula columns:
Officially it is not supported yet. The assumption is that a custom widget should know very little about the table it is used on. The dependency is inverted here, widget should inform Grist what columns it expects and Grist should map those columns back. You can read more about column mappings here Custom - Grist Help Center
But if you really need to get this information, you can use general API exposed to widgets and just query for the metdata. Here is an example how the Calendar widget is doi…
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