{"record":{"id":"1a5d7bef310ef7ba","repo":"RocketChat/Rocket.Chat","slug":"no-valid-agent-found","errorCode":null,"errorMessage":"No valid agent found","messagePattern":"No valid agent found","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"apps/meteor/ee/server/hooks/omnichannel/checkAgentBeforeTakeInquiry.ts","lineNumber":54,"sourceCode":"\t\tcbLogger.debug('Provided agent is not online');\n\t\tthrow new Error('Provided agent is not online');\n\t}\n\n\tif (!settings.get('Livechat_waiting_queue')) {\n\t\tcbLogger.info({ msg: 'Chat can be taken by Agent: waiting queue is disabled', agentId });\n\t\treturn agent;\n\t}\n\n\tif (await allowAgentSkipQueue(agent)) {\n\t\tcbLogger.info({ msg: 'Chat can be taken by Agent: agent can skip queue', agentId });\n\t\treturn agent;\n\t}\n\n\tconst { department: departmentId } = inquiry;\n\tconst user = await Users.getAgentAndAmountOngoingChats(agentId, departmentId);\n\tif (!user) {\n\t\tcbLogger.debug({ msg: 'No valid agent found', agentId });\n\t\tthrow new Error('No valid agent found');\n\t}\n\n\tconst { queueInfo: { chats = 0, chatsForDepartment = 0 } = {} } = user;\n\n\tif (await isAgentWithinChatLimits({ agentId, departmentId, totalChats: chats, departmentChats: chatsForDepartment })) {\n\t\treturn user;\n\t}\n\tthrow new Error('error-max-number-simultaneous-chats-reached');\n};\n\ncallbacks.add('livechat.checkAgentBeforeTakeInquiry', validateMaxChats, callbacks.priority.MEDIUM, 'livechat-before-take-inquiry');\n","sourceCodeStart":36,"sourceCodeEnd":66,"githubUrl":"https://github.com/RocketChat/Rocket.Chat/blob/b2c16d5842cbe6b69b59bdf6fc5e5f1afcd1f0b0/apps/meteor/ee/server/hooks/omnichannel/checkAgentBeforeTakeInquiry.ts#L36-L66","documentation":"Plain Error thrown by validateMaxChats on 'livechat.checkAgentBeforeTakeInquiry' when Users.getAgentAndAmountOngoingChats(agentId, departmentId) returns null — the agentId does not resolve to an existing livechat agent user (with queue statistics). This is a data-resolution failure, distinct from capacity: the agent record itself could not be found.","triggerScenarios":"Taking an inquiry with an agentId that was deleted, lacks the livechat-agent role, or is malformed; department-scoped lookup where the agent exists but is not registered as an agent.","commonSituations":"Agent users removed while their inquiries were still queued; agentId taken from stale UI state; agents created as bots without proper roles.","solutions":["Confirm the agent exists and holds the livechat-agent role (Administration > Omnichannel > Agents)","Re-add the user as an omnichannel agent so getAgentAndAmountOngoingChats can resolve them, then retry","If the agent was deleted intentionally, route/forward the inquiry to a valid agent instead"],"exampleFix":"// before: passing an unverified agentId into the take flow\nawait callbacks.run('livechat.checkAgentBeforeTakeInquiry', inquiry, { agentId });\n\n// after: resolve the agent record first, mirroring the hook's lookup\nconst user = await Users.getAgentAndAmountOngoingChats(agentId, inquiry.department);\nif (!user) throw new Error('agentId does not resolve to a livechat agent: ' + agentId);\nawait callbacks.run('livechat.checkAgentBeforeTakeInquiry', inquiry, { agentId });","handlingStrategy":"validation","validationCode":"const agentResolves = async (agentId: string, departmentId?: string): Promise<boolean> =>\n  Boolean(await Users.getAgentAndAmountOngoingChats(agentId, departmentId));","typeGuard":null,"tryCatchPattern":"try {\n  await takeInquiry(inquiry, { agentId });\n} catch (err: any) {\n  if (err?.message === 'No valid agent found') {\n    // agent deleted or not a livechat agent; reassign the inquiry to a valid agent\n  }\n  throw err;\n}","preventionTips":["Validate agentId against the livechat agents collection in automation code","Clean up queued inquiries when deleting agent users","Ensure agents are created through the omnichannel agents API, not raw user inserts"],"tags":["omnichannel","livechat","agent-not-found","data-integrity"],"backgroundTag":"agent-record-not-found","analyzedSha":"b2c16d5842cbe6b69b59bdf6fc5e5f1afcd1f0b0","analyzedAt":"2026-08-18T15:26:39.429Z","contentChangedAt":"2026-08-18T15:26:39.429Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}