hcengineering/platform · error

Lead not found, _id: ${ref}

Error message

Lead not found, _id: ${ref}

What it means

getLeadTitle resolves a Lead document by ref (or uses the provided doc) and formats its number as 'LEAD-<number>'. If neither a doc is passed nor a Lead with that _id exists in lead.class.Lead, it throws 'Lead not found, _id: <ref>'.

Source

Thrown at plugins/lead-resources/src/utils.ts:7

import { type Doc, type Ref, type TxOperations } from '@hcengineering/core'
import { type Lead } from '@hcengineering/lead'
import lead from './plugin'

export async function getLeadTitle (client: TxOperations, ref: Ref<Doc>, doc?: Lead): Promise<string> {
  const object = doc ?? (await client.findOne(lead.class.Lead, { _id: ref as Ref<Lead> }))
  if (object === undefined) throw new Error(`Lead not found, _id: ${ref}`)
  return `LEAD-${object.number}`
}

export async function getLeadId (client: TxOperations, ref: Ref<Lead>, doc?: Lead): Promise<string> {
  const object = doc ?? (await client.findOne(lead.class.Lead, { _id: ref }))
  if (object === undefined) throw new Error(`Lead not found, _id: ${ref}`)
  return object.identifier
}

View on GitHub (pinned to 63e28dc964)

Solutions

  1. Pass the already-loaded Lead object as the doc parameter to avoid the lookup and the error entirely.
  2. Verify the lead exists before calling (client.findOne(lead.class.Lead, {_id: ref})).
  3. Ensure the ref originates from lead.class.Lead, not another document class.
  4. Refresh stale references after lead deletion instead of reusing cached ids.

Example fix

// before
const object = doc ?? (await client.findOne(lead.class.Lead, { _id: ref as Ref<Lead> }))
if (object === undefined) throw new Error(`Lead not found, _id: ${ref}`)
// after
const object = doc ?? (await client.findOne(lead.class.Lead, { _id: ref as Ref<Lead> }))
if (object === undefined) return 'LEAD-?' // or throw with more context
return `LEAD-${object.number}`
Defensive patterns

Strategy: validation

Validate before calling

const lead = await client.findOne(lead.class.Lead, { _id: ref as Ref<Lead> })
if (lead === undefined) {
  console.warn(`Lead ${ref} not found; skipping title lookup`)
  return
}
const title = await getLeadTitle(client, ref, lead)

Type guard

function isLead (doc: Doc | undefined): doc is Lead {
  return doc !== undefined && (doc as Lead).number !== undefined
}

Try / catch

try {
  const title = await getLeadTitle(client, ref)
  // use title
} catch (err) {
  if (err instanceof Error && err.message.startsWith('Lead not found')) {
    console.warn(err.message)
    return 'LEAD-?'
  }
  throw err
}

Prevention

When it happens

Trigger: Calling getLeadTitle with a ref that is not a Lead (e.g. another class's _id), a deleted lead, a lead in another workspace, or no doc supplied for a stale ref.

Common situations: Display pipelines/formatting functions invoked after the lead was deleted; passing a generic Ref<Doc> from unrelated notifications; cross-workspace references; refs cached in UI after data removal.

Related errors


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