CreateNewItem with values

I am building an eCommerce plugin, mostly for fun, that fetches products and lists them in the sidabar. I have a Block called ProductBlock with a few properties, when i click on a Product in the sidebar i want to reach the form for creating a new ProductBlock item, i can do this with ctx.createNewItem(‘ProductBlock’). But, i would like to pass values to it.
It would be nice to be able to pass an object with key value pars to the createNewItem function that populates the fields.

I am also getting an error when the createNewItem modal is opened, it shows as “genericError” in the modal and when i try to save it seems like it does not know where to save the new block. Am i missing something?

Hi @cristoffer.carlberg,

Welcome to the forum, and that’s a good idea! Please remember to vote on it at the top left :slight_smile:

A workaround that might sometimes (not always) work is to use the CMA to create that item record first (as an unpublished draft, with prefilled values) and then use ctx.editItem() to let the user edit it.

Can you provide a reproduction or at least some sample code for that, please? You can either post it here, DM me, or email it to support@datocms.com. Or share the repo with me at r.tuan@datocms.com?

Maybe you’re trying to create a block outside of a record field…? Blocks don’t have their own lifecycles, but are rather just “embedded” fields inside an actual record, so you can’t create them directly. If that’s not the case, please share some code with us and we’ll take a closer look!