{"record":{"id":"604b07edb505f0e1","repo":"hcengineering/platform","slug":"product-not-found","errorCode":null,"errorMessage":"product not found","messagePattern":"product not found","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"plugins/inventory-resources/src/components/CreateVariant.svelte","lineNumber":49,"sourceCode":"    _class: inventory.class.Variant,\n    space: core.space.Workspace,\n    _id: generateId(),\n    collection: 'variants',\n    modifiedOn: Date.now(),\n    modifiedBy: '' as PersonId\n  }\n\n  const dispatch = createEventDispatcher()\n  const client = getClient()\n\n  export function canClose (): boolean {\n    return doc.attachedTo.length === 0 && doc.name.length === 0\n  }\n\n  async function create () {\n    const productInstance = await client.findOne(inventory.class.Product, { _id: doc.attachedTo as Ref<Product> })\n    if (productInstance === undefined) {\n      throw new Error('product not found')\n    }\n\n    await client.addCollection(inventory.class.Variant, doc.space, doc.attachedTo, productInstance._class, 'variants', {\n      name: doc.name,\n      sku: doc.sku\n    })\n  }\n</script>\n\n<Card\n  label={inventory.string.CreateVariant}\n  okAction={create}\n  canSave={doc.name.trim().length > 0 && doc.sku.trim().length > 0}\n  on:close={() => {\n    dispatch('close')\n  }}\n  on:changeContent\n>","sourceCodeStart":31,"sourceCodeEnd":67,"githubUrl":"https://github.com/hcengineering/platform/blob/63e28dc96483967b2fc21c881b3f1023c1de7718/plugins/inventory-resources/src/components/CreateVariant.svelte#L31-L67","documentation":"Thrown in CreateVariant.svelte when creating an inventory Variant attached to a Product. Before calling client.addCollection, the code looks up the parent Product via client.findOne(inventory.class.Product, {_id: doc.attachedTo}) and throws if it cannot be found in the database. This guards against creating an orphaned variant pointing at a nonexistent or deleted product.","triggerScenarios":"doc.attachedTo holds a Ref<Product> that does not resolve: the product was deleted before variant creation, the _id is stale/wrong, or the component was opened without a valid product selected.","commonSituations":"User deletes the product in another tab while the CreateVariant dialog is open; stale client cache after a sync issue; programmatic navigation passing an invalid product reference.","solutions":["Refresh the product list and re-open the CreateVariant dialog against an existing product","Verify doc.attachedTo actually holds a valid product _id before opening the dialog","Re-login / reconnect the client so its data is synced, then retry","Wrap create() in try/catch and show a user-friendly message instead of crashing"],"exampleFix":"// before\nconst productInstance = await client.findOne(inventory.class.Product, { _id: doc.attachedTo as Ref<Product> })\nif (productInstance === undefined) {\n  throw new Error('product not found')\n}\n// after\nif (doc.attachedTo === undefined) return\nconst productInstance = await client.findOne(inventory.class.Product, { _id: doc.attachedTo as Ref<Product> })\nif (productInstance === undefined) {\n  ui.notify('The selected product no longer exists. Please pick another product.')\n  return\n}","handlingStrategy":"validation","validationCode":"const product = await client.findOne(inventory.class.Product, { _id: doc.attachedTo as Ref<Product> })\nif (doc.attachedTo === undefined || product === undefined) {\n  throw new Error('Cannot create variant: attached product does not exist')\n}","typeGuard":"function hasProduct (doc: { attachedTo?: Ref<Product> }): doc is { attachedTo: Ref<Product> } {\n  return doc.attachedTo !== undefined\n}","tryCatchPattern":"try {\n  await create()\n} catch (err) {\n  if (err instanceof Error && err.message === 'product not found') {\n    ui.notify('Selected product no longer exists; please reselect it.')\n  } else throw err\n}","preventionTips":["Always resolve the parent product with findOne before opening the variant form","Listen for object-removal events to close dialogs referencing deleted products","Never pass raw unsanitized ids into attachedTo","Test variant creation against freshly deleted products"],"tags":["reference-lookup","deleted-object","inventory"],"backgroundTag":"referenced-object-not-found","analyzedSha":"63e28dc96483967b2fc21c881b3f1023c1de7718","analyzedAt":"2026-08-29T15:21:27.377Z","schemaVersion":2},"datasetVersion":"2026-08-29T17:17:51.833Z"}