{"record":{"id":"17c25d0df26d9ede","repo":"twentyhq/twenty","slug":"operation-failed","errorCode":"OPERATION_FAILED","errorMessage":"Failed to create company: no id returned.","messagePattern":"Failed to create company: no id returned\\.","errorType":"exception","errorClass":"PdlOperationError","httpStatus":null,"severity":"error","filePath":"packages/twenty-apps/public/people-data-labs/src/logic-functions/utils/find-or-create-current-company.ts","lineNumber":52,"sourceCode":"    isNonEmptyString(companyMatchKeys.name) ||\n    isNonEmptyString(companyMatchKeys.website);\n\n  if (!canCreateNewCompany) {\n    return undefined;\n  }\n\n  try {\n    const createCompanyResult = (await client.mutation({\n      createCompany: {\n        __args: { data: buildCompanyCreateData(personData) },\n        id: true,\n      },\n    })) as CreateCompanyResult;\n\n    const createdCompanyId = createCompanyResult.createCompany?.id;\n\n    if (!isDefined(createdCompanyId)) {\n      throw new PdlOperationError('Failed to create company: no id returned.');\n    }\n\n    return createdCompanyId;\n  } catch (createCompanyError) {\n    if (!isUniqueViolationError(createCompanyError)) {\n      throw createCompanyError;\n    }\n\n    const raceWinnerCompanyId = await findCompanyId({\n      client,\n      matchKeys: companyMatchKeys,\n    });\n    if (isDefined(raceWinnerCompanyId)) {\n      return raceWinnerCompanyId;\n    }\n\n    throw createCompanyError;\n  }","sourceCodeStart":34,"sourceCodeEnd":70,"githubUrl":"https://github.com/twentyhq/twenty/blob/1f5dd2bbd2a8da3419c8cfd52dd545c0024df1a6/packages/twenty-apps/public/people-data-labs/src/logic-functions/utils/find-or-create-current-company.ts#L34-L70","documentation":"The PDL enrichment creates a company record via the `createCompany` mutation and expects an `id` back. If `createCompanyResult.createCompany?.id` is undefined, the mutation did not return a created company id and the flow throws a `PdlOperationError` (code `OPERATION_FAILED`). The surrounding try/catch specifically handles unique-constraint races by falling back to a `findCompanyId` lookup, so this throw is only reached for non-violation cases where the id is simply absent.","triggerScenarios":"The mutation succeeds at the transport level but returns no `id` (server omitted the field, or returned an error shape genql maps to undefined); the company data built by `buildCompanyCreateData(personData)` is invalid in a way the server rejects without throwing; or a non-unique-violation server error is swallowed into an undefined result by the client.","commonSituations":"A workspace object schema change that made `createCompany` require a field the builder does not provide; a server bug returning a company without an id; permissions on the acting user that prevent company creation surfaced as a null result.","solutions":["Inspect the full `createCompanyResult` (and any client `errors`) to see why `id` is absent before the throw.","Validate the output of `buildCompanyCreateData(personData)` against the current company object's required fields.","Confirm the acting user/role has permission to create companies in this workspace.","If the root cause is a transient server issue, the existing race-fallback only covers unique violations; consider a guarded retry for empty-id results."],"exampleFix":"// before\nconst createdCompanyId = createCompanyResult.createCompany?.id;\nif (!isDefined(createdCompanyId)) {\n  throw new PdlOperationError('Failed to create company: no id returned.');\n}\n\n// after — include the input and any server errors in the message\nconst createdCompanyId = createCompanyResult.createCompany?.id;\nif (!isDefined(createdCompanyId)) {\n  throw new PdlOperationError(\n    `Failed to create company: no id returned (input=${JSON.stringify(buildCompanyCreateData(personData))}; errors=${JSON.stringify((createCompanyResult as any).errors ?? [])})`,\n  );\n}","handlingStrategy":"type-guard","validationCode":"const createCompanyData = buildCompanyCreateData(personData);\n// Validate required company fields before the mutation.\nif (!isNonEmptyString(createCompanyData.name)) {\n  throw new PdlOperationError('Cannot create company: name is missing from person data');\n}","typeGuard":"const hasCreatedCompanyId = (\n  r: unknown,\n): r is { createCompany: { id: string } } =>\n  typeof r === 'object' &&\n  r !== null &&\n  typeof (r as any).createCompany?.id === 'string';","tryCatchPattern":"try {\n  const result = (await client.mutation({ createCompany: { __args: { data }, id: true } })) as CreateCompanyResult;\n  if (!isDefined(result.createCompany?.id)) {\n    throw new PdlOperationError('Failed to create company: no id returned.');\n  }\n  return result.createCompany.id;\n} catch (createCompanyError) {\n  if (isUniqueViolationError(createCompanyError)) {\n    return findCompanyId({ client, matchKeys: companyMatchKeys });\n  }\n  throw createCompanyError;\n}","preventionTips":["Validate the built company data against the workspace's required company fields before mutating.","Always inspect the client `errors` array when a result field is undefined.","Keep the unique-violation fallback as the documented race handler; extend it only for proven races.","Confirm the acting role has company-create permission in the workspace."],"tags":["pdl","people-data-labs","graphql","mutation","enrichment","company"],"backgroundTag":null,"analyzedSha":"1f5dd2bbd2a8da3419c8cfd52dd545c0024df1a6","analyzedAt":"2026-08-12T15:37:27.593Z","schemaVersion":2},"datasetVersion":"2026-08-12T18:17:37.767Z"}