productModel.GetValue() throws on some products

Hi, we have updated our database from 6.2 to 7.1 but on some products, the productModel.GetValue throws InvalidCastException. Where am expecting a null or the Color, am guessing that the data is “malformated”? Any suggestions on how to fix this without a lot of catching in the builder?

var color = productModel.GetValue(“Color”);

System.InvalidCastException: ‘Det gick inte att omvandla ett objekt av typen System.Collections.ObjectModel.ReadOnlyCollection`1[System.String] till typen System.String.’

Litium version: 7.1

The GetValue method need to have the type specified to be able to return the value.

productModel.GetValue<IList<string>>("Color");

Sorry, this was a bad code snippet to simplify. I’m using the default code from 7.1 Accelerator

return new ProductItemViewModel
{
Id = productModel.SelectedVariant.Id,
Price = productPriceModel,
StockStatusDescription = _stockService.GetStockStatusDescription(productModel.SelectedVariant),
Currency = currency,
IsInStock = _stockService.HasStock(productModel.SelectedVariant),
Images = productModel.GetValue<IList>(SystemFieldDefinitionConstants.Images).MapTo<IList>(),
Color = _fieldDefinitionService.Get(“Color”).GetTranslation(productModel.GetValue(“Color”)),

But this should be updated to <IList<string>>?

The accelerator has no support for multiple values in field color from what i have seen.
The variant selection for variant is based on that you have one color and one size on an article.

The answer will be it’s depend

  • If the TextOption does support multiple values you need to use GetValue<ILIst<string>>("Color")
  • if the TextOption does not support multiple values you need to use GetValue<string>("Color")

Default accelerator the field is not supporting multiple values.

To get nicer formatted code, use single backtick ` to start/end inline code and tripple backtick ``` on separate line to start/end code block.

1 Like