hcengineering/platform · error · Error

Failed to create document template: ${template.title}

Error message

Failed to create document template: ${template.title}

What it means

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.

Source

Thrown at packages/importer/src/importer/importer.ts:1156

    this.logger.log('Creating document template: ' + template.title)
    const templateId = template.id ?? generateId<ControlledDocument>()

    const { seqNumber, code, projectDocumentId, success } = await createDocumentTemplateMetadata(
      this.client,
      documents.class.Document,
      spaceId,
      documents.mixin.DocumentTemplate,
      undefined,
      parentProjectDocumentId,
      templateId as unknown as Ref<ControlledDocument>,
      template.docPrefix,
      template.code ?? '',
      template.title,
      template.metaId
    )

    if (!success) {
      throw new Error('Failed to create document template: ' + template.title)
    }

    templateMetaMap.set(templateId, { seqNumber, code })

    for (const subdoc of template.subdocs) {
      if (this.isDocumentTemplate(subdoc)) {
        await this.createDocTemplateMetaHierarhy(
          subdoc as ImportControlledDocumentTemplate,
          templateMetaMap,
          spaceId,
          projectDocumentId
        )
      } else {
        await this.createControlledDocMetaHierarhy(
          subdoc as ImportControlledDocument,
          templateMetaMap,
          spaceId,
          projectDocumentId

View on GitHub (pinned to 63e28dc964)

Solutions

  1. Check server logs for the rejection reason from createDocumentTemplateMetadata (validation, duplicate code, missing metaId).
  2. Ensure template.metaId points to an existing DocumentMeta in the target workspace, or create it before import.
  3. Verify the importing account has permission to create documents in the target space.
  4. Make template codes unique in the import payload and retry the import.
  5. Upgrade/align importer and server package versions so documents.class/mixin schemas match.

Example fix

// before
const { seqNumber, code, projectDocumentId, success } = await createDocumentTemplateMetadata(...)
if (!success) {
  throw new Error('Failed to create document template: ' + template.title)
}
// after
const { seqNumber, code, projectDocumentId, success } = await createDocumentTemplateMetadata(...)
if (!success) {
  this.logger.error('createDocumentTemplateMetadata failed', { title: template.title, metaId: template.metaId, code: template.code })
  throw new Error('Failed to create document template: ' + template.title)
}
Defensive patterns

Strategy: try-catch

Validate before calling

if (template.metaId !== undefined) {
  const meta = await client.findOne(documents.class.DocumentMeta, { _id: template.metaId })
  if (meta === undefined) throw new Error('Referenced metaId does not exist: ' + template.metaId)
}
const codes = new Set(space.docs.flatMap(collectAll).filter(isTemplate).map(t => t.code))
if (codes.size !== countTemplates) throw new Error('Duplicate template codes in payload')

Type guard

function isMetaResult(r: { success: boolean } & Record<string, unknown>): r is { success: true, seqNumber: number, code: string, projectDocumentId: Ref<ProjectDocument> } {
  return r.success === true
}

Try / catch

try {
  await importer.importOrgSpace(space)
} catch (err) {
  if (err instanceof Error && err.message.startsWith('Failed to create document template:')) {
    // check server logs / permissions / metaId validity, fix payload, re-run
  } else throw err
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of hcengineering/platform@63e28dc964 (2026-08-29). Data as JSON: /api/errors/57daf0598732e2e4. Report an issue: GitHub.