How to copy child records between Knack records

copy-records

Knack currently doesn’t have the inbuilt capability to duplicate a record’s connected records in either the Builder or live app.

The only workaround is to create an automation (Knack Flows, Make, etc) to copy multiple records - this can still be cumbersome to maintain, and can consume a lot of valuable operations.

This native Knack form/JavaScript solution is great for use cases where you want to copy a parent record (e.g. Project) and all its related child records (e.g. Tasks).

Here is what the copyRecords function looks like

Knack.on(‘form:submit’, async ({ record, viewKey, isEdit }) => {
if (viewKey === ‘view_aa’) {
const newParentId = record.id;
const oldParentId = record.field_aa_raw[0].id;
copyRecords({
filters: [{ field: ‘field_bb’, operator: ‘is’, value: oldParentId }],
recordToCreate: { field_bb: newParentId },
copyFromFieldKey: ‘field_cc’,
gridSceneKey: ‘scene_bb’,
gridViewKey: ‘view_bb’,
formSceneKey: ‘scene_bb’,
formViewKey: ‘view_cc’
});
}
});

Here is an example of the function call
Knack.on('form:submit', async ({ record, viewKey, isEdit }) => {
  if (viewKey === 'view_aa') {
    const newParentId = record.id;
    const oldParentId = record.field_aa_raw[0].id;
    copyRecords({
      filters: [{ field: 'field_bb', operator: 'is', value: oldParentId }],
      recordToCreate: { field_bb: newParentId },
      copyFromFieldKey: 'field_cc',
      gridSceneKey: 'scene_bb',
      gridViewKey: 'view_bb',
      formSceneKey: 'scene_bb',
      formViewKey: 'view_cc'
    });
  }
});

Click here to access the free installation instructions for both Classic and Next-Gen.
Also feel free to check out my full library of Knack resources here.

1 Like

Just what I needed Stephen! Thank you so much. Earlier this week I was combing through the Knack community looking for this option and finding it on feature requests, and now today this post pops up and its just what I’m looking for. I’ve implemented it on our app and so far it is working exactly as I want it to!