{"record":{"id":"7d92d55cc4358719","repo":"RocketChat/Rocket.Chat","slug":"error-department-not-found-7d92d5","errorCode":null,"errorMessage":"error-department-not-found","messagePattern":"error-department-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":100,"sourceCode":"\t\tcanAssignSelfOnlyAgent,\n\t\tcanAssignAnyAgent,\n\t});\n\n\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 (","sourceCodeStart":82,"sourceCodeEnd":118,"githubUrl":"https://github.com/RocketChat/Rocket.Chat/blob/b2c16d5842cbe6b69b59bdf6fc5e5f1afcd1f0b0/apps/meteor/client/views/omnichannel/components/outboundMessage/components/OutboundMessageWizard/forms/RepliesForm/RepliesForm.tsx#L82-L118","documentation":"In the Outbound Message wizard's RepliesForm, submit() re-checks the department: if a departmentId was chosen and (after awaiting an in-flight refetch) the department object is still missing, it throws FormFetchError('error-department-not-found'). The catch calls trigger() so react-hook-form surfaces the departmentId field error (registered via the isErrorDepartment effect). The department lives in Omnichannel Departments.","triggerScenarios":"Advancing the replies step with a selected department whose lookup (GET /omnichannel/department/:id) returned nothing — deleted department, empty refetch result, or query still unresolved at submit time.","commonSituations":"Department removed by an admin while the wizard was open; departmentId persisted from a draft whose department no longer exists; race between slow fetch and submit.","solutions":["Verify the department still exists in Administration > Omnichannel > Departments; re-select it in the form.","Disable submit while the department query is fetching (isFetchingDepartment).","Clear stale departmentId from saved wizard state when its lookup errors."],"exampleFix":"// before\nif (departmentId && !updatedDepartment) {\n  throw new FormFetchError('error-department-not-found');\n}\n\n// after: block submit until department resolves\nconst canSubmit = !isFetchingDepartment && !(departmentId && !department);\n<ActionButton disabled={!canSubmit} onClick={submit} />","handlingStrategy":"validation","validationCode":"const canSubmit = !isFetchingDepartment && !(departmentId && !department);\n// re-select department when its lookup errors","typeGuard":"const hasDepartment = (d: IDepartment | undefined | null): d is IDepartment => Boolean(d?._id);","tryCatchPattern":null,"preventionTips":["Clear stale department selections when departments change.","Verify the department exists before submitting wizard steps.","Disable submit while the department refetch is in flight."],"tags":["rocket-chat","omnichannel","outbound-message","departments"],"backgroundTag":"entity-not-found","analyzedSha":"b2c16d5842cbe6b69b59bdf6fc5e5f1afcd1f0b0","analyzedAt":"2026-08-18T15:26:39.429Z","schemaVersion":2},"datasetVersion":"2026-08-21T13:17:26.733Z"}