Five New Optimizely Certifications are Here! Validate your expertise and advance your career with our latest certification exams. Click here to find out more

Programmatically moving variation to product as parent has error "Parent of a node must be another node or a catalog."

Vote:
 

Hi

I've got some logic that sometimes has to move a variation to beneath a product, I have code that looks like this:

contentRepository.Move(variation.ContentLink, product.ContentLink, AccessLevel.NoAccess, AccessLevel.NoAccess);

Unfortunately it throws this error whenever it does this:

System.InvalidOperationException: Parent of a node must be another node or a catalog.
at EPiServer.Commerce.Catalog.Provider.Internal.CatalogContentMoveHandler.GetNewParent(ContentReference newParentLink, Int32& newParentNodeId, Int32& newCatalogId)
at EPiServer.Commerce.Catalog.Provider.Internal.CatalogContentMoveHandler.SetParentNodeForEntry(ContentReference contentLink, ContentReference newParentLink)
at EPiServer.Commerce.Catalog.Provider.Internal.CatalogContentMoveHandler.MoveWithTransaction(Action`2 moveMethod, ContentReference contentLink, ContentReference newParentLink)
at EPiServer.Core.Internal.DefaultContentRepository.Move(ContentReference contentLink, ContentReference destinationLink, AccessLevel requiredSourceAccess, AccessLevel requiredDestinationAccess)
at FOO.BAR.Web.Features.Products.Import.ProductImportJob.ImportProducts(ImportProductsRootModel productsRootModel, ErpSystem erpSystem, ContentReference productsCatalogLink) in C:\Dev\FOOBAR\src\FOO.BAR.Web\Fea
tures\Products\Import\ProductImportJob.cs:line 539

From digging around, it seems there might be a bug in the CatalogContentMoveHandler.. It seems to have two separate methods for handling the move of a entry vs node, but then both of those methods use the same GetNewParent private method which has a switch statement for the destination content type to only be CatalogNode or Catalog, otherwise throws the above error..

Is there a bug, or am I using this wrong?

We're on Commerce v11.8.5 (latest v11.x version)

#198611
Nov 01, 2018 6:37
Vote:
 

You can't move a variant to a product. If you want to assign a variant to a product, you would have to use IRelationRepository to save a new ProductVariation relation 

            var relation = new ProductVariation();
            relation.Parent = Product.ContentLink;
            relation.Child = Variant.ContentLink;
            _relationRepository.UpdateRelation(relation);
#198617
Nov 01, 2018 9:03
Vote:
 

Hi Quan

Hmm, it seems contrary to creating a new variation which you can have the product as its parent:

var newVariation = contentRepository.GetDefault<MyVariation>(productLink);

And also in the UI, it seems to support this also:

Oh and additionally the context menu to create a new variation under the product is offered too:

Are you sure it shouldn't be possible to move a variant to under a product?

#198873
Nov 08, 2018 7:21
Vote:
 

Good question. But the UI does not have to reflect the API 100%. It is used to make sure the life for editors as easy and as productive as possible. As a developer you should understand the underlying implementation and go with it.

#198874
Nov 08, 2018 7:30
Vote:
 

Hi Quan, sure but the creation content API does allow putting a variation under a product.. And from noseying around the catalog content provider's move logic, I think it might just be a simple bug - looks like they prepared it to handle move of entries versus nodes differently but then just applied the same check anyway.. Feels like a mistake to me.

#198875
Edited, Nov 08, 2018 7:32
Vote:
 

The API example you showed just set the ParentLink to the product. It will not work if you call GetChildren on the product or GetAncestors on the variants. I would say that is the inconsistency but sometimes we don't bother to check those conditions

#198876
Nov 08, 2018 7:39
Vote:
 

I'll take your wisdom over mine :) But definitely wasn't intuitive behavior for the move logic for me.

#198877
Nov 08, 2018 7:41
Vote:
 

I realized we can make it a little better by having a better exception message, "Parent of a node must be another node or a catalog." is not exactly correct in this case :)

#198883
Nov 08, 2018 8:36
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* 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.