Adding or Updating a FildType

Hi, I’m trying to update the text data in a FiledType.TextOption field but I can’t seem to find the correct method. All Collections and option seem to be readOnly. The following test code is from inside a Products.Events.FieldDefinitionUpdated event. I can read all the values, but can’t find any documentation on how to set, update or replace the values. Creating a new field works fine using ‘fieldDefinitionService.Create()’

                var currentField = fieldDefinitionService.Get("Brand");
                if (currentField != null )
                {

                    //Cast the filed option
                    TextOption brandList = (TextOption)updateEvent.Item.Option;

                    //try to update field
                    for (var i = 0; i < brandList.Items.Count; i++)
                    {
                      
                       //First try
                      brandList.Items[i].Value = + "_"; //Fails Object read only

                       //Second try  fails Read only collection
                       brandList.Items.Insert(i, new TextOption.Item
                        {
                            Value = brandList.Items[i].Value + "_",
                            Name = brandList.Items[i].Name
                        } );

                    }
                  
                    updateEvent.Item.Option = brandLis;  //Fails

                    fieldDefinitionService.Update(updateEvent.Item);  //This have no effect?



                }

Litium version: 6.2

All entities that is fetched from litium is in readonly state to not allow modification of the entity in the cache. To be able to change any settings you need to make an writable clone of the entity,

You can find a section about ReadOnly on this page https://docs.litium.com/documentation/architecture/litium-api

If you change your first line to the following you will get a writable object that you can change.

var currentField = fieldDefinitionService.Get("Brand")?.MakeWritableClone();
1 Like