{"record":{"id":"9fbe9088eaa0214b","repo":"hcengineering/platform","slug":"template-meta-not-found-template-id","errorCode":null,"errorMessage":"Template meta not found: ${template.id}","messagePattern":"Template meta not found: (.+?)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/importer/src/importer/importer.ts","lineNumber":1063,"sourceCode":"      if (this.isDocumentTemplate(doc)) {\n        await this.createDocTemplateMetaHierarhy(doc as ImportControlledDocumentTemplate, templateMetaMap, spaceId)\n      } else {\n        await this.createControlledDocMetaHierarhy(doc as ImportControlledDocument, templateMetaMap, spaceId)\n      }\n    }\n\n    // Partition templates and documents\n    const templateMap = new Map<Ref<ControlledDocument>, ImportControlledDocumentTemplate>()\n    const documentMap = new Map<Ref<ControlledDocument>, ImportControlledDocument>()\n    for (const doc of space.docs) {\n      this.partitionTemplatesFromDocuments(doc, documentMap, templateMap)\n    }\n\n    // Create attached docs for templates\n    for (const template of templateMap.values()) {\n      const meta = templateMetaMap.get(template.id)\n      if (meta === undefined) {\n        throw new Error('Template meta not found: ' + template.id)\n      }\n      await this.createDocTemplateAttachedDoc(template, meta.seqNumber, meta.code, spaceId)\n    }\n\n    // Create attached docs for documents\n    for (const document of documentMap.values()) {\n      await this.createControlledDocAttachedDoc(document, spaceId)\n    }\n\n    return spaceId\n  }\n\n  private partitionTemplatesFromDocuments (\n    doc: ImportControlledDocOrTemplate,\n    documentMap: Map<Ref<ControlledDocument>, ImportControlledDocument>,\n    templateMap: Map<Ref<ControlledDocument>, ImportControlledDocumentTemplate>\n  ): void {\n    if (this.isDocumentTemplate(doc)) {","sourceCodeStart":1045,"sourceCodeEnd":1081,"githubUrl":"https://github.com/hcengineering/platform/blob/63e28dc96483967b2fc21c881b3f1023c1de7718/packages/importer/src/importer/importer.ts#L1045-L1081","documentation":"During importOrgSpace, templates are first processed by createDocTemplateMetaHierarhy which records { seqNumber, code } into templateMetaMap keyed by template id. Later, when creating attached docs, each template must have that meta entry; if it is missing the importer's internal invariant is broken and it throws this error.","triggerScenarios":"A template appears in templateMap (collected by partitionTemplatesFromDocuments over all docs and subdocs) but was never passed through createDocTemplateMetaHierarhy — e.g. a template nested under a document branch (createControlledDocMetaHierarhy) that does not recurse into template children, or a doc.id collision overwriting the map key.","commonSituations":"Import data where a DocumentTemplate is nested inside a non-template parent so the meta-hierarchy pass skips it; duplicated ids in the import payload causing templateMetaMap key mismatches; hand-edited import JSON with inconsistent class fields.","solutions":["Ensure createDocTemplateMetaHierarhy is invoked for every doc classified as a template, including templates nested under controlled documents.","Check import data for duplicate doc.id values that collide in templateMetaMap and make ids unique.","Verify each doc's class field correctly marks templates (documents.mixin.DocumentTemplate) so partitioning matches the meta pass.","Add a validation pass before import that asserts every template in space.docs will be visited by the meta hierarchy traversal."],"exampleFix":"// before\nconst meta = templateMetaMap.get(template.id)\nif (meta === undefined) {\n  throw new Error('Template meta not found: ' + template.id)\n}\n// after\nlet meta = templateMetaMap.get(template.id)\nif (meta === undefined) {\n  this.logger.warn('Template meta missing, computing lazily: ' + template.id)\n  await this.createDocTemplateMetaHierarhy(template, templateMetaMap, spaceId)\n  meta = templateMetaMap.get(template.id)\n}\nif (meta === undefined) {\n  throw new Error('Template meta not found: ' + template.id)\n}","handlingStrategy":"validation","validationCode":"const templateIds = new Set<string>()\nconst collect = (d: ImportDoc): void => {\n  if (d.class === documents.mixin.DocumentTemplate) templateIds.add(d.id)\n  d.subdocs.forEach(collect)\n}\nspace.docs.forEach(collect)\nif (new Set(templateIds).size !== templateIds.size) throw new Error('Duplicate template ids in import payload')","typeGuard":"function hasTemplateMeta(\n  map: Map<Ref<ControlledDocument>, { seqNumber: number, code: string }>,\n  id: Ref<ControlledDocument>\n): boolean {\n  return map.has(id)\n}","tryCatchPattern":"try {\n  await importer.importOrgSpace(space)\n} catch (err) {\n  if (err instanceof Error && err.message.startsWith('Template meta not found:')) {\n    const id = err.message.split(': ')[1]\n    // inspect import payload for that template: nesting/duplicate id issue\n  } else throw err\n}","preventionTips":["Keep template ids unique across the whole import payload","Ensure templates nested under controlled documents are still routed through the meta hierarchy pass","Verify class fields (documents.mixin.DocumentTemplate) are set correctly in the import JSON","Dry-run classification (partitionTemplatesFromDocuments) and cross-check against meta-map keys before committing"],"tags":["import","templates","state","invariant"],"backgroundTag":"missing-lookup-entry","analyzedSha":"63e28dc96483967b2fc21c881b3f1023c1de7718","analyzedAt":"2026-08-29T15:21:27.377Z","schemaVersion":2},"datasetVersion":"2026-08-29T17:17:51.833Z"}