{"record":{"id":"bb0f39d4ffe1f13a","repo":"hcengineering/platform","slug":"lead-not-found-id-ref","errorCode":null,"errorMessage":"Lead not found, _id: ${ref}","messagePattern":"Lead not found, _id: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"plugins/lead-resources/src/utils.ts","lineNumber":7,"sourceCode":"import { type Doc, type Ref, type TxOperations } from '@hcengineering/core'\nimport { type Lead } from '@hcengineering/lead'\nimport lead from './plugin'\n\nexport async function getLeadTitle (client: TxOperations, ref: Ref<Doc>, doc?: Lead): Promise<string> {\n  const object = doc ?? (await client.findOne(lead.class.Lead, { _id: ref as Ref<Lead> }))\n  if (object === undefined) throw new Error(`Lead not found, _id: ${ref}`)\n  return `LEAD-${object.number}`\n}\n\nexport async function getLeadId (client: TxOperations, ref: Ref<Lead>, doc?: Lead): Promise<string> {\n  const object = doc ?? (await client.findOne(lead.class.Lead, { _id: ref }))\n  if (object === undefined) throw new Error(`Lead not found, _id: ${ref}`)\n  return object.identifier\n}\n","sourceCodeStart":1,"sourceCodeEnd":16,"githubUrl":"https://github.com/hcengineering/platform/blob/63e28dc96483967b2fc21c881b3f1023c1de7718/plugins/lead-resources/src/utils.ts#L1-L16","documentation":"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>'.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Pass the already-loaded Lead object as the doc parameter to avoid the lookup and the error entirely.","Verify the lead exists before calling (client.findOne(lead.class.Lead, {_id: ref})).","Ensure the ref originates from lead.class.Lead, not another document class.","Refresh stale references after lead deletion instead of reusing cached ids."],"exampleFix":"// before\nconst object = doc ?? (await client.findOne(lead.class.Lead, { _id: ref as Ref<Lead> }))\nif (object === undefined) throw new Error(`Lead not found, _id: ${ref}`)\n// after\nconst object = doc ?? (await client.findOne(lead.class.Lead, { _id: ref as Ref<Lead> }))\nif (object === undefined) return 'LEAD-?' // or throw with more context\nreturn `LEAD-${object.number}`","handlingStrategy":"validation","validationCode":"const lead = await client.findOne(lead.class.Lead, { _id: ref as Ref<Lead> })\nif (lead === undefined) {\n  console.warn(`Lead ${ref} not found; skipping title lookup`)\n  return\n}\nconst title = await getLeadTitle(client, ref, lead)","typeGuard":"function isLead (doc: Doc | undefined): doc is Lead {\n  return doc !== undefined && (doc as Lead).number !== undefined\n}","tryCatchPattern":"try {\n  const title = await getLeadTitle(client, ref)\n  // use title\n} catch (err) {\n  if (err instanceof Error && err.message.startsWith('Lead not found')) {\n    console.warn(err.message)\n    return 'LEAD-?'\n  }\n  throw err\n}","preventionTips":["Pass the already-fetched Lead as the doc parameter whenever available.","Confirm refs passed to formatting helpers belong to lead.class.Lead.","Purge UI caches of lead refs after deletion.","Display lead titles defensively — data may disappear due to concurrent edits."],"tags":["not-found","data-access","huly-platform"],"backgroundTag":"document-not-found","analyzedSha":"63e28dc96483967b2fc21c881b3f1023c1de7718","analyzedAt":"2026-08-29T15:21:27.377Z","schemaVersion":2},"datasetVersion":"2026-08-29T17:17:51.833Z"}