Copy a table row to a new row in the same table

Hi All,

What is the best way to copy a row in a table to a new row in the same table? Example:

Row 1 has Iten Number, Item Description, Lot Number, Misc
Copy Row 1 to a new row with all the same data (user wants to then wants to change the lot number manually).

He may have multiple same items but with different lot numbers and the users are looking for a quick copy function. Thank you.

1 Like

Hello @dpoetsch

Here’s a simple script (it’s not perfect, but it gets the job done—you can further improve it if needed) that copies the data from the row above when adding a new row.

(function () {
  'use strict';

  const TABLE_FIELD_CODE = 'Table';

  kintone.events.on(['app.record.create.change.' + TABLE_FIELD_CODE, 'app.record.edit.change.' + TABLE_FIELD_CODE], function (event) {
    const table = event.record[TABLE_FIELD_CODE].value;

    if (table.length < 2) return event;

    const lastIndex = table.length - 1;
    const newRow = table[lastIndex];
    const prevRow = table[lastIndex - 1];

    if (
      !newRow.value.ItemNumber.value &&
      !newRow.value.ItemDescription.value &&
      !newRow.value.LotNumber.value
    ) {
      newRow.value.ItemNumber.value = prevRow.value.ItemNumber.value;
      newRow.value.ItemDescription.value = prevRow.value.ItemDescription.value;
      newRow.value.LotNumber.value = prevRow.value.LotNumber.value;
    }

    return event;
  });
})();

1 Like

Hi @Chris,

Thank you this helps a lot. I was having issues getting to add a row. The index was throwing me off.

HI @Chris,

I'm trying to modify the code so that when the user checks a check box on the orginal line it will perform the copy. If the checkbox is not checked it will not copy if a new row is added. I added a line (see below) but it will not work. I'm playing around with the code right now. thanks.


1 Like

Hi @Chris,

Ok I think It got it. This seems to work. Testing some more to make sure.

2 Likes