Update reference field

How can I update a reference field based on another field?

For a reference field, the only element available seems to be a dropdown and it’s value is automatically validated against the referenced collection. Thus, if I’m adding a new records, I get an error if I use a dropdown.

I’ve also tried using a back end as follows:

This is the original backend code for phone number in Customer Profile that works fine.

export function CustomerProfile_beforeInsert(item, context) {
//////// WORKS FINE
// phone number
item.customerPhone = formatPhoneNumber(item.customerPhone)

return item;
}

I updated this code based on some searches and updated the above as follows to copy the groupId to the reference field (it’s two fields because reference was added later-long story):

export function CustomerProfile_beforeInsert(item, context) {
//////// THIS PART WORKS FINE
// phone number
item.customerPhone = formatPhoneNumber(item.customerPhone)

///////// REFERENCE FIELD IS NOT GETTING UPDATED
// copy groupId to reference field
item.reference = item.groupId;

return item;
}

GroupId is updated via an input text and a Save() command when a button is clicked.

I’m working in Editor. What I’m trying to do is create a Customer Profile item when a new Group item is first created. These are custom Group and Customer Profile collections. I’m updating all the fields in Customer Profile via hidden input elements connected the Customer Profile collection on the page for creating a new Group item. Everthing works fine except for the referece field.

Please help.

Hello, @James_Hill
When groupId whispers to the reference field, we’ll follow these steps:

Understand the Choreography:
You’re creating a Customer Profile item when a new Group item is born.
The reference field needs to mirror the groupId.
Backstage in the Backend:
Your backend code is the conductor of this dance.
Let’s tweak it to ensure the reference field gets its steps right.
Update the Backend Logic:
In your CustomerProfile_beforeInsert function:
After formatting the phone number (which works fine), add the following:

// Copy groupId to reference field
item.reference = item.groupId;

This step ensures the reference field aligns with the groupId.
Test the Routine:
Create a new Group item and observe if the reference field behaves as expected.
Imagine it as the grand performance on opening night.

Remember, even in the world of backend logic, precision and synchronization lead to applause.

I hope this info is helpful to you.

Best Regard,
Gregory Chavez

Issue solved. item.groupId was not getting populated. Thus, the copy was coping a blank item.