{"record":{"id":"48b7591470f058f9","repo":"RocketChat/Rocket.Chat","slug":"department-departmentdata-id-not-found","errorCode":null,"errorMessage":"Department ${departmentData._id} not found","messagePattern":"Department (.+?) not found","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"warning","filePath":"apps/meteor/app/apps/server/bridges/listeners.ts","lineNumber":491,"sourceCode":"\t\t\t\tconst [roomId] = args.payload;\n\t\t\t\tconst room = await this.orch.getConverters().get('rooms').convertById(roomId);\n\n\t\t\t\tif (!room) {\n\t\t\t\t\tthrow new Error(`Room with id ${roomId} not found`);\n\t\t\t\t}\n\n\t\t\t\treturn this.orch\n\t\t\t\t\t.getManager()\n\t\t\t\t\t.getListenerManager()\n\t\t\t\t\t.executeListener(args.event, room as IAppsLivechatRoom);\n\t\t\t}\n\n\t\t\tcase AppInterface.IPostLivechatDepartmentDisabled: {\n\t\t\t\tconst [departmentData] = args.payload;\n\t\t\t\tconst department = await this.orch.getConverters().get('departments').convertDepartment(departmentData);\n\n\t\t\t\tif (!department) {\n\t\t\t\t\tthrow new Error(`Department ${departmentData._id} not found`);\n\t\t\t\t}\n\n\t\t\t\treturn this.orch.getManager().getListenerManager().executeListener(args.event, { department });\n\t\t\t}\n\n\t\t\tcase AppInterface.IPostLivechatDepartmentRemoved: {\n\t\t\t\tconst [departmentData] = args.payload;\n\t\t\t\tconst department = await this.orch.getConverters().get('departments').convertDepartment(departmentData);\n\n\t\t\t\tif (!department) {\n\t\t\t\t\tthrow new Error(`Department ${departmentData._id} not found`);\n\t\t\t\t}\n\n\t\t\t\treturn this.orch.getManager().getListenerManager().executeListener(args.event, { department });\n\t\t\t}\n\n\t\t\tdefault:\n\t\t\t\tconst [roomData] = args.payload;","sourceCodeStart":473,"sourceCodeEnd":509,"githubUrl":"https://github.com/RocketChat/Rocket.Chat/blob/f9d3ec372bb580fa8d036f94cf03925a478ef768/apps/meteor/app/apps/server/bridges/listeners.ts#L473-L509","documentation":"Thrown by the IPostLivechatDepartmentDisabled case in livechatEvent when the departments converter's convertDepartment returns null for the supplied departmentData. The bridge will not dispatch the department-disabled event to Apps because the department object could not be converted. This typically means the department record referenced by departmentData._id is inconsistent or already gone.","triggerScenarios":"Server emits IPostLivechatDepartmentDisabled carrying a departmentData payload; convertDepartment(departmentData) resolves falsy, e.g. the underlying LivechatDepartment document was removed, the document shape changed in a version mismatch, or the converter's internal lookup by departmentData._id returns nothing.","commonSituations":"A department is removed immediately after being disabled (disable-then-remove in quick succession); a schema/typing drift between the apps-engine IDepartment and the persisted document; a custom omnichannel migration that strips departments; partial department data passed to the event.","solutions":["Check that the department identified by departmentData._id still exists in LivechatDepartment at processing time.","If disable and remove are issued back-to-back, serialize them so the disabled event drains before removal.","Verify the department payload dispatched to the event matches the current LivechatDepartment schema.","On version upgrades, confirm the departments converter and LivechatDepartment model are from the same release."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"const dept = await LivechatDepartment.findOneById(departmentData._id);\nif (!dept) {\n  return; // do not emit IPostLivechatDepartmentDisabled\n}\nawait emitEvent(AppInterface.IPostLivechatDepartmentDisabled, { payload: [departmentData] });","typeGuard":"const isPersistedDepartment = async (id: string): Promise<boolean> => {\n  return Boolean(await LivechatDepartment.findOneById(id, { projection: { _id: 1 } }));\n};","tryCatchPattern":"try {\n  await listenerManager.executeListener(args.event, { department });\n} catch (e) {\n  this.orch.debugLog(`Skipping ${args.event}: ${(e as Error).message}`);\n}","preventionTips":["Do not remove a department in the same flow that disables it without draining the disabled event first.","Keep the apps-engine IDepartment schema and LivechatDepartment model in sync across upgrades.","When disabling programmatically, pass the full department document in the payload."],"tags":["apps-engine","livechat","event-listener","department","data-integrity"],"backgroundTag":null,"analyzedSha":"f9d3ec372bb580fa8d036f94cf03925a478ef768","analyzedAt":"2026-08-12T19:07:17.372Z","schemaVersion":2},"datasetVersion":"2026-08-12T23:17:12.415Z"}