{"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/f9d3ec372bb580fa8d036f94cf03925a478ef768/apps/meteor/ee/server/lib/omnichannel/unit.ts#L1-L18","documentation":"Thrown by the checkUnitsFromUser patch in unit.ts:15 — an enterprise extension of the core unit-visibility check. When a businessUnit id is supplied and the user's scoped units (getUnitsFromUser) do not contain a unit with that _id (findOneById returns null within the scope), it aborts. Meteor.Error 'error-unit-not-found' with a descriptive message including the offending businessUnit id.","triggerScenarios":"Any core flow that calls checkUnitsFromUser with a businessUnit id the current user is not permitted to see (e.g. routing a conversation to a unit outside their monitor scope). Triggers during room routing, agent assignment, or department operations that pass a businessUnit context.","commonSituations":"Agent reassigned across business units without having the new unit in their scope; stale businessUnit id stored on a department after the unit was deleted; permission scope narrowed by an admin.","solutions":["Ensure the businessUnit id is in getUnitsFromUser(userId) before triggering the operation.","Reassign the user as a monitor of the target business unit, or clear the businessUnit reference.","Catch Meteor.Error 'error-unit-not-found' at the routing boundary and fall back to unscoped behavior."],"exampleFix":"// before\nawait someOperation({ businessUnit, userId });\n\n// after\nif (businessUnit) {\n  const mine = await getUnitsFromUser(userId);\n  const exists = await LivechatUnit.findOneById(businessUnit, { projection: { _id: 1 } }, { unitsFromUser: mine });\n  if (!exists) throw new Error('businessUnit not in user scope');\n}\nawait someOperation({ businessUnit, userId });","handlingStrategy":"validation","validationCode":"if (businessUnit) {\n  const mine = await getUnitsFromUser(userId);\n  const unit = await LivechatUnit.findOneById(businessUnit, { projection: { _id: 1 } }, { unitsFromUser: mine });\n  if (!unit) throw new Error('businessUnit not in user scope');\n}","typeGuard":"const isUnitInScope = async (businessUnit: string, userId: string) => {\n  const mine = await getUnitsFromUser(userId);\n  return Boolean(await LivechatUnit.findOneById(businessUnit, { projection: { _id: 1 } }, { unitsFromUser: mine }));\n};","tryCatchPattern":"try { await op({ businessUnit, userId }); }\ncatch (e) {\n  if (e instanceof Meteor.Error && e.error === 'error-unit-not-found') { /* clear unit ref */ return; }\n  throw e;\n}","preventionTips":["Keep department.businessUnit ids consistent with monitor scopes.","Reassign monitors when business units are renamed/restructured."],"tags":["omnichannel","business-unit","permissions","patch","meteor-error"],"backgroundTag":null,"analyzedSha":"f9d3ec372bb580fa8d036f94cf03925a478ef768","analyzedAt":"2026-08-12T19:07:17.372Z","schemaVersion":2},"datasetVersion":"2026-08-12T23:17:12.415Z"}