{"record":{"id":"782e6f4595154c8f","repo":"bitwarden/server","slug":"one-or-more-collections-could-not-be-found","errorCode":null,"errorMessage":"One or more collections could not be found.","messagePattern":"One or more collections could not be found\\.","errorType":"exception","errorClass":"BadRequestException","httpStatus":400,"severity":"error","filePath":"bitwarden_license/src/Services/Pam/Services/AccessRuleWriteValidator.cs","lineNumber":74,"sourceCode":"        if (siblings.Any(r => r.Id != existingRuleId && string.Equals(r.Name, name, StringComparison.OrdinalIgnoreCase)))\n        {\n            throw new BadRequestException(\"A rule with that name already exists.\");\n        }\n    }\n\n    private async Task<List<Guid>> ValidateCollectionsAsync(Guid organizationId, IEnumerable<Guid> collectionIds,\n        Guid? existingRuleId)\n    {\n        var distinctIds = collectionIds.Distinct().ToList();\n        if (distinctIds.Count == 0)\n        {\n            return distinctIds;\n        }\n\n        var collections = await _collectionRepository.GetManyByManyIdsAsync(distinctIds);\n        if (collections.Count != distinctIds.Count)\n        {\n            throw new BadRequestException(\"One or more collections could not be found.\");\n        }\n\n        if (collections.Any(c => c.OrganizationId != organizationId))\n        {\n            throw new BadRequestException(\"One or more collections do not belong to this organization.\");\n        }\n\n        // Deletes clear Collection.AccessRuleId and the FK forbids dangling links, so any set link points at an\n        // existing rule; only a link to a different rule is a conflict. A rule being created has no id, so for it\n        // any link at all conflicts.\n        if (collections.Any(c => c.AccessRuleId.HasValue && c.AccessRuleId != existingRuleId))\n        {\n            throw new BadRequestException(\"One or more collections are already governed by another access rule.\");\n        }\n\n        return distinctIds;\n    }\n}","sourceCodeStart":56,"sourceCodeEnd":92,"githubUrl":"https://github.com/bitwarden/server/blob/e93b962371d80964556f5590c6615f5160a437a1/bitwarden_license/src/Services/Pam/Services/AccessRuleWriteValidator.cs#L56-L92","documentation":"Thrown by AccessRuleWriteValidator.ValidateCollectionsAsync (line 74) after fetching collections by the submitted IDs. If _collectionRepository.GetManyByManyIdsAsync returns fewer records than the number of distinct collectionIds submitted, one or more collections do not exist in the database.","triggerScenarios":"Calling ValidateAsync with a collectionIds list containing a GUID that has no corresponding Collection row. The fetched list count is compared against distinctIds.Count and they don't match.","commonSituations":"Client sends a stale or deleted collection ID; GUID is copy-pasted incorrectly from another org; collection was deleted between page load and rule save; collection ID belongs to a different database/environment.","solutions":["Verify every collection ID in the request still exists via _collectionRepository.GetManyByManyIdsAsync before calling ValidateAsync.","Remove or replace deleted collection IDs from the request payload.","Ensure the client refreshes its collection list before submitting the access-rule form."],"exampleFix":"// before — blindly pass unverified collection IDs\nvar ids = collectionIdsFromRequest;\nawait _validator.ValidateAsync(orgId, rule, ids);\n// after — pre-validate existence\nvar existing = await _collectionRepository.GetManyByManyIdsAsync(ids.Distinct().ToList());\nif (existing.Count != ids.Distinct().Count())\n    return BadRequest(\"Some collection IDs are invalid or deleted.\");\nawait _validator.ValidateAsync(orgId, rule, ids);","handlingStrategy":"validation","validationCode":"var distinct = collectionIds.Distinct().ToList();\nvar found = await _collectionRepository.GetManyByManyIdsAsync(distinct);\nif (found.Count != distinct.Count)\n{\n    var missing = distinct.Except(found.Select(c => c.Id));\n    return BadRequest($\"Unknown collection IDs: {string.Join(\", \", missing)}\");\n}","typeGuard":null,"tryCatchPattern":"try { await _validator.ValidateAsync(orgId, rule, collectionIds); }\ncatch (BadRequestException ex) when (ex.Message.Contains(\"could not be found\"))\n{ /* refresh collection list, re-submit */ }","preventionTips":["Refresh the client's collection list before showing the access-rule form.","Filter out collection IDs that are no longer present in the UI before submit.","Log missing IDs to help identify stale references."],"tags":["pam","access-rule","collections","validation","data-integrity"],"backgroundTag":null,"analyzedSha":"e93b962371d80964556f5590c6615f5160a437a1","analyzedAt":"2026-08-13T14:22:19.382Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}