Developer forum

Forum » Development » How to save a product with customfields

How to save a product with customfields


Reply
 Hi,

I'm trying to save a product using the API, but i can't get my custom fields saved.

When i'm updating an existing product (on that is created in backend), this code works:

Product p = new Product( item.varenummer, "", lang.LanguageID );

ProductFieldValue pf2 = p.ProductFieldValues.GetProductFieldValue( "LeveringsBredde2" );

pf2.Value = "test" ;

p.Save();

But when i'm creating a product direktly using the API, i get a Null exception on

p.ProductFieldValues.

What's the correct ways to save customfields to a product, both to new and existing products.

Regards
 Martin


Replies

 
Jeppe Eriksson Agger Dynamicweb Employee
Jeppe Eriksson Agger
Reply
Hi Martin

Is this what you're looking for? http://engage.dynamicweb-cms.com/Forum-5.aspx?action=ShowThread&ThreadID=2210

- Jeppe
 
Reply
I got that far, but what do i do when prod.ProductFieldValues returns null, which it does when the the object is not yet saved.

Can i somehow manually set p.ProductFieldValues to what it's suppose to be.

It seems weird that it's null when there are CustomFields present ind the database.

Regards
 Martin

 
Reply
Comeone guys, :-)

How do i set a custom field if produkt.ProductFieldValues == null?

Someone at DW has to know this...

/ Martin
 
Reply
Hi there,

Simply supply it with a new ProductFieldValueCollection instance, just as you would instantiate any other .NET object or collection. Something like this should work:

if (product.ProductFieldValues == null)
{
  // Create the fields collection
  product.ProductFieldValues = new ProductFieldValueCollection();
}
ProductFieldValue myFieldValue = product.ProductFieldValues.GetProductFieldValue(customFieldName);
if (myFieldValue == null)
{
  // Create the field
  myFieldValue = new ProductFieldValue();
  ProductField myField;
  foreach (ProductField field in ProductField.FindProductFieldsBySystemName(customFieldName))
  {
    myField = field;
    break;
  }
  myFieldValue.ProductField = myField;
  product.ProductFieldValues.Add(myFieldValue);
  myFieldValue.Value = customFieldValue;
}

Hope this helps,

Imar
 
Reply
Hi Imar,

Thank you for the code sample.
I ended up with this code to:

ProductFieldValue myFieldValue = valueCollection.GetProductFieldValue( SystemName );
if ( myFieldValue == null )
{
  // Create the field
  myFieldValue = new ProductFieldValue();
  ProductField myField = new ProductField();
  foreach ( ProductField field in ProductField.FindProductFieldsBySystemName( SystemName ) )
  {
    myField = field;
    break;
  }
  myFieldValue.ProductField = myField;
  valueCollection.Add( myFieldValue );
  myFieldValue.Value = Value;
}
else {
  myFieldValue.Value = Value;
}
 
Reply
Ah, yes, the line

myFieldValue.Value = customFieldValue;

should have been placed outside the if as it also needs a value when the field already exists.

I wouldn't do this though:

ProductField myField = new ProductField();
foreach ( ProductField field in ProductField.FindProductFieldsBySystemName( SystemName ) )

You shouldn't "new up" the ProductField. If you find it in the collection using FindProductFieldsBySystemName, you already get a valid instance, so newing up the item just wastes CPU cycles and memory.
If the item is not found using FindProductFieldsBySystemName, you want myField to be null, and the code to crash so you can fix the problem. By newing up an instance, missing system fields go unnoticed, yet don't get saved.

Hope this helps,

Imar
 
Lars Hejgaard Sørensen
Reply

Hi,

Another option is to save the product and re-instantiate it. Not very pretty, but yet, not much risk involved since DW does all the dirty work in this approach.

BR.
Lars

 

You must be logged in to post in the forum