How to duplicate a record via API and change a field value?

Dato feature question! Would it be possible to create a new Question/Active Learning Card duplicate option (in addition to the current one, not as a replacement) that generates a new UUID upon duplication rather than copying the old one?

Content team would like to have a new duplicate option, which generates new UUID for UUID field.

This is our plugin to generate UUIDs, we would like to have a new duplicate option which duplicates the record and changes uuid field by generating a new one.

From the API, I see a duplicate option, but only accepts an item id to duplicate, is there a way to change a field of duplicated record?

@henok.tesfaye to duplicate and change a field you should do two calls, you cannot do it in one go unfortunately.

The problem is that you also need to generate the UUID on your end as the plugin is something run only on the CMS UI, so you cannot expect it to work.

Hope that makes sense?

1 Like

Ok, thanks. I’ll go with the two calls.

But for the UUID, I can run the similar logic the CMS UI do for the new plugin. So I hope I can get the new duplicated record id(which is basically different from the original record) and update any field I want using the id, let me check that and will come back soon.

Thank you!

1 Like

I’m able to implement the duplicate record plugin which updates a uuid field as well, by having two requests one to duplicate the record, and the other one to update the uuid field of the duplicated record.

// duplicate a record and generate a new cuid id for the duplciated record.
      const { id: duplicateRecordItemId } = await client.item.duplicate(itemId)
      await client.items.update(duplicateRecordItemId, {
        uuid: cuid()
      })
      plugin.notice('Record has been successfully duplicated!')

Content team has noticed that if they close the record page while duplicating, the duplicated record uuid is same with the original uuid.

if I close out of the item record before I get the green pop-up registering a successful duplicate, the item duplicates, but the UUID is not revised–I’m guessing I’m cutting off the process by closing out? If there’s any way to allow the process to continue after exiting a record that would be amazing for workflow.

But if we keep waiting the record page till we get the success message, the record will be duplicated and will have a new uuid. Please see the record

How can we make sure the two requests complete, even if we close the record page? Thanks!

1 Like

Hello @henok.tesfaye

You have a couple of options to prohibit the user to leave the record page while a request is being made.

You could open a modal for confirmation of duplication, with no close option, and only close the modal once both requests have been completed.

For a more unusual approach, you could also edit a field value to set the record state as “With unsaved changes” and only revert the change once both requests are done.

I would go for the modal approach though.

Hope this helps!

1 Like

Thank you @m.finamor . The modal options looks good. But since plugins are embedded inside an iframe. How can we show a modal which appears on the center of Dato page, not just in that iframe.

1 Like

This is only available in the new plugin SDK: https://www.datocms.com/docs/plugin-sdk/modals

If you are using the old SDK you can migrate your plugin to use the new one: Plugin SDK - Migrating from legacy plugins - DatoCMS Docs

1 Like