{"record":{"id":"379c358e3a59c9fe","repo":"RocketChat/Rocket.Chat","slug":"error-user-not-belong-to-department","errorCode":"error-user-not-belong-to-department","errorMessage":"error-user-not-belong-to-department","messagePattern":"error-user-not-belong-to-department","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"apps/meteor/server/lib/omnichannel/Helper.ts","lineNumber":678,"sourceCode":"\t\t\tmsg: 'Forwarding room to department and user',\n\t\t\troomId: room._id,\n\t\t\tdepartmentId,\n\t\t\tagentId,\n\t\t});\n\t\tconst user = await Users.findOneOnlineAgentById(\n\t\t\tagentId,\n\t\t\tsettings.get<boolean>('Livechat_enabled_when_agent_idle'),\n\t\t\tsettings.get<boolean>('Livechat_accept_chats_with_no_agents'),\n\t\t\t{},\n\t\t);\n\t\tif (!user) {\n\t\t\tthrow new Error('error-user-is-offline');\n\t\t}\n\t\tconst isInDepartment = await LivechatDepartmentAgents.findOneByAgentIdAndDepartmentId(agentId, departmentId, {\n\t\t\tprojection: { _id: 1 },\n\t\t});\n\t\tif (!isInDepartment) {\n\t\t\tthrow new Error('error-user-not-belong-to-department');\n\t\t}\n\t\tconst { username } = user;\n\t\tagent = { agentId, username };\n\t}\n\n\tconst department = await LivechatDepartment.findOneById<\n\t\tPick<ILivechatDepartment, 'allowReceiveForwardOffline' | 'fallbackForwardDepartment' | 'name'>\n\t>(departmentId, {\n\t\tprojection: {\n\t\t\tallowReceiveForwardOffline: 1,\n\t\t\tfallbackForwardDepartment: 1,\n\t\t\tname: 1,\n\t\t},\n\t});\n\n\t// Cases:\n\t// 1. Routing is manual\n\t// 2. Server is out of macs","sourceCodeStart":660,"sourceCodeEnd":696,"githubUrl":"https://github.com/RocketChat/Rocket.Chat/blob/b2c16d5842cbe6b69b59bdf6fc5e5f1afcd1f0b0/apps/meteor/server/lib/omnichannel/Helper.ts#L660-L696","documentation":"When forwarding to department+agent, LivechatDepartmentAgents.findOneByAgentIdAndDepartmentId must find the agent inside the target department; otherwise it throws error-user-not-belong-to-department. It fires after the online check, so the agent exists and is online but is not assigned to the destination department.","triggerScenarios":"Forward payload pairs the userId of an agent never added to that department, or removed from it after a reorg; department membership changed concurrently with the transfer.","commonSituations":"New agents not yet added to departments; admin removed an agent from the department; agent pickers listing agents across all departments without filtering by the chosen target.","solutions":["Choose an agent assigned to the target department (Omnichannel -> Departments -> agents)","Add the agent to the department first, then retry the forward","UI: filter the agent picker by the selected department"],"exampleFix":"// before\nawait forwardRoomToDepartment(room, { departmentId, userId: anyOnlineAgent._id }, guest);\n\n// after\nconst inDept = await LivechatDepartmentAgents.findOneByAgentIdAndDepartmentId(agentId, departmentId, { projection: { _id: 1 } });\nif (!inDept) throw new Error('error-user-not-belong-to-department');\nawait forwardRoomToDepartment(room, { departmentId, userId: agentId }, guest);","handlingStrategy":"validation","validationCode":"const inDept = await LivechatDepartmentAgents.findOneByAgentIdAndDepartmentId(agentId, departmentId, {\n  projection: { _id: 1 },\n});\nif (!inDept) {\n  // pick an agent who belongs to the target department\n}","typeGuard":null,"tryCatchPattern":"try {\n  await forwardRoomToDepartment(room, transferData, guest);\n} catch (err) {\n  if (err instanceof Error && err.message === 'error-user-not-belong-to-department') {\n    // add the agent to the department or choose a member agent, then retry\n  } else throw err;\n}","preventionTips":["Filter agent pickers by the selected department","Keep department membership in sync when agents change teams"],"tags":["omnichannel","livechat","transfer","department","membership","rocket-chat"],"backgroundTag":"group-membership-missing","analyzedSha":"b2c16d5842cbe6b69b59bdf6fc5e5f1afcd1f0b0","analyzedAt":"2026-08-18T15:26:39.429Z","schemaVersion":2},"datasetVersion":"2026-08-21T13:17:26.733Z"}