Concatenate 2 fields in a Collection to a third field

I have a collection of ID ProductsServices
I have a reference field called ‘make’
I have a text field called ‘model’
I have a text field called ‘makeModel’

When a new item is added to the collection directly, I want to Concatenate make and model into the makeModel field with a space in between.

e.g.
make = Ford
model = Mustang
makeModel = Ford Mustang

I have tried many many things but nothing seems to work.

Could anyone help with this please ?

If you have no access to the string that represents the ‘make’ when initially inserting the item into the CMS, you can accomplish this using the beforeInsert() data hook.

Upon using wixData.insert() to create a new item, you can query the reference ID to get the relevant string associated to the ‘make’ and then concatenate it with the known ‘model’ string.

Adding the ‘makeModel’ key to the item object within the beforeInsert() data hook will insert new items with the properly formatted concatenated text field without having to add the key in the initial wixData.insert() call.

Example

export function ProductsServices_beforeInsert(item, context) {

    return wixData.get('ReferencedCollection', item.reference)
        .then((reference) => {
            item.makeModel = reference.name + " " + item.model; // Make sure both strings are correctly referenced according to your CMS schema
            return item;
        });

}

Hi,

I am new to Wix, Velo and coding. How would I query the string in another Collection ?

The collection where I store the ‘make’ is called ‘Brands’ and the field in which the ‘make’ is stored is called ‘brandName’.

How should my code look please ?

If that is the case, then this should work:

export function ProductsServices_beforeInsert(item, context) {

    return wixData.get('Brands', item.reference)
        .then((reference) => {
            item.makeModel = reference.brandName + " " + item.model; // Make sure both strings are correctly referenced according to your CMS schema
            return item;
        });

}

You can test it by adding manually adding a new item in the CMS dashboard with a ‘make’ reference and ‘model’ string, and you should see it automatically insert the concatenated string in the ‘makeModel’ field within the ‘ProductsServices’ CMS collection.

Sadly it doesnt work

Did you place this function in your site’s data.js file? Data hooks (such as this function) belong in a file called data.js in the Backend section of your site.

1 Like

Yes, it’s right there…

It seems I may have incorrectly referenced the ‘brandName’ in the wixData.get()

Try the following:

export function ProductsServices_beforeInsert(item, context) {

    return wixData.get('Brands', item.reference) //Change item.reference to the fieldname of the make reference inside of ProductsServices
        .then((reference) => {
            console.log(reference.brandName + " " + item.model);
            item.makeModel = reference.brandName + " " + item.model; // Make sure both strings are correctly referenced according to your CMS schema
            return item;
        });

}

I also added a console.log() to the code so you could make sure the brandName and model strings are not being returned as null in the Site Event Logs

1 Like

Sorry. That didnt work

This may help…

This is helpful! According to this schema the code should be:

export function ProductsServices_beforeInsert(item, context) {

    return wixData.get('Brands', item.make)
        .then((reference) => {
            console.log(reference.brandName + " " + item.model);
            item.makeModel = reference.brandName + " " + item.model; 
            return item;
        });

}

Sorry, that didnt work either. I now get this error…

Screenshot 2024-04-30 at 08.23.28

Is this inside of the ‘ProductsServices’ collection?

If so, this error (WDE0002) seems to be related to when you insert a item into this collection.

Check that the item ItemId you provided is a valid string.

Hi

Where did I provide an ID. I’m sorry but I am new to coding, Wix and Velo so I am not sure if I provided a valid string or not - I just copied in your code.

No worries! Items can be added to a CMS collection using the dashboard, Velo API, or a form connected to a dataset.

Based on the error you provided, it seems the ItemId column contains a invalid value (it should be a string).

This can happen when you use the insert() method to add a item to the CMS collection without a ItemId value in the object. It can also happen when a CMS item is updated at some point (such as in a Data Hook) without a valid ItemId.

To debug this issue, I recommend finding where the CMS item is created and seeing what value is being inserted for ItemId. If nothing is wrong there, you can then check to see where the ItemId is being updated. You can debug this issue by printing the value and viewing it in the Site Logs.

Which ItemId is it referring to ? In the Brands collection or ProductsServices collection ?

I don’t think I am going to get this to work but thank you anyway.