Setting a reference value using wix data REST API

Question:
How do I set a reference to a blog post entry in a CMS table using wix-data REST APIs?

Product:
WIX Data REST APIs

What are you trying to achieve:
I have an automation that creates a CMS entry when a new Blog Post (of a particular category) is created. The CMS table has a field called document that is a single reference to the Blog Post. Currently I have to update that reference by hand. As part of my automation I would like to update that reference automatically.

What have you already tried:
Iā€™m trying the WIX Data REST API, method: ā€œpostā€, url: ā€œitems/insert-referenceā€.
It throws an exception:
ā€˜WDE0020: Provided property [document] is not a multi-reference fieldā€™.
If I change the document field to a multi-reference the code works, but really it needs to be a single reference field. So I feel this is probably the wrong API endpoint to be using.

Iā€™ve also tried just doing a put with url = items/a6fc9dcc-fe33-4475-9117-fcd357be71e1, where that guid is the id of the CMS entry I want to update. This does update the field, but updates it with the ID of the blog, not the reference. So this also feels like the wrong approach. Maybe thereā€™s some way to retrieve the reference number, and update the CMS record with that?

If only there was some way to get the reference ID.

Additional information:
I have a bad idea for an approach to this, which is to add a multi-reference field to the table, and in my automation put the reference there, then pull it out and put it into the single reference field. This will work, but oh what a hack.

Correct, this only works with multi-reference fields as noted in the documentation. For a single reference, you should use the regular insert() function, noting this from the docs:

" When inserting an item into a collection that has a reference field, set the value of the reference field to the referenced itemā€™s _id value or the entire referenced item object."

1 Like

My problem was that I was working directly with the blogs instead of the blogCMS. Itā€™s working now thanks to you ( and no thanks to ChatGPT).

1 Like

Oh awesome! For working with ChatGPT, sometimes I find you have to feed it the context of the documentation for it to give better answers for our environment. Something to try next time

1 Like