Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • M N 125 posts 212 karma points
    Mar 24, 2016 @ 21:39
    M N
    0

    View products crashes after dynamically adding a product with data type

    Hey all,

    So I'm writing a CSV import, as I have 40k products. I'll share in the other thread when I'm done.. Creating the products within catalog/collection etc has been no problem. Easy really, loving the services!

    However, when I add a product via code with a detached data type it basically breaks the whole backoffice View Products (click on a collection etc).

    Here is how I am adding my product, basic services setup -

            var productService = MerchelloContext.Current.Services.ProductService;
            var productVariantService = MerchelloContext.Current.Services.ProductService;
            var entityService = MerchelloContext.Current.Services.EntityCollectionService;
            var detachedContentService = MerchelloContext.Current.Services.DetachedContentTypeService;
            var contentTypeService = Services.ContentTypeService;
    

    Set up warehouse and catalog

            var warehouse = merchServices.WarehouseService.GetDefaultWarehouse();
            var catalog = warehouse.WarehouseCatalogs.FirstOrDefault(x => x.Name == "PrimaryCatalog");
    

    Create a product

            var product = productService.CreateProduct("New Product", "423623", 6.99M);
            product.Shippable = true;
            product.OnSale = false;
            product.SalePrice = 6M;
            product.CostOfGoods = 4M;
            product.Taxable = true;
            product.TrackInventory = false;
            product.Available = true;
            product.Weight = .3215M;
    

    Add it to Catalog & Collection

            product.AddToCatalogInventory(catalog);
    
            //Not sure if there is a nicer way of doing this, didn't see a way to grab by name
            var bagCollection = entityService.GetAll().FirstOrDefault(a => a.Name == "Bags");
    
            //save it
            productService.Save(product, false);
    
            //add to collections
            product.AddToCollection(bagCollection);
    

    Grab my content type (basic type created in Umbraco back office)

            var contentType = contentTypeService.GetContentType("ProductApparel");
    

    add detached content for "ProductApparel"

             var detachedContent = new ProductVariantDetachedContent(
                    product.ProductVariantKey,
                    detachedContentType,
                    "en-US",
                    new DetachedDataValuesCollection(
                        new[]
                        {
                            new KeyValuePair<string, string>(
                                "AnotherValue",
                                "\"12345678\""),
                            new KeyValuePair<string, string>(
                                "InternalNumber",
                                "\"AB1234\""),
                            new KeyValuePair<string, string>(
                                "availableSizes",
                                "\"XS-4XL\""),
                        }))
                {CanBeRendered = true};
    
            //add it to current product
            product.DetachedContents.Add(detachedContent);
    
            //save
            productService.Save(product, false);
    

    This all works great.. And when I view the product from the backoffice, it works only once. I click on "Bags" and I see my product. I view extended content, and the values are populated as I would expect. But, if I click off and back to "Bags" collection, I get the empty error slide out from the right. (An error occurred etc)..

    Past this point, I can't see any products due to crashes - show stopper. In order to even delete it and continue working, I have to call this first, then everything functions properly.

                productService.RemoveDetachedContent(prod, detachedContentType.Key, false );
                productService.Save(prod);
    

    Firebug console is showing a problem with PostGetCollectionEntities.. I wish I knew what Null was attempting to be casted to an Int32..

    "NetworkError: 500 Internal Server Error - http://localhost:51982/umbraco/backoffice/Merchello/EntityCollectionApi/PostGetCollectionEntities"

    {"message":"An error has occurred.","exceptionMessage":"The 'ObjectContent`1' type failed to serialize
     the response body for content type 'application/json; charset=utf-8'.","exceptionType":"System.InvalidOperationException"
    ,"stackTrace":null,"innerException":{"message":"An error has occurred.","exceptionMessage":"Can not convert
     Null to Int32.","exceptionType":"System.ArgumentException","stackTrace":"   at Newtonsoft.Json.Linq
    .JToken.op_Explicit(JToken value)\r\n   at Merchello.Web.Models.ContentEditing.ExamineDisplayExtensions
    .GetProductVariantDetachedContentDisplayCollection(SearchResult result, String alias)\r\n   at Merchello
    .Web.Models.ContentEditing.ExamineDisplayExtensions.ToProductVariantDisplay(SearchResult result)\r\n
       at Merchello.Web.Models.ContentEditing.ExamineDisplayExtensions.ToProductDisplay(SearchResult result
    , Func`2 getProductVariants)\r\n   at Merchello.Web.Search.CachedProductQuery.PerformMapSearchResultToDisplayObject
    (SearchResult result)\r\n   at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()\r\n
       at System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable`1 source)\r\n   at Merchello.Web.Search
    .CachedProductQuery.GetDisplayObject(Guid key)\r\n   at Merchello.Web.Search.QueryResultFactory`1.<BuildQueryResult
    >b__2(Guid x)\r\n   at System.Linq.Enumerable.WhereSelectListIterator`2.MoveNext()\r\n   at Newtonsoft
    .Json.Serialization.JsonSerializerInternalWriter.SerializeList(JsonWriter writer, IEnumerable values
    , JsonArrayContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty
     containerProperty)\r\n   at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue
    (JsonWriter writer, Object value, JsonContract valueContract, JsonProperty member, JsonContainerContract
     containerContract, JsonProperty containerProperty)\r\n   at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter
    .SerializeObject(JsonWriter writer, Object value, JsonObjectContract contract, JsonProperty member, JsonContainerContract
     collectionContract, JsonProperty containerProperty)\r\n   at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter
    .SerializeValue(JsonWriter writer, Object value, JsonContract valueContract, JsonProperty member, JsonContainerContract
     containerContract, JsonProperty containerProperty)\r\n   at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter
    .Serialize(JsonWriter jsonWriter, Object value, Type objectType)\r\n   at Newtonsoft.Json.JsonSerializer
    .SerializeInternal(JsonWriter jsonWriter, Object value, Type objectType)\r\n   at System.Net.Http.Formatting
    .BaseJsonMediaTypeFormatter.WriteToStream(Type type, Object value, Stream writeStream, Encoding effectiveEncoding
    )\r\n   at System.Net.Http.Formatting.JsonMediaTypeFormatter.WriteToStream(Type type, Object value, Stream
     writeStream, Encoding effectiveEncoding)\r\n   at System.Net.Http.Formatting.BaseJsonMediaTypeFormatter
    .WriteToStream(Type type, Object value, Stream writeStream, HttpContent content)\r\n   at System.Net
    .Http.Formatting.BaseJsonMediaTypeFormatter.WriteToStreamAsync(Type type, Object value, Stream writeStream
    , HttpContent content, TransportContext transportContext, CancellationToken cancellationToken)\r\n---
     End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.CompilerServices
    .TaskAwaiter.ThrowForNonSuccess(Task task)\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification
    (Task task)\r\n   at System.Web.Http.WebHost.HttpControllerHandler.<WriteBufferedResponseContentAsync
    >d__1b.MoveNext()"}}
    

    Sorry this is so long!! Any other advice on how to trouble shoot this would be awesome!

    Something is null, when it shouldn't be.. Just not sure how I should go about hunting it down! Or maybe I'm missing something in my product add?

    Thanks in advance! Awesome work Merchello team!

  • M N 125 posts 212 karma points
    Mar 24, 2016 @ 22:04
    M N
    0

    Alright, I figured it out.. Guess I just had to type this out to realize my error.

    The Product Key was "00000-000--000000-0000".. Simply changed the product add to

    productService.CreateProductWithKey("New Product", "423623", 6.99M);
    

    instead of

    productService.CreateProduct("New Product", "423623", 6.99M);
    

    Doesn't really explain the NULL -> Int32 conversion log error, but lesson learned to always assign a key?

  • kapil 3 posts 73 karma points
    Jul 13, 2017 @ 12:57
    kapil
    0

    Problem:1 In detached content section how can we find "detachedContentType".

    Problem:2 By using createproductwith key instead of create product, i am getting the same error in backend that "Request error: The URL returned a 404 (not found): /umbraco/backoffice/UmbracoApi/Media/GetById".

    Problem:3 All works fine and i am able to get all detached properties via getpropertyvalue, issue is whenever i am fetching the product by urlslug got an error of 404 and when i saved the extended content from backend ,it start working.

Please Sign in or register to post replies

Write your reply to:

Draft