I have three fields in my dataset (make, model and makeModel) and I want to concatenate make and model into the makeModel field. For example…
make: Ford (this is a reference field to another Collection)
model: Mustang (this is a Text field)
makeModel: Ford Mustang (this is the concatenated field of type Text)
I have tried this but it doesnt seem to work. I am sure I have the names of the dataset and the fields correct in the code.
import wixData from 'wix-data';
export function ProductsServices_beforeInsert(item, context) {
const fieldValue1 = item.make;
const fieldValue2 = item.model;
// Concatenate the values
const concatenatedValue = fieldValue1 + " " + fieldValue2;
// Update the item with the concatenated value before inserting it into the collection
item.makeModel = concatenatedValue;
}
Simply add return item at the end of your code and you can use it by item = ProductsServices_beforeInsert(item) (i am assuming that you have declared the item varible and just want to concatenate the make and model values of the item)
export function ProductsServices_beforeInsert(item, context) {
const fieldValue1 = item.make;
const fieldValue2 = item.model;
// Concatenate the values
const concatenatedValue = fieldValue1 + " " + fieldValue2;
// Update the item with the concatenated value before inserting it into the collection
item.makeModel = concatenatedValue;
item = ProductsServices_beforeInsert(item)
}
you need to add an return statement in your function you can use this code
import wixData from 'wix-data';
export function ProductsServices_beforeInsert(item, context) {
const fieldValue1 = item.make;
const fieldValue2 = item.model;
// Concatenate the values
const concatenatedValue = fieldValue1 + " " + fieldValue2;
// Update the item with the concatenated value before inserting it into the collection
item.makeModel = concatenatedValue;
return(item);
}
I copied this code from ChatGPT so I don’t know if all the variables that need to be set have been set or not. I am an absolute beginner to Velo and Java.
I’m not sure what else I need to add.