nopSolutions/nopCommerce · error · NopException

Failed to retrieve company

Error message

Failed to retrieve company

What it means

After confirming a CompanyCode is set, ExportTaxCodesAsync looks it up among the companies returned by GetAccountCompaniesAsync. If no company's companyCode matches the stored value, it throws 'Failed to retrieve company'. The stored code is stale relative to what Avalara reports.

Source

Thrown at src/Plugins/Nop.Plugin.Tax.Avalara/Services/AvalaraTaxManager.cs:962

    /// <summary>
    /// Export current tax codes to Avalara services
    /// </summary>
    /// <returns>
    /// A task that represents the asynchronous operation
    /// The task result contains the number of exported tax codes; null in case of error
    /// </returns>
    public async Task<int?> ExportTaxCodesAsync()
    {
        return (await HandleFunctionAsync<int?>(async () =>
        {
            if (string.IsNullOrEmpty(_avalaraTaxSettings.CompanyCode) || _avalaraTaxSettings.CompanyCode.Equals(Guid.Empty.ToString()))
                throw new NopException("Company not selected");

            //get selected company
            var selectedCompany = (await GetAccountCompaniesAsync())
                ?.FirstOrDefault(company => _avalaraTaxSettings.CompanyCode.Equals(company?.companyCode))
                ?? throw new NopException("Failed to retrieve company");

            //get existing tax codes (only active)
            var taxCodes = await ServiceClient.ListTaxCodesByCompanyAsync(selectedCompany.id, "isActive eq true", null, null, null, null)
                ?? throw new NopException("No response from the service");

            var existingTaxCodes = taxCodes.value?.Select(taxCode => taxCode.taxCode).ToList() ?? new List<string>();

            //prepare tax codes to export
            var taxCodesToExport = await (await _taxCategoryService.GetAllTaxCategoriesAsync()).SelectAwait(async taxCategory => new TaxCodeModel
            {
                createdDate = DateTime.UtcNow,
                description = CommonHelper.EnsureMaximumLength(taxCategory.Name, 255),
                isActive = true,
                taxCode = CommonHelper.EnsureMaximumLength(taxCategory.Name, 25),
                taxCodeTypeId = CommonHelper.EnsureMaximumLength(await _genericAttributeService
                    .GetAttributeAsync<string>(taxCategory, AvalaraTaxDefaults.TaxCodeTypeAttribute) ?? "P", 2)
            }).Where(taxCode => !string.IsNullOrEmpty(taxCode.taxCode)).ToListAsync();

View on GitHub (pinned to 64bdf2ff08)

Solutions

  1. Re-fetch companies ('Get account companies') and reselect the correct company.
  2. Verify the company still exists in the Avalara admin.
  3. Ensure the credentials point at the expected account (sandbox vs production).
  4. Save settings and retry the export.

Example fix

// before
var selectedCompany = (await GetAccountCompaniesAsync())
    ?.FirstOrDefault(company => _avalaraTaxSettings.CompanyCode.Equals(company?.companyCode))
    ?? throw new NopException("Failed to retrieve company");

// after - list available codes to aid recovery
var companies = await GetAccountCompaniesAsync();
var selectedCompany = companies?.FirstOrDefault(c => _avalaraTaxSettings.CompanyCode.Equals(c?.companyCode))
    ?? throw new NopException($"Failed to retrieve company '{_avalaraTaxSettings.CompanyCode}'. Available codes: {string.Join(", ", companies?.Select(c => c.companyCode) ?? Array.Empty<string>())}");
Defensive patterns

Strategy: validation

Validate before calling

// Verify the stored code still exists before exporting
var companies = await taxManager.GetAccountCompaniesAsync();
var exists = companies?.Any(c => _avalaraTaxSettings.CompanyCode.Equals(c?.companyCode)) ?? false;
if (!exists)
    throw new NopException($"Stored company '{_avalaraTaxSettings.CompanyCode}' no longer exists.");
await taxManager.ExportTaxCodesAsync();

Prevention

When it happens

Trigger: GetAccountCompaniesAsync returned companies, but none has a companyCode equal to _avalaraTaxSettings.CompanyCode - the company was deleted, renamed, or the credentials now point at a different account.

Common situations: Company deleted or its code changed in Avalara; account switched; stale cached settings; company was selected against a different (sandbox vs production) account.

Related errors


AI-assisted analysis of nopSolutions/nopCommerce@64bdf2ff08 (2026-08-13). Data as JSON: /api/errors/1b4484b2eb1ae179. Report an issue: GitHub.