PurchaseOrder.AcceptChanges() and OrderRepository.Save(purchaseOrder) behaves inconsistently on Order Validation

Vote:
 

Recently, we were encountering save issues on purchase order update - that we narrowed down to a missing primary category (see following error message):

"Cannot insert the value NULL into column 'CatalogNode', table 'epicommerce.dbo.LineItem'; column does not allow nulls. UPDATE fails."

The code looks like below:

var orderReference = _orderRepository.SaveAsPurchaseOrder(cart);

var purchaseOrder = _orderRepository.Load<IPurchaseOrder>(orderReference.OrderGroupId);
purchaseOrder.OrderNumber = customTrackingNumber;
purchaseOrder.Properties["CustomField1"] = customField1Value;
purchaseOrder.Properties["CustomField2"] = customField2Value;

_orderRepository.Save(purchaseOrder); //THROWS ERROR HERE!!!

It makes me think now: Is Primary Category required?

If yes, then why did Add To Cart work -- should it validate that the primary category is required? Why did SaveAsPurchaseOrder() also work? Then fail on orderRepository.Save(purchaseOrder)?

When I looked at the database, the CatalogNode has empty blank string value (which seems to be a workaround on the NULL).

What's the best way to prevent this error from happening? I'm a bit torn on this -- it feels like firefighting to me if I forcefully validate the primary category.

Then, I tried using PurchaseOrder instead of IPurchaseOrder and it did not throw the error.

var orderReference = _orderRepository.SaveAsPurchaseOrder(cart);

var purchaseOrder = _orderRepository.Load<PurchaseOrder>(orderReference.OrderGroupId);
purchaseOrder.TrackingNumber = customTrackingNumber;
purchaseOrder["CustomField1"] = customField1Value;
purchaseOrder["CustomField2"] = customField2Value;

purchaseOrder.AcceptChanges(); //Saved successfully -- it did not throw an error

Which should be the approach?

#248684
Edited, Feb 16, 2021 0:13
Vote:
 

Have you validated your cart (_orderValidationService.ValidateOrder(cart) )before to convert into purchase order?

#248721
Edited, Feb 16, 2021 17:44
Vote:
 

@shella - Just checked on a running solution and Primary category is not required. I have added product directly to catalogue with no primary category and it works fine.

We are using IPurchaseOrder to convert to PO

var po = _orderRepository.Load<IPurchaseOrder>(orderReference.OrderGroupId);

I can see you have extended PO metaclass. Do any of these properties are required field. 

As @Sanjay mentioned; are you validating cart before converting it to purchase order. 

Check Episerver Foundation code from line #679

#248732
Feb 16, 2021 19:26
Vote:
 

Yes, we validate the cart.

As @Naveed said, Primary Category is not required. But on the Commerce Database - CatalogNode is a non-nullable column in the LineItem database table.

The extended PO metaclasses are not required and are even marked mandatory on the meta fields.

The error message specifically says: "Cannot insert the value NULL into column 'CatalogNode', table 'epicommerce.dbo.LineItem'; column does not allow nulls. UPDATE fails."

Thusthe error is not related to the extended meta fields.

I think what I don't get at this point is why purchaseOrder.AcceptChanges() work and _orderRepository.Save(iPurchaseOrder) fails -- with the same .

Those are different classes and source codes, I know, but, they have different behaviour -- it's just that I thought we're moving away from the MediaChase concrete classes.

#248810
Feb 17, 2021 23:19
Vote:
 

I have tried again, Created a product in catalogue root and successfully complete its purchase. I have looked at the database & LineItem table. Yes, the CatalogNode column does not allow null but I "think"  OrderRepository is saving empty string. I can clearly see in the table, there is no value in CatalogueNode but _orderRepository.save does not give any exception. 

see screenshot https://ibb.co/tPsN5nt 

In my point of view the difference between purchaseOrder.AcceptChanges() and _orderRepository.Save(iPurchaseOrder) is _orderRepository.Save(iPurchaseOrder) do necessary validation before saving PO

#248991
Feb 22, 2021 21:35
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.