From eaac569812fdf3116d9ecd41e3fb7c4b49fca1a5 Mon Sep 17 00:00:00 2001 From: Baptiste Devessier Date: Thu, 27 Nov 2025 18:56:56 +0100 Subject: [PATCH] Fix variable usage in Search Record workflow action (#16147) Closes https://github.com/twentyhq/twenty/issues/16141 --------- Co-authored-by: prastoin --- .../WorkflowEditActionFindRecords.tsx | 1 - .../components/WorkflowFindRecordsFilters.tsx | 12 +---- .../find-records.workflow-action.ts | 54 ++++++++++++++++++- .../src/types/FieldMetadataOptions.ts | 8 +-- 4 files changed, 57 insertions(+), 18 deletions(-) diff --git a/packages/twenty-front/src/modules/workflow/workflow-steps/workflow-actions/find-records-action/components/WorkflowEditActionFindRecords.tsx b/packages/twenty-front/src/modules/workflow/workflow-steps/workflow-actions/find-records-action/components/WorkflowEditActionFindRecords.tsx index 8bef879f5f1..8a8f290c57c 100644 --- a/packages/twenty-front/src/modules/workflow/workflow-steps/workflow-actions/find-records-action/components/WorkflowEditActionFindRecords.tsx +++ b/packages/twenty-front/src/modules/workflow/workflow-steps/workflow-actions/find-records-action/components/WorkflowEditActionFindRecords.tsx @@ -68,7 +68,6 @@ type FindRecordsFormData = { export type FindRecordsActionFilter = { recordFilterGroups?: RecordFilterGroup[]; recordFilters?: RecordFilter[]; - gqlOperationFilter?: JsonValue; }; export type FindRecordsActionOrderBy = { diff --git a/packages/twenty-front/src/modules/workflow/workflow-steps/workflow-actions/find-records-action/components/WorkflowFindRecordsFilters.tsx b/packages/twenty-front/src/modules/workflow/workflow-steps/workflow-actions/find-records-action/components/WorkflowFindRecordsFilters.tsx index 09871637665..734aa13f93f 100644 --- a/packages/twenty-front/src/modules/workflow/workflow-steps/workflow-actions/find-records-action/components/WorkflowFindRecordsFilters.tsx +++ b/packages/twenty-front/src/modules/workflow/workflow-steps/workflow-actions/find-records-action/components/WorkflowFindRecordsFilters.tsx @@ -10,7 +10,6 @@ import { type FindRecordsActionFilter } from '@/workflow/workflow-steps/workflow import { WorkflowVariablePicker } from '@/workflow/workflow-variables/components/WorkflowVariablePicker'; import { useRecoilCallback } from 'recoil'; -import { computeRecordGqlOperationFilter } from 'twenty-shared/utils'; export const WorkflowFindRecordsFilters = ({ objectMetadataItem, @@ -41,25 +40,16 @@ export const WorkflowFindRecordsFilters = ({ currentRecordFiltersCallbackState, ); - const gqlOperationFilter = computeRecordGqlOperationFilter({ - fields: objectMetadataItem.fields, - filterValueDependencies: {}, - recordFilters: currentRecordFilters, - recordFilterGroups: currentRecordFilterGroups, - }); - const newFilter = { recordFilterGroups: currentRecordFilterGroups, recordFilters: currentRecordFilters, - gqlOperationFilter, - } as FindRecordsActionFilter; + } satisfies FindRecordsActionFilter; onChange(newFilter); }, [ currentRecordFilterGroupsCallbackState, currentRecordFiltersCallbackState, - objectMetadataItem.fields, onChange, ], ); diff --git a/packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/record-crud/find-records.workflow-action.ts b/packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/record-crud/find-records.workflow-action.ts index d7fbbce49d3..25ffa8a0895 100644 --- a/packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/record-crud/find-records.workflow-action.ts +++ b/packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/record-crud/find-records.workflow-action.ts @@ -1,6 +1,14 @@ import { Injectable } from '@nestjs/common'; -import { resolveInput } from 'twenty-shared/utils'; +import { + FieldMetadataComplexOption, + FieldMetadataDefaultOption, +} from 'twenty-shared/types'; +import { + computeRecordGqlOperationFilter, + isDefined, + resolveInput, +} from 'twenty-shared/utils'; import { type WorkflowAction } from 'src/modules/workflow/workflow-executor/interfaces/workflow-action.interface'; @@ -10,6 +18,7 @@ import { } from 'src/engine/core-modules/record-crud/exceptions/record-crud.exception'; import { FindRecordsService } from 'src/engine/core-modules/record-crud/services/find-records.service'; import { ScopedWorkspaceContextFactory } from 'src/engine/twenty-orm/factories/scoped-workspace-context.factory'; +import { WorkflowCommonWorkspaceService } from 'src/modules/workflow/common/workspace-services/workflow-common.workspace-service'; import { WorkflowStepExecutorException, WorkflowStepExecutorExceptionCode, @@ -27,6 +36,7 @@ export class FindRecordsWorkflowAction implements WorkflowAction { private readonly findRecordsService: FindRecordsService, private readonly scopedWorkspaceContextFactory: ScopedWorkspaceContextFactory, private readonly workflowExecutionContextService: WorkflowExecutionContextService, + private readonly workflowCommonWorkspaceService: WorkflowCommonWorkspaceService, ) {} async execute({ @@ -64,9 +74,49 @@ export class FindRecordsWorkflowAction implements WorkflowAction { const executionContext = await this.workflowExecutionContextService.getExecutionContext(runInfo); + const { flatObjectMetadata, flatFieldMetadataMaps } = + await this.workflowCommonWorkspaceService.getObjectMetadataInfo( + workflowActionInput.objectName, + workspaceId, + ); + + const fields = flatObjectMetadata.fieldMetadataIds + .map((fieldId) => { + const field = flatFieldMetadataMaps.byId[fieldId]; + + if (!field) { + return null; + } + + return { + id: field.id, + name: field.name, + type: field.type, + label: field.label, + // Note: force cast is required until we deprecate the CreateFieldInput and UpdateFieldInput + // type derivation from the FieldMetadataDto + options: field.options as + | (FieldMetadataDefaultOption & { id: string })[] + | (FieldMetadataComplexOption & { id: string })[] + | null, + }; + }) + .filter(isDefined); + + const gqlOperationFilter = + workflowActionInput.filter?.recordFilters && + workflowActionInput.filter?.recordFilterGroups + ? computeRecordGqlOperationFilter({ + fields, + recordFilters: workflowActionInput.filter.recordFilters, + recordFilterGroups: workflowActionInput.filter.recordFilterGroups, + filterValueDependencies: {}, + }) + : {}; + const toolOutput = await this.findRecordsService.execute({ objectName: workflowActionInput.objectName, - filter: workflowActionInput.filter?.gqlOperationFilter, + filter: gqlOperationFilter, orderBy: workflowActionInput.orderBy?.gqlOperationOrderBy, limit: workflowActionInput.limit, workspaceId, diff --git a/packages/twenty-shared/src/types/FieldMetadataOptions.ts b/packages/twenty-shared/src/types/FieldMetadataOptions.ts index 977fe33f11f..e71fbdeafcb 100644 --- a/packages/twenty-shared/src/types/FieldMetadataOptions.ts +++ b/packages/twenty-shared/src/types/FieldMetadataOptions.ts @@ -15,13 +15,13 @@ export type TagColor = export class FieldMetadataDefaultOption { id?: string; - position!: number; - label!: string; - value!: string; + position: number; + label: string; + value: string; } export class FieldMetadataComplexOption extends FieldMetadataDefaultOption { - color!: TagColor; + color: TagColor; } type FieldMetadataOptionsMapping = {