However, sometimes some fields on some rows go missing (even though I am using the query THEN update). I’ve been trying to achieve the same thing using getCurrentItem & setFieldValue as follows:
But I just can’t get it to work. It only writes in the first row only, and doesn’t move on to the next item. I’ve also tried using for loops too, same result.
I’d really appreciate someone pointing out what I’m doing wrong… Thanks.
@jonatandor35 Sorry, typo (bit like your retrun items ;-0 :-))!! Only kidding, sorry, I really appreciate the help as always J.D. Yes, I can see how this would work. But just for the exercise (and for others finding this post in the future I guess) I’d love to try and get it working with setFieldValues…I’ll be honest and say that update() / bulkUpdate scare me a little bit in overwriting everything, I have a big database with lots of items, and I’d really prefer to just set the fields that I need to…seems less risky somehow! Particularly if I make a coding error, since the only field I can overwrite is the one I call in setFieldValues(), that’s why I prefer that approach, if that makes sense?
@aquacruise yes I had a few typos . Fixed now.
Re the setFieldValues, I never use it, so I can’t help (maybe someone else), but I don’t think the way you did it can work, because there’s no “currentItem” there.
@jonatandor35 Many thanks. I’ll prompt @givemeawhisky as they suggested this as a better way to update for selected fields. I did try using .next() to move to the next item but that didn’t work.
@jonatandor35 YES YES YES! How do I like a thousand times!! I’m new to all this, so didn’t even know you could do that! Far simpler. For anybody else finding this post in the future, the solution is:
Have a quick look at https://support.wix.com/en/article/corvid-using-data-hooks . Go to your data manager, click hooks, and then add/edit code and choose before Insert. This will create a data.js file. Then I just added the following:
export function myDatabaseName_beforeInsert(item, context) {
item.seoTitle = item.name + item.length + item.builder
return item //Edit as per J.D. below
}
And that’s it! Every time I insert a new item in the database, it automatically does what I was trying to achieve. The only thing that is missing (for me) is that I will need to go and manually edit all the existing entries, since this only works on new items inserted into the database.
Write a beforeUpadte() hook with the same function, then run a 1-time query to retrieve all items and bulk update them (then you can remove the beforeUpdate() hook.
(I have no experience with routers)