{"record":{"id":"d01ececd643a21a3","repo":"RocketChat/Rocket.Chat","slug":"error-invalid-department-d01ece","errorCode":"error-invalid-department","errorMessage":"error-invalid-department","messagePattern":"error-invalid-department","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"apps/meteor/server/lib/omnichannel/transfer.ts","lineNumber":77,"sourceCode":"\t\tawait transfer(room, guest, {\n\t\t\ttransferredBy,\n\t\t\tdepartmentId: guest.department,\n\t\t});\n\t}\n}\n\nexport async function transfer(room: IOmnichannelRoom, guest: ILivechatVisitor, transferData: TransferData) {\n\tlivechatLogger.debug({ msg: 'Transferring room', roomId: room._id, transferredBy: transferData?.transferredBy?._id });\n\tif (room.onHold) {\n\t\tthrow new Error('error-room-onHold');\n\t}\n\n\tif (transferData.departmentId) {\n\t\tconst department = await LivechatDepartment.findOneById<Pick<ILivechatDepartment, 'name' | '_id'>>(transferData.departmentId, {\n\t\t\tprojection: { name: 1 },\n\t\t});\n\t\tif (!department) {\n\t\t\tthrow new Error('error-invalid-department');\n\t\t}\n\n\t\ttransferData.department = department;\n\t\tlivechatLogger.debug({ msg: 'Transferring room to department', roomId: room._id, departmentId: transferData.department?._id });\n\t}\n\n\treturn RoutingManager.transferRoom(room, guest, transferData);\n}\n","sourceCodeStart":59,"sourceCodeEnd":86,"githubUrl":"https://github.com/RocketChat/Rocket.Chat/blob/b2c16d5842cbe6b69b59bdf6fc5e5f1afcd1f0b0/apps/meteor/server/lib/omnichannel/transfer.ts#L59-L86","documentation":"transfer() validates transferData.departmentId when present: it loads LivechatDepartment.findOneById with a name projection, and if no department document matches it throws Error('error-invalid-department'). The department must exist at transfer time because its name is attached to the transfer data shown to agents/visitors.","triggerScenarios":"Transferring a livechat room to a departmentId that has no LivechatDepartment document: a deleted department, a wrong/archived id from a stale UI list, or an id typo in API calls.","commonSituations":"Departments deleted while old transfer dialogs stayed open, clients caching department lists, integrations passing department ids from another environment, or transfers targeting archived departments.","solutions":["List current departments (livechat departments API / Administration > Omnichannel > Departments) and use a valid _id","Refresh the department list in the transfer dialog before submitting","If the department was deleted intentionally, update saved transfer targets/defaults that still reference it","Verify the exact id spelling (department _id, not name) in API payloads"],"exampleFix":"// before\nawait transfer(room, guest, { departmentId, transferredBy });\n\n// after\nconst department = await LivechatDepartment.findOneById(departmentId, { projection: { name: 1 } });\nif (!department) throw new Meteor.Error('error-invalid-department');\nawait transfer(room, guest, { departmentId, transferredBy });","handlingStrategy":"validation","validationCode":"import { LivechatDepartment } from '@rocket.chat/models';\n\nconst department = await LivechatDepartment.findOneById(departmentId, { projection: { name: 1 } });\nif (!department) {\n  throw new Meteor.Error('error-invalid-department', `Department ${departmentId} does not exist`);\n}\nawait transfer(room, guest, { ...transferData, departmentId });","typeGuard":null,"tryCatchPattern":"try {\n  await transfer(room, guest, transferData);\n} catch (err) {\n  if (err instanceof Error && err.message === 'error-invalid-department') {\n    // refresh the department list and make the agent re-pick\n  } else {\n    throw err;\n  }\n}","preventionTips":["Refresh the department list when opening the transfer dialog","Remove deleted departments from saved defaults and integration configs","Validate department ids at the API boundary before they reach the transfer flow"],"tags":["omnichannel","transfer","department","validation","entity-not-found"],"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-14T05:17:10.506Z"}