{"record":{"id":"e70979f9f5292018","repo":"RocketChat/Rocket.Chat","slug":"error-fallback-department-not-found","errorCode":"error-fallback-department-not-found","errorMessage":"Fallback department not found","messagePattern":"Fallback department not found","errorType":"error_code","errorClass":"Meteor.Error","httpStatus":null,"severity":"error","filePath":"apps/meteor/server/lib/omnichannel/departmentsLib.ts","lineNumber":121,"sourceCode":"\tif (_id && !department) {\n\t\tthrow new Meteor.Error('error-department-not-found', 'Department not found', {\n\t\t\tmethod: 'livechat:saveDepartment',\n\t\t});\n\t}\n\n\tif (fallbackForwardDepartment === _id) {\n\t\tthrow new Meteor.Error(\n\t\t\t'error-fallback-department-circular',\n\t\t\t'Cannot save department. Circular reference between fallback department and department',\n\t\t);\n\t}\n\n\tif (fallbackForwardDepartment) {\n\t\tconst fallbackDep = await LivechatDepartment.findOneById<Pick<ILivechatDepartment, '_id'>>(fallbackForwardDepartment, {\n\t\t\tprojection: { _id: 1 },\n\t\t});\n\t\tif (!fallbackDep) {\n\t\t\tthrow new Meteor.Error('error-fallback-department-not-found', 'Fallback department not found', {\n\t\t\t\tmethod: 'livechat:saveDepartment',\n\t\t\t});\n\t\t}\n\t}\n\n\tconst departmentDB = await LivechatDepartment.createOrUpdateDepartment(_id, departmentData);\n\tif (departmentDB && departmentAgents) {\n\t\tawait updateDepartmentAgents(departmentDB._id, departmentAgents, departmentDB.enabled);\n\t}\n\n\tif (department?.enabled !== departmentData.enabled) {\n\t\tvoid notifyOnLivechatDepartmentAgentChangedByDepartmentId(departmentDB._id, department ? 'updated' : 'inserted');\n\t}\n\n\t// Disable event\n\tif (department?.enabled && !departmentDB?.enabled) {\n\t\tawait callbacks.run('livechat.afterDepartmentDisabled', departmentDB);\n\t\tvoid Apps.self?.triggerEvent(AppEvents.IPostLivechatDepartmentDisabled, { department: departmentDB });","sourceCodeStart":103,"sourceCodeEnd":139,"githubUrl":"https://github.com/RocketChat/Rocket.Chat/blob/b2c16d5842cbe6b69b59bdf6fc5e5f1afcd1f0b0/apps/meteor/server/lib/omnichannel/departmentsLib.ts#L103-L139","documentation":"Thrown by `livechat:saveDepartment` when `fallbackForwardDepartment` is set and survives the self-reference check, but `LivechatDepartment.findOneById(fallbackForwardDepartment, { projection: { _id: 1 } })` returns null. The referenced fallback department must exist at save time; referential integrity is enforced in application code, not by the schema.","triggerScenarios":"Setting a fallback department id that was deleted, is from another environment, or is a typo; deleting a department that others use as fallback and then saving any of those dependents (note removeDepartment only unsets fallback on rooms, so stale references can linger).","commonSituations":"Deleted fallback targets after department cleanup; ids copied between dev and prod databases; concurrent deletes while an edit form is open.","solutions":["Re-open the fallback selector and choose an existing department (refresh the list first)","Clear the fallback field if no fallback is needed","When deleting departments programmatically, also update departments that reference the deleted one as fallback to avoid stale ids"],"exampleFix":"// before\nawait saveDepartment(_id, { ...data, fallbackForwardDepartment: 'dep-old' }); // deleted\n\n// after\nconst fb = await LivechatDepartment.findOneById('dep-old', { projections: { _id: 1 } } as any);\nawait saveDepartment(_id, { ...data, ...(fb ? { fallbackForwardDepartment: fb._id } : {}) });","handlingStrategy":"validation","validationCode":"if (deptData.fallbackForwardDepartment) {\n  const fb = await LivechatDepartment.findOneById(deptData.fallbackForwardDepartment, { projection: { _id: 1 } });\n  if (!fb) throw new Error('Fallback department does not exist');\n}\nawait saveDepartment(_id, deptData);","typeGuard":"const fallbackResolvable = async (fallbackId: string): Promise<boolean> =>\n  (await LivechatDepartment.findOneById(fallbackId, { projection: { _id: 1 } })) != null;","tryCatchPattern":"try {\n  await saveDepartment(_id, deptData);\n} catch (e) {\n  if (isMeteorError(e, 'error-fallback-department-not-found')) {\n    // re-pick fallback from a freshly loaded department list\n  }\n}","preventionTips":["Populate fallback selectors from a live department query, not cached data","When deleting departments, sweep others referencing it as fallback","Validate cross-referenced ids in migrations/imports"],"tags":["omnichannel","departments","not-found","fallback","foreign-key"],"backgroundTag":"dangling-foreign-key","analyzedSha":"b2c16d5842cbe6b69b59bdf6fc5e5f1afcd1f0b0","analyzedAt":"2026-08-18T15:26:39.429Z","contentChangedAt":"2026-08-18T15:26:39.429Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}