Insert or Update Unique Records in a Collection
Learn how to insert or update unique records directly into a collection using Corvid (a.k.a. Wix Code).
This example shows you how to insert unique shape records into a collection called #Shapes. Once a shape exists within the #Shapes collection, you will be able to update its colour, but not add another shape of the same type. Try the example below.
#btnSubmit
Try it!
#iptShape
#iptColour
#txtMessage
Select a shape and colour for insert.
#repShapes
Rectangle
Green
Square
Green
Triangle
Red
Circle
Blue
Circle
Red
Circle
Red
Rectangle
Green
Circle
Blue
Square
Green
Add onChange event listeners to #iptShape and #iptColour dropdown boxes. These will check that a shape and colour have been selected before enabling the #btnSubmit button. Add an onClick event listener to #btnSubmit to insert or update the #Shapes collection.
Once the #btnSubmit is clicked, the wixData.query function checks the #Shapes collection for whether the shape entered in #iptShape is already in the collection. If it does not exist within the collection, it executes the saveShape function to insert it into the collection. If it already exists in the collection, it updates its colour. The #txtMessage text displays whether an insert or update operation has occurred.
This code does not allow duplicate shape's to be entered into the #Shapes collection. Also, note that the insert and update permissions need to allow for the permissions of user which will be executing this code.
Page Code