{"record":{"id":"72f5690f17d23698","repo":"nopSolutions/nopCommerce","slug":"guests-role-could-not-be-loaded","errorCode":null,"errorMessage":"'Guests' role could not be loaded","messagePattern":"'Guests' role could not be loaded","errorType":"exception","errorClass":"NopException","httpStatus":null,"severity":"error","filePath":"src/Libraries/Nop.Services/Messages/NewsLetterSubscriptionService.cs","lineNumber":267,"sourceCode":"                if (createdToUtc.HasValue)\r\n                    query = query.Where(nls => nls.CreatedOnUtc <= createdToUtc.Value);\r\n                if (storeId > 0)\r\n                    query = query.Where(nls => nls.StoreId == storeId);\r\n                if (subscriptionTypeId > 0)\r\n                    query = query.Where(nls => nls.TypeId == subscriptionTypeId);\r\n                if (isActive.HasValue)\r\n                    query = query.Where(nls => nls.Active == isActive.Value);\r\n                query = query.OrderBy(nls => nls.Email);\r\n\r\n                return query;\r\n            }, pageIndex, pageSize);\r\n\r\n            return subscriptions;\r\n        }\r\n\r\n        //filter by customer role\r\n        var guestRole = await _customerService.GetCustomerRoleBySystemNameAsync(NopCustomerDefaults.GuestsRoleName)\r\n            ?? throw new NopException(\"'Guests' role could not be loaded\");\r\n\r\n        if (guestRole.Id == customerRoleId)\r\n        {\r\n            //guests\r\n            var subscriptions = await _subscriptionRepository.GetAllPagedAsync(query =>\r\n            {\r\n                if (!string.IsNullOrEmpty(email))\r\n                    query = query.Where(nls => nls.Email.Contains(email));\r\n                if (createdFromUtc.HasValue)\r\n                    query = query.Where(nls => nls.CreatedOnUtc >= createdFromUtc.Value);\r\n                if (createdToUtc.HasValue)\r\n                    query = query.Where(nls => nls.CreatedOnUtc <= createdToUtc.Value);\r\n                if (storeId > 0)\r\n                    query = query.Where(nls => nls.StoreId == storeId);\r\n                if (subscriptionTypeId > 0)\r\n                    query = query.Where(nls => nls.TypeId == subscriptionTypeId);\r\n                if (isActive.HasValue)\r\n                    query = query.Where(nls => nls.Active == isActive.Value);\r","sourceCodeStart":249,"sourceCodeEnd":285,"githubUrl":"https://github.com/nopSolutions/nopCommerce/blob/64bdf2ff08c8b39e65717bcf974fb43dc2ef68f2/src/Libraries/Nop.Services/Messages/NewsLetterSubscriptionService.cs#L249-L285","documentation":"Thrown by NewsLetterSubscriptionService when filtering subscriptions by customer role and detecting the requested role is the built-in 'Guests' role. It resolves the Guests role by system name (NopCustomerDefaults.GuestsRoleName) to switch to guest-specific query logic; if the role is absent, the query cannot proceed and throws NopException. A missing Guests role means the base customer-role seeding was not applied.","triggerScenarios":"Calling the newsletter subscription paged query with a customerRoleId equal to the Guests role while the Guests customer role is missing from the DB.","commonSituations":"Database where customer roles were not seeded (incomplete install); migration that removed built-in roles; test fixture that truncated CustomerRole.","solutions":["Ensure the base installer seeded the built-in customer roles (Administrators, Registered, Guests, Vendors).","Verify the Guests role row exists (SystemName == NopCustomerDefaults.GuestsRoleName).","Reinstall customer-role seed data from a clean DB.","Check for a migration that deleted built-in roles."],"exampleFix":"// before\nvar guestRole = await _customerService.GetCustomerRoleBySystemNameAsync(NopCustomerDefaults.GuestsRoleName)\n    ?? throw new NopException(\"'Guests' role could not be loaded\");\n\n// after - degrade gracefully instead of throwing\nvar guestRole = await _customerService.GetCustomerRoleBySystemNameAsync(NopCustomerDefaults.GuestsRoleName);\nif (guestRole is null || guestRole.Id != customerRoleId)\n    return await baseQuery.GetAllPagedAsync(pageIndex, pageSize);","handlingStrategy":"validation","validationCode":"// Confirm the Guests role exists before newsletter role filtering\nvar guestRole = await _customerService.GetCustomerRoleBySystemNameAsync(NopCustomerDefaults.GuestsRoleName);\nif (guestRole is null)\n    throw new InvalidOperationException(\"Built-in 'Guests' customer role is missing; reseed customer roles.\");","typeGuard":"static async Task<bool> GuestsRoleExistsAsync(ICustomerService svc)\n    => await svc.GetCustomerRoleBySystemNameAsync(NopCustomerDefaults.GuestsRoleName) is not null;","tryCatchPattern":"try { await _subscriptionRepository.GetAllPagedAsync(...); }\ncatch (NopException ex) when (ex.Message == \"'Guests' role could not be loaded\")\n{ /* reseed customer roles or fall back to a non-role-filtered query */ }","preventionTips":["Always seed built-in customer roles during installation.","Never truncate CustomerRole between installs.","Verify the Guests role row exists before exposing role-filtered queries."],"tags":["customer-role","newsletter","seeding","nopcommerce"],"backgroundTag":null,"analyzedSha":"64bdf2ff08c8b39e65717bcf974fb43dc2ef68f2","analyzedAt":"2026-08-13T21:19:38.062Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}