{"record":{"id":"e007737532c84278","repo":"RocketChat/Rocket.Chat","slug":"invalid-department","errorCode":"invalid-department","errorMessage":"Provided department does not exists","messagePattern":"Provided department does not exists","errorType":"error_code","errorClass":"Meteor.Error","httpStatus":null,"severity":"error","filePath":"apps/meteor/server/lib/omnichannel/departmentsLib.ts","lineNumber":204,"sourceCode":") {\n\tconst department = await LivechatDepartment.findOneById<Pick<ILivechatDepartment, 'enabled'>>(_id, { projection: { enabled: 1 } });\n\tif (!department) {\n\t\tthrow new Meteor.Error('error-department-not-found', 'Department not found');\n\t}\n\n\treturn updateDepartmentAgents(_id, departmentAgents, department.enabled);\n}\n\nexport async function setDepartmentForGuest({ visitorId, department }: { visitorId: string; department: string }) {\n\tlivechatLogger.debug({\n\t\tmsg: 'Switching departments for visitor',\n\t\tvisitorId,\n\t\tdepartment,\n\t});\n\n\tconst dep = await LivechatDepartment.findOneById<Pick<ILivechatDepartment, '_id'>>(department, { projection: { _id: 1 } });\n\tif (!dep) {\n\t\tthrow new Meteor.Error('invalid-department', 'Provided department does not exists');\n\t}\n\n\t// Visitor is already validated at this point\n\treturn LivechatVisitors.updateDepartmentById(visitorId, department);\n}\n\nexport async function removeDepartment(departmentId: string) {\n\tlivechatLogger.debug({ msg: 'Removing department', departmentId });\n\n\tconst department = await LivechatDepartment.findOneById<Pick<ILivechatDepartment, '_id' | 'businessHourId' | 'parentId'>>(departmentId, {\n\t\tprojection: { _id: 1, businessHourId: 1, parentId: 1 },\n\t});\n\tif (!department) {\n\t\tthrow new Error('error-department-not-found');\n\t}\n\n\tconst { _id } = department;\n","sourceCodeStart":186,"sourceCodeEnd":222,"githubUrl":"https://github.com/RocketChat/Rocket.Chat/blob/b2c16d5842cbe6b69b59bdf6fc5e5f1afcd1f0b0/apps/meteor/server/lib/omnichannel/departmentsLib.ts#L186-L222","documentation":"Thrown by `setDepartmentForGuest({ visitorId, department })` when `LivechatDepartment.findOneById(department, { projection: { _id: 1 } })` returns null. Before switching a visitor to a new department (LivechatVisitors.updateDepartmentById), the target department must exist.","triggerScenarios":"Livechat widget/API flows that assign a department to a visitor using an id or name slug that no longer resolves — e.g. a widget still embedding a department deleted after deployment, or a department selector defaulting to a stale value.","commonSituations":"Widget snippets hard-coding a department; departments renamed/recreated (new _id) while clients cache the old one; preview environments pointing at prod department ids.","solutions":["Fetch the current department list (`GET /v1/livechat/department`) and use a valid id before assigning","Make widget department configuration dynamic instead of hard-coded","If the department was deleted, pick a fallback existing department or omit department to use routing defaults"],"exampleFix":"// before\nawait setDepartmentForGuest({ visitorId, department: 'dep-gone' });\n\n// after\nconst dep = await LivechatDepartment.findOneByIdOrName('sales', { projection: { _id: 1 } });\nif (!dep) throw new Error('Configure a valid department for the widget');\nawait setDepartmentForGuest({ visitorId, department: dep._id });","handlingStrategy":"validation","validationCode":"const dep = await LivechatDepartment.findOneByIdOrName(department, { projection: { _id: 1 } });\nif (!dep) throw new Error('Invalid department configured');\nawait setDepartmentForGuest({ visitorId, department: dep._id });","typeGuard":"const isValidDepartment = async (idOrName: string): Promise<boolean> =>\n  (await LivechatDepartment.findOneByIdOrName(idOrName, { projection: { _id: 1 } })) != null;","tryCatchPattern":"try {\n  await setDepartmentForGuest({ visitorId, department });\n} catch (e) {\n  if (isMeteorError(e, 'invalid-department')) {\n    // fall back to default routing (omit department)\n  }\n}","preventionTips":["Fetch departments dynamically for widget selectors","Re-validate embedded department ids after workspace reorganizations","On failure, degrade to no-department routing instead of blocking the visitor"],"tags":["omnichannel","departments","visitor","not-found","livechat"],"backgroundTag":"entity-not-found","analyzedSha":"b2c16d5842cbe6b69b59bdf6fc5e5f1afcd1f0b0","analyzedAt":"2026-08-18T15:26:39.429Z","schemaVersion":2},"datasetVersion":"2026-08-27T19:17:21.184Z"}