{"record":{"id":"57daf0598732e2e4","repo":"hcengineering/platform","slug":"failed-to-create-document-template-template-tit","errorCode":null,"errorMessage":"Failed to create document template: ${template.title}","messagePattern":"Failed to create document template: (.+?)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/importer/src/importer/importer.ts","lineNumber":1156,"sourceCode":"    this.logger.log('Creating document template: ' + template.title)\n    const templateId = template.id ?? generateId<ControlledDocument>()\n\n    const { seqNumber, code, projectDocumentId, success } = await createDocumentTemplateMetadata(\n      this.client,\n      documents.class.Document,\n      spaceId,\n      documents.mixin.DocumentTemplate,\n      undefined,\n      parentProjectDocumentId,\n      templateId as unknown as Ref<ControlledDocument>,\n      template.docPrefix,\n      template.code ?? '',\n      template.title,\n      template.metaId\n    )\n\n    if (!success) {\n      throw new Error('Failed to create document template: ' + template.title)\n    }\n\n    templateMetaMap.set(templateId, { seqNumber, code })\n\n    for (const subdoc of template.subdocs) {\n      if (this.isDocumentTemplate(subdoc)) {\n        await this.createDocTemplateMetaHierarhy(\n          subdoc as ImportControlledDocumentTemplate,\n          templateMetaMap,\n          spaceId,\n          projectDocumentId\n        )\n      } else {\n        await this.createControlledDocMetaHierarhy(\n          subdoc as ImportControlledDocument,\n          templateMetaMap,\n          spaceId,\n          projectDocumentId","sourceCodeStart":1138,"sourceCodeEnd":1174,"githubUrl":"https://github.com/hcengineering/platform/blob/63e28dc96483967b2fc21c881b3f1023c1de7718/packages/importer/src/importer/importer.ts#L1138-L1174","documentation":"createDocTemplateMetaHierarhy calls createDocumentTemplateMetadata to create the template's Document/meta records on the server. The helper returns a success flag; when it is false the template metadata was not persisted and the importer throws this error with the template title.","triggerScenarios":"createDocumentTemplateMetadata returns success:false during importOrgSpace meta-hierarchy creation — e.g. server-side validation failure, duplicate template code/metaId, missing metaId reference, or the underlying client.createDoc/addCollection tx being rejected.","commonSituations":"Import payload references a metaId (DocumentMeta) that does not exist in the target workspace; template.code collides with an existing template; server rejects the op due to permissions or schema mismatch after a version upgrade.","solutions":["Check server logs for the rejection reason from createDocumentTemplateMetadata (validation, duplicate code, missing metaId).","Ensure template.metaId points to an existing DocumentMeta in the target workspace, or create it before import.","Verify the importing account has permission to create documents in the target space.","Make template codes unique in the import payload and retry the import.","Upgrade/align importer and server package versions so documents.class/mixin schemas match."],"exampleFix":"// before\nconst { seqNumber, code, projectDocumentId, success } = await createDocumentTemplateMetadata(...)\nif (!success) {\n  throw new Error('Failed to create document template: ' + template.title)\n}\n// after\nconst { seqNumber, code, projectDocumentId, success } = await createDocumentTemplateMetadata(...)\nif (!success) {\n  this.logger.error('createDocumentTemplateMetadata failed', { title: template.title, metaId: template.metaId, code: template.code })\n  throw new Error('Failed to create document template: ' + template.title)\n}","handlingStrategy":"try-catch","validationCode":"if (template.metaId !== undefined) {\n  const meta = await client.findOne(documents.class.DocumentMeta, { _id: template.metaId })\n  if (meta === undefined) throw new Error('Referenced metaId does not exist: ' + template.metaId)\n}\nconst codes = new Set(space.docs.flatMap(collectAll).filter(isTemplate).map(t => t.code))\nif (codes.size !== countTemplates) throw new Error('Duplicate template codes in payload')","typeGuard":"function isMetaResult(r: { success: boolean } & Record<string, unknown>): r is { success: true, seqNumber: number, code: string, projectDocumentId: Ref<ProjectDocument> } {\n  return r.success === true\n}","tryCatchPattern":"try {\n  await importer.importOrgSpace(space)\n} catch (err) {\n  if (err instanceof Error && err.message.startsWith('Failed to create document template:')) {\n    // check server logs / permissions / metaId validity, fix payload, re-run\n  } else throw err\n}","preventionTips":["Confirm every template.metaId exists in the target workspace before import","Ensure template codes are unique","Verify write permissions for the importing account in the target space","Keep importer and server package versions in sync"],"tags":["import","templates","server","persistence"],"backgroundTag":"template-creation-failed","analyzedSha":"63e28dc96483967b2fc21c881b3f1023c1de7718","analyzedAt":"2026-08-29T15:21:27.377Z","schemaVersion":2},"datasetVersion":"2026-08-29T17:17:51.833Z"}