{"record":{"id":"23f45e854804098a","repo":"RocketChat/Rocket.Chat","slug":"business-hour-department-businesshourid-not-fou","errorCode":null,"errorMessage":"Business hour ${department.businessHourId} not found","messagePattern":"Business hour (.+?) not found","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"apps/meteor/ee/server/lib/omnichannel/business-hour/Multiple.ts","lineNumber":206,"sourceCode":"\t\tconst defaultBH = await this.BusinessHourRepository.findOneDefaultBusinessHour();\n\t\tif (!defaultBH) {\n\t\t\tthrow new Error('Default business hour not found');\n\t\t}\n\t\tawait this.UsersRepository.closeAgentsBusinessHoursByBusinessHourIds([businessHour._id, defaultBH._id]);\n\n\t\t// If i'm the only one, disable the business hour\n\t\tconst imTheOnlyOne = !(await LivechatDepartment.countByBusinessHourIdExcludingDepartmentId(businessHour._id, department._id));\n\t\tif (imTheOnlyOne) {\n\t\t\tbhLogger.warn({\n\t\t\t\tmsg: 'onDepartmentDisabled: department is the only one on business hour, disabling it',\n\t\t\t\tdepartmentId: department._id,\n\t\t\t\tbusinessHourId: businessHour._id,\n\t\t\t});\n\t\t\tawait this.BusinessHourRepository.disableBusinessHour(businessHour._id);\n\n\t\t\tbusinessHour = await this.BusinessHourRepository.findOneById(department.businessHourId);\n\t\t\tif (!businessHour) {\n\t\t\t\tthrow new Error(`Business hour ${department.businessHourId} not found`);\n\t\t\t}\n\t\t}\n\n\t\t// start default business hour and this BH if needed\n\t\tif (!settings.get('Livechat_enable_business_hours')) {\n\t\t\treturn;\n\t\t}\n\n\t\tconst businessHourToOpen = await filterBusinessHoursThatMustBeOpened([businessHour, defaultBH]);\n\t\tfor await (const bh of businessHourToOpen) {\n\t\t\tawait openBusinessHour(bh, false);\n\t\t}\n\n\t\tawait makeAgentsUnavailableBasedOnBusinessHour();\n\t\tawait businessHourManager.restartCronJobsIfNecessary();\n\t}\n\n\tasync onDepartmentArchived(department: Pick<ILivechatDepartment, '_id' | 'businessHourId'>): Promise<void> {","sourceCodeStart":188,"sourceCodeEnd":224,"githubUrl":"https://github.com/RocketChat/Rocket.Chat/blob/b2c16d5842cbe6b69b59bdf6fc5e5f1afcd1f0b0/apps/meteor/ee/server/lib/omnichannel/business-hour/Multiple.ts#L188-L224","documentation":"In onDepartmentDisabled, when the disabled department was the last one using the business hour, the hour is disabled and then re-fetched with findOneById(department.businessHourId) before reopening logic. If that re-fetch returns null, it throws 'Business hour ${id} not found'.","triggerScenarios":"Disabling the last department of a business hour while the business-hour record disappears between the disable and the re-fetch (concurrent removal or data inconsistency).","commonSituations":"Concurrent admin actions deleting the same business hour; inconsistent data after partial migration; the department still referencing a deleted business hour id.","solutions":["Ensure departments are unlinked from a business hour before deleting that hour","Clear stale businessHourId references on departments (set to null) so onDepartmentDisabled does not chase missing records","Retry the department-disable after the concurrent business-hour operation settles"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"const bh = await BusinessHourRepository.findOneById(department.businessHourId);\nif (!bh) {\n\t// clear the stale reference: await LivechatDepartment.removeBusinessHourFromDepartmentsByIdsAndBusinessHourId([department._id], department.businessHourId);\n}\nawait onDepartmentDisabled(department);","typeGuard":null,"tryCatchPattern":"try {\n\tawait onDepartmentDisabled(department);\n} catch (err) {\n\tif (err instanceof Error && /^Business hour .+ not found$/.test(err.message)) {\n\t\t// stale businessHourId on the department: clear the reference and retry\n\t} else {\n\t\tthrow err;\n\t}\n}","preventionTips":["Unlink departments from a business hour before deleting the hour","Avoid concurrent admin operations on the same department/business hour pair","Periodically audit departments for dangling businessHourId references"],"tags":["omnichannel","business-hour","department","race-condition","data-integrity"],"backgroundTag":"resource-not-found","analyzedSha":"b2c16d5842cbe6b69b59bdf6fc5e5f1afcd1f0b0","analyzedAt":"2026-08-18T15:26:39.429Z","schemaVersion":2},"datasetVersion":"2026-08-27T19:17:21.184Z"}