{"record":{"id":"cd96829a1a62abc0","repo":"hcengineering/platform","slug":"could-not-find-a-latest-revision-for-training-ob","errorCode":null,"errorMessage":"Could not find a latest revision for training ${object.code}","messagePattern":"Could not find a latest revision for training (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"plugins/training-resources/src/actions/trainingDraftAction.ts","lineNumber":43,"sourceCode":"  async (object: Training) => {\n    const client = getClient()\n    const currentEmployeeRef = getCurrentEmployeeRef()\n\n    // TODO: Move to server plugins when we have them to avoid concurrency\n    const latestRevision = await client.findOne(\n      object._class,\n      {\n        space: object.space,\n        code: object.code\n      },\n      {\n        sort: {\n          revision: SortingOrder.Descending\n        }\n      }\n    )\n    if (latestRevision === undefined) {\n      throw new Error(`Could not find a latest revision for training ${object.code}`)\n    }\n\n    const ops = client.apply()\n    const newTrainingRef = await ops.createDoc(object._class, object.space, {\n      title: object.title,\n      description: object.description,\n      attachments: 0,\n      passingScore: object.passingScore,\n      code: object.code,\n      state: TrainingState.Draft,\n      releasedBy: null,\n      releasedOn: null,\n      questions: 0,\n      requests: 0,\n      revision: latestRevision.revision + 1,\n      owner: currentEmployeeRef,\n      author: currentEmployeeRef\n    })","sourceCodeStart":25,"sourceCodeEnd":61,"githubUrl":"https://github.com/hcengineering/platform/blob/63e28dc96483967b2fc21c881b3f1023c1de7718/plugins/training-resources/src/actions/trainingDraftAction.ts#L25-L61","documentation":"Thrown by the training draft action when a query for the Training document's latest revision (sorted by revision descending) returns nothing. The library requires an existing latest revision to base the new draft/revision creation on; without it the operation cannot proceed safely. It is a data/model invariant violation, not a client error.","triggerScenarios":"Calling the draft creation action for a training whose code exists but which has no revision documents attached (no Sequences/revisions created yet, revisions deleted, or wrong class/space queried).","commonSituations":"Running against a test/dev database where revisions were never seeded; a migration that dropped revision objects; querying with an incorrect attachedTo class or space filter; the training was created without an initial revision step.","solutions":["Ensure an initial revision exists for the training before invoking the draft action (create the base revision document).","Verify the training's code matches an actual Training document with revisions in the target space.","Check the lookup query filters (attachedTo class, space, sorting on revision) against your data model.","Restore missing revision data from backup or re-run the seeding/migration that generates revisions."],"exampleFix":"// before\nconst ops = client.apply() // assumes latestRevision exists\n// after\nif (latestRevision === undefined) {\n  await client.createDoc(training.class.Revision, object.space, { attachedTo: object._id, revision: 1 })\n}\nconst ops = client.apply()","handlingStrategy":"validation","validationCode":"const latest = await client.findOne(training.class.Revision, { attachedTo: object._id }, { sort: { revision: SortingOrder.Descending } })\nif (latest === undefined) throw new Error(`Training ${object.code} has no revisions; seed an initial revision first`)","typeGuard":"function hasLatestRevision(r: Revision | undefined): r is Revision { return r !== undefined }","tryCatchPattern":"try { await runDraftAction(object) } catch (e) { if (e.message.includes('latest revision')) await seedInitialRevision(object); else throw e }","preventionTips":["Seed an initial revision whenever a training is created","Re-run model seeding migrations on new workspaces","Verify revision data before batch draft operations"],"tags":["data-integrity","training","missing-object"],"backgroundTag":"missing-revision-record","analyzedSha":"63e28dc96483967b2fc21c881b3f1023c1de7718","analyzedAt":"2026-08-29T15:21:27.377Z","schemaVersion":2},"datasetVersion":"2026-08-29T17:17:51.833Z"}