Import Products throws Unhandled Exception in Category Mappings

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
5 anos atrás
Nop Version: 4.0

When importing products from XLSX, the ImportManger class (ImportProductsFromXlsx function), will generate an unhandled exception if the category hierarchy does not match, but the last child category name does match.

For example, if my excel file contains this category entry: "Body >> Bumper >> Bumper Grilles"
However, the correct value should be: "Body Parts >> Bumper >> Bumper Grilles"

The code at line 1343 will throw an unhandled exception: Sequence contains no matching element

This is the current code:
var importedCategories = categoryNames.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries).Select(x => allCategories.ContainsKey(x.Trim()) ? allCategories[x.Trim()].Id : allCategories.Values.First(c => c.Name == x.Trim()).Id).ToList();

This happens because:
allCategories.ContainsKey(x.Trim())

Will return false, then execute this condition:
allCategories.Values.First(c => c.Name == x.Trim()).Id
Which will fail, since the string will not be found, and throw a "Sequence contains no matching element" error.

We need to gracefully handle this error and report what the error is to the end user, in this case the fully qualified category path was not found the it the list of all allCategories.
5 anos atrás
I made some changes to the code to give a more meaningful error message, and also provide which record in the file is causing the error.  Possibly this can get incorporated into the next version?

Revised code:
var categoryNames = tempProperty.StringValue.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries);

//category mappings
var categories = isNew || !allProductsCategoryIds.ContainsKey(product.Id) ? new int[0] : allProductsCategoryIds[product.Id];
var importedCategories = categoryNames.Select(x =>
        allCategories.ContainsKey(x.Trim()) ? allCategories[x.Trim()].Id :
            allCategories.Values.Any(c => c.Name == x.Trim()) ? allCategories.Values.First(c => c.Name == x.Trim()).Id :
                throw new ArgumentException($"Categories do not contain a match for '{x.Trim()}'")
    ).ToList();
5 anos atrás
Hi Sizzler. Thank you for your help, we implemented your suggestion, please see this commit for more details

Sizzler wrote:
I made some changes to the code to give a more meaningful error message, and also provide which record in the file is causing the error.  Possibly this can get incorporated into the next version?

Revised code:
var categoryNames = tempProperty.StringValue.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries);

//category mappings
var categories = isNew || !allProductsCategoryIds.ContainsKey(product.Id) ? new int[0] : allProductsCategoryIds[product.Id];
var importedCategories = categoryNames.Select(x =>
        allCategories.ContainsKey(x.Trim()) ? allCategories[x.Trim()].Id :
            allCategories.Values.Any(c => c.Name == x.Trim()) ? allCategories.Values.First(c => c.Name == x.Trim()).Id :
                throw new ArgumentException($"Categories do not contain a match for '{x.Trim()}'")
    ).ToList();
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.