{"record":{"id":"1b4484b2eb1ae179","repo":"nopSolutions/nopCommerce","slug":"failed-to-retrieve-company","errorCode":null,"errorMessage":"Failed to retrieve company","messagePattern":"Failed to retrieve company","errorType":"exception","errorClass":"NopException","httpStatus":null,"severity":"error","filePath":"src/Plugins/Nop.Plugin.Tax.Avalara/Services/AvalaraTaxManager.cs","lineNumber":962,"sourceCode":"\n    /// <summary>\n    /// Export current tax codes to Avalara services\n    /// </summary>\n    /// <returns>\n    /// A task that represents the asynchronous operation\n    /// The task result contains the number of exported tax codes; null in case of error\n    /// </returns>\n    public async Task<int?> ExportTaxCodesAsync()\n    {\n        return (await HandleFunctionAsync<int?>(async () =>\n        {\n            if (string.IsNullOrEmpty(_avalaraTaxSettings.CompanyCode) || _avalaraTaxSettings.CompanyCode.Equals(Guid.Empty.ToString()))\n                throw new NopException(\"Company not selected\");\n\n            //get selected company\n            var selectedCompany = (await GetAccountCompaniesAsync())\n                ?.FirstOrDefault(company => _avalaraTaxSettings.CompanyCode.Equals(company?.companyCode))\n                ?? throw new NopException(\"Failed to retrieve company\");\n\n            //get existing tax codes (only active)\n            var taxCodes = await ServiceClient.ListTaxCodesByCompanyAsync(selectedCompany.id, \"isActive eq true\", null, null, null, null)\n                ?? throw new NopException(\"No response from the service\");\n\n            var existingTaxCodes = taxCodes.value?.Select(taxCode => taxCode.taxCode).ToList() ?? new List<string>();\n\n            //prepare tax codes to export\n            var taxCodesToExport = await (await _taxCategoryService.GetAllTaxCategoriesAsync()).SelectAwait(async taxCategory => new TaxCodeModel\n            {\n                createdDate = DateTime.UtcNow,\n                description = CommonHelper.EnsureMaximumLength(taxCategory.Name, 255),\n                isActive = true,\n                taxCode = CommonHelper.EnsureMaximumLength(taxCategory.Name, 25),\n                taxCodeTypeId = CommonHelper.EnsureMaximumLength(await _genericAttributeService\n                    .GetAttributeAsync<string>(taxCategory, AvalaraTaxDefaults.TaxCodeTypeAttribute) ?? \"P\", 2)\n            }).Where(taxCode => !string.IsNullOrEmpty(taxCode.taxCode)).ToListAsync();\n","sourceCodeStart":944,"sourceCodeEnd":980,"githubUrl":"https://github.com/nopSolutions/nopCommerce/blob/64bdf2ff08c8b39e65717bcf974fb43dc2ef68f2/src/Plugins/Nop.Plugin.Tax.Avalara/Services/AvalaraTaxManager.cs#L944-L980","documentation":"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.","triggerScenarios":"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.","commonSituations":"Company deleted or its code changed in Avalara; account switched; stale cached settings; company was selected against a different (sandbox vs production) account.","solutions":["Re-fetch companies ('Get account companies') and reselect the correct company.","Verify the company still exists in the Avalara admin.","Ensure the credentials point at the expected account (sandbox vs production).","Save settings and retry the export."],"exampleFix":"// before\nvar selectedCompany = (await GetAccountCompaniesAsync())\n    ?.FirstOrDefault(company => _avalaraTaxSettings.CompanyCode.Equals(company?.companyCode))\n    ?? throw new NopException(\"Failed to retrieve company\");\n\n// after - list available codes to aid recovery\nvar companies = await GetAccountCompaniesAsync();\nvar selectedCompany = companies?.FirstOrDefault(c => _avalaraTaxSettings.CompanyCode.Equals(c?.companyCode))\n    ?? throw new NopException($\"Failed to retrieve company '{_avalaraTaxSettings.CompanyCode}'. Available codes: {string.Join(\", \", companies?.Select(c => c.companyCode) ?? Array.Empty<string>())}\");","handlingStrategy":"validation","validationCode":"// Verify the stored code still exists before exporting\nvar companies = await taxManager.GetAccountCompaniesAsync();\nvar exists = companies?.Any(c => _avalaraTaxSettings.CompanyCode.Equals(c?.companyCode)) ?? false;\nif (!exists)\n    throw new NopException($\"Stored company '{_avalaraTaxSettings.CompanyCode}' no longer exists.\");\nawait taxManager.ExportTaxCodesAsync();","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Re-fetch companies after any account/credential change.","Revalidate the selected company before each export.","Prompt reselection when the stored code is missing from the live list."],"tags":["avalara","company","config"],"backgroundTag":null,"analyzedSha":"64bdf2ff08c8b39e65717bcf974fb43dc2ef68f2","analyzedAt":"2026-08-13T21:19:38.062Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}