{"record":{"id":"641854bbe16d63b0","repo":"RocketChat/Rocket.Chat","slug":"error-unit-not-found-641854","errorCode":"error-unit-not-found","errorMessage":"Error! No Active Business Unit found with id: ${businessUnit}","messagePattern":"Error! No Active Business Unit found with id: (.+?)","errorType":"exception","errorClass":"Meteor.Error","httpStatus":null,"severity":"error","filePath":"apps/meteor/ee/server/lib/omnichannel/unit.ts","lineNumber":15,"sourceCode":"import { LivechatUnit } from '@rocket.chat/models';\nimport { getUnitsFromUser } from '@rocket.chat/omni-core-ee';\nimport { Meteor } from 'meteor/meteor';\n\nimport type { CheckUnitsFromUser } from '../../../../server/api/v1/omnichannel/lib/livechat';\nimport { checkUnitsFromUser } from '../../../../server/api/v1/omnichannel/lib/livechat';\n\ncheckUnitsFromUser.patch(async (_next, { businessUnit, userId }: CheckUnitsFromUser) => {\n\tif (!businessUnit) {\n\t\treturn;\n\t}\n\tconst unitsFromUser = await getUnitsFromUser(userId);\n\tconst unit = await LivechatUnit.findOneById(businessUnit, { projection: { _id: 1 } }, { unitsFromUser });\n\tif (!unit) {\n\t\tthrow new Meteor.Error('error-unit-not-found', `Error! No Active Business Unit found with id: ${businessUnit}`);\n\t}\n});\n","sourceCodeStart":1,"sourceCodeEnd":18,"githubUrl":"https://github.com/RocketChat/Rocket.Chat/blob/b2c16d5842cbe6b69b59bdf6fc5e5f1afcd1f0b0/apps/meteor/ee/server/lib/omnichannel/unit.ts#L1-L18","documentation":"This Enterprise patch on checkUnitsFromUser runs when a businessUnit id is supplied: it computes the units the user may see (getUnitsFromUser(userId)) and calls LivechatUnit.findOneById(businessUnit, { projection: { _id: 1 } }, { unitsFromUser }). The unitsFromUser constraint restricts the lookup to the user's own units, so the throw Meteor.Error('error-unit-not-found', 'Error! No Active Business Unit found with id: ...') means the unit does not exist, is not active, or is outside the user's unit membership.","triggerScenarios":"Any flow routed through checkUnitsFromUser with a businessUnit param that is deleted or disabled, or a valid unit the user has no department membership in - getUnitsFromUser returns the allowed set and findOneById is constrained to it, so both cases produce the same miss.","commonSituations":"A unit was deleted or disabled during a reorganization but users/integrations still reference its id; the user was removed from the unit's departments so the unit drops out of unitsFromUser; unit ids copied between environments (staging vs production) where the unit does not exist.","solutions":["Verify the businessUnit id exists and the unit is active in Administration > Omnichannel, then correct the id being sent.","Ensure the affected user belongs to a department under that unit so getUnitsFromUser includes it.","Clean up stale unit references (user records, external integrations) after deleting or disabling units."],"exampleFix":"// before\nawait runWithUnitCheck({ businessUnit: unitId, userId }); // throws error-unit-not-found\n\n// after - verify the unit is visible to this user before calling\nconst unitsFromUser = await getUnitsFromUser(userId);\nconst unit = await LivechatUnit.findOneById(unitId, { projection: { _id: 1 } }, { unitsFromUser });\nif (!unit) throw new Meteor.Error('error-unit-not-found', `Unit ${unitId} missing, inactive, or not assigned to user`);\nawait runWithUnitCheck({ businessUnit: unitId, userId });","handlingStrategy":"validation","validationCode":"const unitsFromUser = await getUnitsFromUser(userId);\nconst unit = await LivechatUnit.findOneById(businessUnit, { projections: { _id: 1 } }, { unitsFromUser });\nif (!unit) {\n\t// unit missing, inactive, or outside the user's membership; do not proceed with checkUnitsFromUser\n}","typeGuard":null,"tryCatchPattern":"try {\n\tawait runWithUnitCheck({ businessUnit, userId });\n} catch (e: any) {\n\tif (e?.error === 'error-unit-not-found') {\n\t\tthrow new Meteor.Error('error-unit-not-found', `Unit ${businessUnit} is missing, inactive, or not assigned to this user`);\n\t}\n\tthrow e;\n}","preventionTips":["Verify unit ids (exist + active) before persisting them on users or integrations.","Keep users members of a department under the unit they are expected to operate on.","Run cleanup passes to drop references to deleted/disabled units."],"tags":["omnichannel","business-unit","entity-not-found","enterprise"],"backgroundTag":"entity-not-found","analyzedSha":"b2c16d5842cbe6b69b59bdf6fc5e5f1afcd1f0b0","analyzedAt":"2026-08-18T15:26:39.429Z","contentChangedAt":"2026-08-18T15:26:39.429Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}