Developer forum

Forum » Development » Problem setting custom order field

Problem setting custom order field

Per Ljung
Reply
Hi, I get NullReference Exception when calling this submethod:

SetOrderField(order, "Ordernumber", orderNo);

SetOrderField(order, "TransferDate", DateTime.Now.Date);
order.Save(order.ID);


The same applies to setting product fields.

Here is the submethod:

private static void SetOrderField(Order order, string fieldName, object value)
        {
            if (order.OrderFieldValues == null)
            {
                order.OrderFieldValues = new OrderFieldValueCollection();

            }

            OrderFieldValue fieldValue = order.OrderFieldValues.GetOrderFieldValue(fieldName);

            OrderField newField = null;

            if (fieldValue == null)
            {
                fieldValue = new OrderFieldValue();
                foreach (OrderField field in OrderField.getOrderFields())
                {
                    if (field.Name == fieldName)
                    {
                        newField = field;
                        break;

                    }
                }

            }

            fieldValue.OrderField = newField;

            order.OrderFieldValues.Add(fieldValue);

            fieldValue.Value = value;

        }
Best regards,
Per

Replies

 
Morten Bengtson
Reply
Hi Per,

Maybe the field is actually named something else and does not match "Ordernumber"? That would cause your field value to be created without a field.

Is name and systemname identical on all of your order fields?
First you try to get the field value by systemname - order.OrderFieldValues.GetOrderFieldValue(fieldName);
And later you try to find the field by name (not systemname) - if (field.Name == fieldName)
 
You can try this...
        public static void SetOrderFieldValue(Order order, string systemname, object value)
        {
            var orderFields = OrderField.getOrderFields();
            if (!orderFields.Contains(systemname))
            {
                // the field does not exist. you need to create in in management center (or programmatically)
                throw new ArgumentException(systemname, "systemname");
            }
            
            var val = order.OrderFieldValues.GetOrderFieldValue(systemname);
            if (val != null)
            {
                // field value exists. just change the value (maybe add validation to make sure you are not setting a text value on a date field or something like that?)
                val.Value = value;
            }
            else
            {
                // field value does not exist. find the correct field and add new field value to the order.
                foreach (OrderField field in orderFields)
                {
                    if (!field.SystemName.Equals(systemname, StringComparison.OrdinalIgnoreCase)) continue;

                    val = new OrderFieldValue(field, value);
                    order.OrderFieldValues.Add(val);

                    break;
                }
            }
        }


 

You must be logged in to post in the forum