{"record":{"id":"42454b4b81b2e307","repo":"RocketChat/Rocket.Chat","slug":"error-agent-not-found","errorCode":null,"errorMessage":"error-agent-not-found","messagePattern":"error-agent-not-found","errorType":"exception","errorClass":"FormFetchError","httpStatus":null,"severity":"warning","filePath":"apps/meteor/client/views/omnichannel/components/outboundMessage/components/OutboundMessageWizard/forms/RepliesForm/RepliesForm.tsx","lineNumber":104,"sourceCode":"\tuseEffect(() => {\n\t\tisErrorDepartment && trigger('departmentId');\n\t\treturn () => clearErrors('departmentId');\n\t}, [clearErrors, isErrorDepartment, trigger]);\n\n\tconst submit = useStableCallback(async ({ agentId, departmentId }: RepliesFormData) => {\n\t\ttry {\n\t\t\tconst agent = agents?.find((agent) => agent.agentId === agentId);\n\n\t\t\t// Wait if department or agent is still being fetched in background\n\t\t\tconst updatedDepartment =\n\t\t\t\tdepartmentId && isFetchingDepartment ? await refetchDepartment().then((r) => r.data?.department) : department;\n\n\t\t\tif (departmentId && !updatedDepartment) {\n\t\t\t\tthrow new FormFetchError('error-department-not-found');\n\t\t\t}\n\n\t\t\tif (agentId && !agent) {\n\t\t\t\tthrow new FormFetchError('error-agent-not-found');\n\t\t\t}\n\n\t\t\tonSubmit({ departmentId, department: updatedDepartment, agentId, agent });\n\t\t} catch (error) {\n\t\t\tif (error instanceof FormFetchError) {\n\t\t\t\ttrigger();\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tdispatchToastMessage({ type: 'error', message: t('Something_went_wrong') });\n\t\t}\n\t});\n\n\treturn (\n\t\t<Form id={repliesFormId} onSubmit={handleSubmit(submit)} noValidate>\n\t\t\t<Scrollable vertical>\n\t\t\t\t<FieldGroup justifyContent='start' paddingInline={2}>\n\t\t\t\t\t<DepartmentField","sourceCodeStart":86,"sourceCodeEnd":122,"githubUrl":"https://github.com/RocketChat/Rocket.Chat/blob/b2c16d5842cbe6b69b59bdf6fc5e5f1afcd1f0b0/apps/meteor/client/views/omnichannel/components/outboundMessage/components/OutboundMessageWizard/forms/RepliesForm/RepliesForm.tsx#L86-L122","documentation":"Agent counterpart in RepliesForm: submit() looks up the chosen agentId in the already-loaded agents list (agents?.find(a => a.agentId === agentId)); if no entry matches while an agentId is set, it throws FormFetchError('error-agent-not-found'), and the catch triggers form validation to flag the agent field. This catches agents removed from the list (or department) between rendering and submitting.","triggerScenarios":"Submitting the replies step with an agentId that is not present in the fetched agents array for the selected department — agent unassigned/deactivated, list refetched and shorter, or a stale persisted agentId.","commonSituations":"Agent removed from the department or set inactive while the wizard was open; department changed so the agent list no longer contains the previously picked agent; drafts carrying old agent ids.","solutions":["Re-select the agent from the current list for the chosen department.","Ensure the agent is active and assigned to the selected department.","Refetch/reset the agent selection when departmentId changes so the id always belongs to the current list."],"exampleFix":"// before\nconst agent = agents?.find((agent) => agent.agentId === agentId);\nif (agentId && !agent) {\n  throw new FormFetchError('error-agent-not-found');\n}\n\n// after: reset stale selection when the agent list changes\nuseEffect(() => {\n  if (agentId && !agents?.some((a) => a.agentId === agentId)) {\n    setValue('agentId', undefined); // clear stale pick instead of erroring\n  }\n}, [agents, agentId, setValue]);","handlingStrategy":"validation","validationCode":"const agent = agents?.find((a) => a.agentId === agentId);\nif (agentId && !agent) {\n  setValue('agentId', undefined); // clear stale selection before submit\n}","typeGuard":"const isKnownAgent = (id: string | undefined, agents?: { agentId: string }[]): boolean =>\n  !id || Boolean(agents?.some((a) => a.agentId === id));","tryCatchPattern":null,"preventionTips":["Reset agent selection when the department (and thus agent list) changes.","Keep agents active and assigned to the relevant departments.","Validate persisted drafts against current agent lists before reuse."],"tags":["rocket-chat","omnichannel","outbound-message","livechat-agents"],"backgroundTag":"entity-not-found","analyzedSha":"b2c16d5842cbe6b69b59bdf6fc5e5f1afcd1f0b0","analyzedAt":"2026-08-18T15:26:39.429Z","schemaVersion":2},"datasetVersion":"2026-08-21T18:17:14.833Z"}