{"record":{"id":"98988be00d8497ce","repo":"hcengineering/platform","slug":"failed-to-create-document-template-attached-doc","errorCode":null,"errorMessage":"Failed to create document template attached doc: ${template.title}","messagePattern":"Failed to create document template attached doc: (.+?)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/importer/src/importer/importer.ts","lineNumber":1241,"sourceCode":"        seqNumber,\n        prefix: template.docPrefix,\n        content: contentId,\n        changeControl: changeControlId,\n        commentSequence: 0,\n        requests: 0,\n        labels: 0\n      },\n      template.id as unknown as Ref<ControlledDocument>\n    )\n\n    await ops.createMixin(template.id, documents.class.Document, spaceId, documents.mixin.DocumentTemplate, {\n      sequence: 0,\n      docPrefix: template.docPrefix\n    })\n\n    const commit = await ops.commit()\n    if (!commit.result) {\n      throw new Error('Failed to create document template attached doc: ' + template.title)\n    }\n\n    this.logger.log('Document template attached doc created: ' + result)\n    return result\n  }\n\n  private async createControlledDocMetaHierarhy (\n    doc: ImportControlledDocument,\n    templateMetaMap: Map<Ref<ControlledDocument>, { seqNumber: number, code: string }>,\n    spaceId: Ref<DocumentSpace>,\n    parentProjectDocumentId?: Ref<ProjectDocument>\n  ): Promise<Ref<ControlledDocument>> {\n    this.logger.log('Creating controlled document: ' + doc.title)\n    const documentId = doc.id ?? generateId<ControlledDocument>()\n\n    const result = await createControlledDocMetadata(\n      this.client,\n      documents.template.ProductChangeControl,","sourceCodeStart":1223,"sourceCodeEnd":1259,"githubUrl":"https://github.com/hcengineering/platform/blob/63e28dc96483967b2fc21c881b3f1023c1de7718/packages/importer/src/importer/importer.ts#L1223-L1259","documentation":"createDocTemplateAttachedDoc builds a transaction batch (ops.addCollection + ops.createMixin) and commits it. If commit() returns a result without result set, the transaction failed server-side and the attached ControlledDocument for the template was not created, so the importer throws this error.","triggerScenarios":"ops.commit() returns { result: undefined } while creating the template's attached ControlledDocument — e.g. a tx in the batch is rejected (invalid parent metaId, missing space, permission denial), or a concurrent modification/ conflicts during commit.","commonSituations":"Target space or template.metaId document missing/deleted before the commit; account lacking create permission for ControlledDocument in the space; server-side tx validation failures after schema changes; transient connectivity drop during commit.","solutions":["Inspect commit() result/error details and server logs to find which tx in the batch was rejected.","Verify template.metaId (parent DocumentMeta) and spaceId still exist and are writable before committing.","Ensure the importing account has permission to add ControlledDocument and the DocumentTemplate mixin in the space.","Retry the import; if transient conflicts, serialize imports or retry the commit with backoff.","Align importer/server versions so the ControlledDocument data schema matches what addCollection sends."],"exampleFix":"// before\nconst commit = await ops.commit()\nif (!commit.result) {\n  throw new Error('Failed to create document template attached doc: ' + template.title)\n}\n// after\nconst commit = await ops.commit()\nif (!commit.result) {\n  this.logger.error('Commit failed for template attached doc', { title: template.title, commit })\n  throw new Error('Failed to create document template attached doc: ' + template.title)\n}","handlingStrategy":"retry","validationCode":"const spaceDoc = await client.findOne(documents.class.OrgSpace, { _id: spaceId })\nif (spaceDoc === undefined) throw new Error('Target space missing before import')\nconst meta = template.metaId !== undefined\n  ? await client.findOne(documents.class.DocumentMeta, { _id: template.metaId })\n  : undefined\nif (template.metaId !== undefined && meta === undefined) throw new Error('Parent DocumentMeta missing: ' + template.metaId)","typeGuard":"function isCommitSuccess(commit: { result?: unknown }): commit is { result: NonNullable<unknown> } {\n  return commit.result !== undefined && commit.result !== null\n}","tryCatchPattern":"try {\n  await importer.importOrgSpace(space)\n} catch (err) {\n  if (err instanceof Error && err.message.startsWith('Failed to create document template attached doc:')) {\n    await delay(backoff)\n    await importer.importOrgSpace(space) // retry after checking server state\n  } else throw err\n}","preventionTips":["Verify the parent DocumentMeta and space exist and are writable before the commit","Run imports serially to avoid concurrent-modification commit conflicts","Add retry with backoff for transient commit failures","Check server tx-rejection logs after any failed import and align schemas/versions"],"tags":["transaction","commit","import","templates"],"backgroundTag":"transaction-commit-failed","analyzedSha":"63e28dc96483967b2fc21c881b3f1023c1de7718","analyzedAt":"2026-08-29T15:21:27.377Z","schemaVersion":2},"datasetVersion":"2026-08-29T17:17:51.833Z"}