{"record":{"id":"27c4556d8c0286dc","repo":"hcengineering/platform","slug":"createdocumenttemplatemetadata-ops-commit-faile","errorCode":null,"errorMessage":"createDocumentTemplateMetadata: ops.commit() failed","messagePattern":"createDocumentTemplateMetadata: ops\\.commit\\(\\) failed","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"plugins/controlled-documents/src/docutils.ts","lineNumber":452,"sourceCode":"  })\n\n  const projectDocumentId = await client.addCollection(\n    documents.class.ProjectDocument,\n    space,\n    projectMetaId,\n    documents.class.ProjectMeta,\n    'documents',\n    {\n      project: projectId,\n      initial: projectId,\n      document: templateId\n    }\n  )\n\n  const success = await ops.commit()\n\n  if (!success.result) {\n    console.warn('createDocumentTemplateMetadata: ops.commit() failed', {\n      _class,\n      space,\n      _mixin,\n      project,\n      parent,\n      templateId,\n      prefix,\n      specCode,\n      specTitle,\n      seqNumber,\n      code,\n      documentMetaId,\n      projectDocumentId\n    })\n  }\n\n  return { success: success.result, seqNumber, code, documentMetaId, projectDocumentId }\n}","sourceCodeStart":434,"sourceCodeEnd":470,"githubUrl":"https://github.com/hcengineering/platform/blob/63e28dc96483967b2fc21c881b3f1023c1de7718/plugins/controlled-documents/src/docutils.ts#L434-L470","documentation":"createDocumentTemplateMetadata creates the metadata record (seqNumber, code, project link) for a document template inside an Ops transaction. When ops.commit() returns falsy result, the metadata was not persisted; the failure is warned with the full parameter set.","triggerScenarios":"Calling createDocumentTemplateMetadata where the referenced templateId, project, parent, or space is invalid, the generated code/sequence conflicts with an existing record, or the target template object was deleted between template creation and metadata commit.","commonSituations":"Sequence number collision under concurrency (two templates metadata created at once); template creation committed but metadata step raced with deletion; wrong project/parent passed from a caller.","solutions":["Use the logged fields (_class, space, _mixin, project, parent, templateId) to find the failing reference.","Re-run metadata creation with retry — the callers ({ seqNumber, code, projectDocumentId, success }) already support retry flows.","Ensure sequence/code generation is atomic or re-fetch the next sequence on conflict.","Verify the template object exists before committing its metadata."],"exampleFix":"// before\nconst r = await createDocumentTemplateMetadata(client, { templateId, project, parent, space })\nif (!r.success) console.warn('meta failed')\n// after\nlet r = await createDocumentTemplateMetadata(client, { templateId, project, parent, space })\nif (!r.success) r = await retryMetadata(client, { templateId, project, parent, space })\nif (!r.success) throw new Error(`template metadata commit failed: ${templateId}`)","handlingStrategy":"retry","validationCode":"const template = await client.findOne(documents.class.DocumentTemplate, { _id: templateId })\nif (template === undefined) throw new Error('template must exist before metadata commit')","typeGuard":"function metaCreated(r: any): r is { success: true; seqNumber: number; code: string } {\n  return r != null && r.success === true\n}","tryCatchPattern":"let r = await createDocumentTemplateMetadata(client, args)\nif (!r.success) {\n  r = await createDocumentTemplateMetadata(client, args) // re-fetch sequence on retry\n  if (!r.success) throw new Error('template metadata commit failed')\n}","preventionTips":["Serialize sequence/code generation or retry with a fresh sequence on conflict.","Commit metadata immediately after template creation to reduce deletion races.","Log the returned code/seqNumber for traceability."],"tags":["controlled-documents","commit-failure","sequence-conflict"],"backgroundTag":"transaction-commit-failed","analyzedSha":"63e28dc96483967b2fc21c881b3f1023c1de7718","analyzedAt":"2026-08-29T15:21:27.377Z","schemaVersion":2},"datasetVersion":"2026-08-29T17:17:51.833Z"}