Concatenate Fields Before Insert

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; 
}

Thank you in advance

Allan

You need to return item at the end of this function.

I’m new to Velo, Java and coding.

How do I do that, please ?

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)

also why dont you just concatente while creating it like this:

item = {
    make: makeVarible
    model: modelVarible
    makeModel: makeVarible + modelVarible
}

(change the makeVarible and modelVarible with your own data)

So would my code be…

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;
item = ProductsServices_beforeInsert(item)
}

Where would I add this code ? Which hook ?

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.

I am now just getting “undefined undefined” in the makeModel field.

function ProductsServices_beforeInsert(item) {
    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;
}

let item = {
    make: "make",
    model: "model",
    makeModel: ""
}

item = ProductsServices_beforeInsert(item)

console.log(item)

so the output should be { make: "make", model: "model", makeModel: "make model" }

Note: i deleted the unused varible “context”

I had " return item; " and I was getting undefined undefined

I then changed it to return(item); but now nothing happens

sorry i am used to return(item); usage but in javascript its return item sorry

i edited and fixed the error

where is the commas?

What commas ?? Do I need to add commas ?

add comma after varible inside item like this

let item = {
    make: "make",
    model: "model",
    makeModel: ""
}

I am still getting " undefined undefined "

it works on me. Can you show me all of the code that you are using