{"record":{"id":"0039202f5c574ef8","repo":"RocketChat/Rocket.Chat","slug":"error-department-not-found","errorCode":"error-department-not-found","errorMessage":"Department not found","messagePattern":"Department not found","errorType":"error_code","errorClass":"Meteor.Error","httpStatus":null,"severity":"error","filePath":"apps/meteor/server/lib/omnichannel/departmentsLib.ts","lineNumber":104,"sourceCode":"\tcheck(\n\t\tdepartmentAgents,\n\t\tMatch.Maybe({\n\t\t\tupsert: Match.Maybe(Array),\n\t\t\tremove: Match.Maybe(Array),\n\t\t}),\n\t);\n\n\tconst { requestTagBeforeClosingChat, chatClosingTags, fallbackForwardDepartment } = departmentData;\n\tif (requestTagBeforeClosingChat && (!chatClosingTags || chatClosingTags.length === 0)) {\n\t\tthrow new Meteor.Error(\n\t\t\t'error-validating-department-chat-closing-tags',\n\t\t\t'At least one closing tag is required when the department requires tag(s) on closing conversations.',\n\t\t\t{ method: 'livechat:saveDepartment' },\n\t\t);\n\t}\n\n\tif (_id && !department) {\n\t\tthrow new Meteor.Error('error-department-not-found', 'Department not found', {\n\t\t\tmethod: 'livechat:saveDepartment',\n\t\t});\n\t}\n\n\tif (fallbackForwardDepartment === _id) {\n\t\tthrow new Meteor.Error(\n\t\t\t'error-fallback-department-circular',\n\t\t\t'Cannot save department. Circular reference between fallback department and department',\n\t\t);\n\t}\n\n\tif (fallbackForwardDepartment) {\n\t\tconst fallbackDep = await LivechatDepartment.findOneById<Pick<ILivechatDepartment, '_id'>>(fallbackForwardDepartment, {\n\t\t\tprojection: { _id: 1 },\n\t\t});\n\t\tif (!fallbackDep) {\n\t\t\tthrow new Meteor.Error('error-fallback-department-not-found', 'Fallback department not found', {\n\t\t\t\tmethod: 'livechat:saveDepartment',","sourceCodeStart":86,"sourceCodeEnd":122,"githubUrl":"https://github.com/RocketChat/Rocket.Chat/blob/b2c16d5842cbe6b69b59bdf6fc5e5f1afcd1f0b0/apps/meteor/server/lib/omnichannel/departmentsLib.ts#L86-L122","documentation":"Thrown by `livechat:saveDepartment` when an `_id` was supplied (edit path) but `LivechatDepartment.findOneById(_id)` (executed earlier in saveDepartment) returned null. The method treats a supplied id as a promise the document exists; a missing one aborts before `createOrUpdateDepartment` could accidentally insert with a caller-chosen id.","triggerScenarios":"`livechat:saveDepartment` / REST `PUT /v1/livechat/department/{_id}` where the id was deleted in another session, is a typo/truncated string, or comes from an environment that was re-seeded and no longer has that document.","commonSituations":"Editing a department in two admin tabs where one deletes it; stale department ids cached by integrations after a database restore; copy-paste of ids between environments (dev/prod).","solutions":["Re-fetch the department list (`GET /v1/livechat/department`) and retry with the current id, or create a new department if it was deleted","Guard the save with an existence check (`LivechatDepartment.findOneById`) and treat this error as 'deleted elsewhere' in the UI","For integrations, stop hard-coding department ids; resolve by name at runtime"],"exampleFix":"// before\nawait saveDepartment(staleId, deptData);\n\n// after\nconst dep = await LivechatDepartment.findOneById(staleId, { projection: { _id: 1 } });\nif (!dep) throw new Error('Department was deleted; reload the list');\nawait saveDepartment(staleId, deptData);","handlingStrategy":"validation","validationCode":"const dep = await LivechatDepartment.findOneById(_id, { projection: { _id: 1 } });\nif (!dep) throw new Error('Department no longer exists — reload');\nawait saveDepartment(_id, deptData);","typeGuard":"const departmentExists = async (id: string): Promise<boolean> =>\n  (await LivechatDepartment.findOneById(id, { projection: { _id: 1 } })) != null;","tryCatchPattern":"try {\n  await saveDepartment(_id, deptData);\n} catch (e) {\n  if (isMeteorError(e, 'error-department-not-found')) {\n    // refresh list; offer create-new flow\n  }\n}","preventionTips":["Resolve department ids by name at runtime instead of caching them","Reload the row being edited before saving in multi-admin workspaces","Return 404 semantics from wrappers when this code appears"],"tags":["omnichannel","departments","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"}