hcengineering/platform · error · Error

Lead creation failed

Error message

Lead  creation failed

What it means

Thrown in CreateLead.svelte when preconditions for creating a Lead fail: either the Sequence object for lead.class.Lead is missing (client.findOne returned undefined) or the customer contact is null/undefined. Sequences generate incrementing lead numbers, so without one the lead cannot be numbered, and a customer is required data.

Source

Thrown at plugins/lead-resources/src/components/CreateLead.svelte:89

      state = rawStates[0]?._id
      object.status = state
    }
  }

  $: if (_space === undefined) {
    if (funnels.find((it) => it._id === _space) === undefined) {
      _space = funnels[0]?._id
    }
  }

  $: funnel = funnels.find((it) => it._id === _space)

  let kind: Ref<TaskType> | undefined = undefined

  async function createLead () {
    const sequence = await client.findOne(core.class.Sequence, { attachedTo: lead.class.Lead })
    if (sequence === undefined || customer == null) {
      throw new Error('Lead  creation failed')
    }
    if (kind === undefined) {
      throw new Error('kind is not specified')
    }

    const incResult = await client.update(sequence, { $inc: { sequence: 1 } }, true)
    const number = (incResult as any).object.sequence

    const value: AttachedData<Lead> = {
      status: state,
      number,
      identifier: `LEAD-${number}`,
      title,
      kind,
      rank: '',
      assignee: null,
      startDate: null,
      dueDate: null,

View on GitHub (pinned to 63e28dc964)

Solutions

  1. Select a customer in the dialog before submitting
  2. Ensure the Sequence object for Lead exists (seed it via plugin setup/upgrade routine, e.g. lead.class.Leed sequence with initial value)
  3. Re-run product/workspace migrations so core sequences are created
  4. Catch the error and surface which precondition failed (sequence vs customer)

Example fix

// before
const sequence = await client.findOne(core.class.Sequence, { attachedTo: lead.class.Lead })
if (sequence === undefined || customer == null) {
  throw new Error('Lead  creation failed')
}
// after
const sequence = await client.findOne(core.class.Sequence, { attachedTo: lead.class.Lead })
if (sequence === undefined) {
  await ensureSequence(lead.class.Lead, 0) // seed missing sequence
}
if (customer == null) {
  ui.notify('Please select a customer first')
  return
}
Defensive patterns

Strategy: validation

Validate before calling

const sequence = await client.findOne(core.class.Sequence, { attachedTo: lead.class.Lead })
if (sequence === undefined) {
  await ensureSequence(lead.class.Lead)
}
if (customer == null) {
  throw new Error('Select a customer before creating a lead')
}

Type guard

function canCreateLead (sequence: Sequence | undefined, customer: Ref<Contact> | null | undefined): boolean {
  return sequence !== undefined && customer != null
}

Try / catch

try {
  await createLead()
} catch (err) {
  if (err instanceof Error && err.message.startsWith('Lead')) {
    ui.notify('Lead cannot be created: missing sequence or customer.')
  } else throw err
}

Prevention

When it happens

Trigger: No core.class.Sequence document with attachedTo === lead.class.Lead exists in the workspace, or the component's `customer` variable was never set (null/undefined) when createLead() runs.

Common situations: Fresh workspace or migration where Lead sequence was never seeded; user submits the form before selecting a customer; component state reset losing the customer selection.

Related errors


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