Skip to content

The open alternative to Salesforce, designed for AI.

Remap step variable references when duplicating a workflow (#26990)

Thomas Trompette8d14ec5e60ea
diff --git a/packages/twenty-server/src/modules/workflow/workflow-builder/utils/__tests__/remap-duplicated-step-destinations.util.spec.ts b/packages/twenty-server/src/modules/workflow/workflow-builder/utils/__tests__/remap-duplicated-step-destinations.util.spec.ts
index 743418a3f10..3c9cff1d96c 100644
--- a/packages/twenty-server/src/modules/workflow/workflow-builder/utils/__tests__/remap-duplicated-step-destinations.util.spec.ts
+++ b/packages/twenty-server/src/modules/workflow/workflow-builder/utils/__tests__/remap-duplicated-step-destinations.util.spec.ts
@@ -130,4 +130,78 @@ describe('remapDuplicatedStepDestinations', () => {
expect(initialLoopStepIds).toEqual(['cloned-loop-start']);
});
+
+ it('remaps loop back-edges and variable references to cloned steps', () => {
+ const sourceFind = buildStep({
+ id: 'source-find',
+ type: WorkflowActionType.FIND_RECORDS,
+ nextStepIds: ['source-iterator'],
+ });
+ const sourceIterator = buildStep({
+ id: 'source-iterator',
+ type: WorkflowActionType.ITERATOR,
+ settings: {
+ input: {
+ items: '{{source-find.all}}',
+ initialLoopStepIds: ['source-loop'],
+ },
+ },
+ } as unknown as Partial<WorkflowAction> & { id: string });
+ const sourceLoop = buildStep({
+ id: 'source-loop',
+ name: 'Log {{source-iterator.currentItem.id}}',
+ nextStepIds: ['source-iterator'],
+ settings: {
+ input: {
+ logicFunctionInput: {
+ companyId: '{{source-iterator.currentItem.id}}',
+ triggerId: '{{trigger.recordId}}',
+ },
+ },
+ },
+ } as unknown as Partial<WorkflowAction> & { id: string });
+
+ const { steps } = remapDuplicatedStepDestinations({
+ trigger: { ...TRIGGER, nextStepIds: ['source-find'] },
+ sourceToClonedPairs: [
+ {
+ source: sourceFind,
+ duplicated: { ...sourceFind, id: 'cloned-find', nextStepIds: [] },
+ },
+ {
+ source: sourceIterator,
+ duplicated: { ...sourceIterator, id: 'cloned-iterator' },
+ },
+ {
+ source: sourceLoop,
+ duplicated: { ...sourceLoop, id: 'cloned-loop', nextStepIds: [] },
+ },
+ ],
+ clonedStepIdBySourceStepId: new Map([
+ ['source-find', 'cloned-find'],
+ ['source-iterator', 'cloned-iterator'],
+ ['source-loop', 'cloned-loop'],
+ ]),
+ });
+
+ const [find, iterator, loop] = steps as unknown as {
+ name: string;
+ nextStepIds: string[];
+ settings: { input: Record<string, unknown> };
+ }[];
+
+ expect(find.nextStepIds).toEqual(['cloned-iterator']);
+ expect(iterator.settings.input).toEqual({
+ items: '{{cloned-find.all}}',
+ initialLoopStepIds: ['cloned-loop'],
+ });
+ expect(loop.name).toBe('Log {{source-iterator.currentItem.id}}');
+ expect(loop.nextStepIds).toEqual(['cloned-iterator']);
+ expect(loop.settings.input).toEqual({
+ logicFunctionInput: {
+ companyId: '{{cloned-iterator.currentItem.id}}',
+ triggerId: '{{trigger.recordId}}',
+ },
+ });
+ });
});
diff --git a/packages/twenty-server/src/modules/workflow/workflow-builder/utils/__tests__/remap-duplicated-step-variables.util.spec.ts b/packages/twenty-server/src/modules/workflow/workflow-builder/utils/__tests__/remap-duplicated-step-variables.util.spec.ts
new file mode 100644
index 00000000000..b7445137e70
--- /dev/null
+++ b/packages/twenty-server/src/modules/workflow/workflow-builder/utils/__tests__/remap-duplicated-step-variables.util.spec.ts
@@ -0,0 +1,47 @@
+import { remapDuplicatedStepVariables } from 'src/modules/workflow/workflow-builder/utils/remap-duplicated-step-variables.util';
+
+const CLONED_STEP_ID_BY_SOURCE_STEP_ID = new Map([['source', 'copy']]);
+
+describe('remapDuplicatedStepVariables', () => {
+ it('points references to a duplicated step at its clone', () => {
+ expect(
+ remapDuplicatedStepVariables(
+ 'Company {{source.id}} named {{source.name}} ({{source}})',
+ CLONED_STEP_ID_BY_SOURCE_STEP_ID,
+ ),
+ ).toBe('Company {{copy.id}} named {{copy.name}} ({{copy}})');
+ });
+
+ it('remaps references padded with spaces, which the resolver trims', () => {
+ expect(
+ remapDuplicatedStepVariables(
+ '{{ source.name }}',
+ CLONED_STEP_ID_BY_SOURCE_STEP_ID,
+ ),
+ ).toBe('{{ copy.name }}');
+ });
+
+ it('leaves trigger references, other steps and plain text untouched', () => {
+ expect(
+ remapDuplicatedStepVariables(
+ 'source {{trigger.recordId}} {{other.id}} {{sourceStep.id}}',
+ CLONED_STEP_ID_BY_SOURCE_STEP_ID,
+ ),
+ ).toBe('source {{trigger.recordId}} {{other.id}} {{sourceStep.id}}');
+ });
+
+ it('remaps nested values and keys without mutating the input', () => {
+ const input = {
+ values: ['{{source.id}}', null, 3, true],
+ record: { '{{source.id}}': '{{source.name}}' },
+ };
+
+ expect(
+ remapDuplicatedStepVariables(input, CLONED_STEP_ID_BY_SOURCE_STEP_ID),
+ ).toEqual({
+ values: ['{{copy.id}}', null, 3, true],
+ record: { '{{copy.id}}': '{{copy.name}}' },
+ });
+ expect(input.values[0]).toBe('{{source.id}}');
+ });
+});
diff --git a/packages/twenty-server/src/modules/workflow/workflow-builder/utils/remap-duplicated-step-destinations.util.ts b/packages/twenty-server/src/modules/workflow/workflow-builder/utils/remap-duplicated-step-destinations.util.ts
index 1bbaea539b3..9607ee96304 100644
--- a/packages/twenty-server/src/modules/workflow/workflow-builder/utils/remap-duplicated-step-destinations.util.ts
+++ b/packages/twenty-server/src/modules/workflow/workflow-builder/utils/remap-duplicated-step-destinations.util.ts
@@ -1,6 +1,7 @@
import { isDefined } from 'twenty-shared/utils';
import { WorkflowActionType } from 'twenty-shared/workflow';
+import { remapDuplicatedStepVariables } from 'src/modules/workflow/workflow-builder/utils/remap-duplicated-step-variables.util';
import { type WorkflowAction } from 'src/modules/workflow/workflow-executor/workflow-actions/types/workflow-action.type';
import { type WorkflowTrigger } from 'src/modules/workflow/workflow-trigger/types/workflow-trigger.type';
@@ -12,6 +13,19 @@ const remapIds = (
(stepId) => clonedStepIdBySourceStepId.get(stepId) ?? stepId,
);
+const remapStepSettingsVariables = <TStep extends WorkflowAction>(
+ step: TStep,
+ clonedStepIdBySourceStepId: Map<string, string>,
+): TStep => {
+ return {
+ ...step,
+ settings: remapDuplicatedStepVariables(
+ step.settings,
+ clonedStepIdBySourceStepId,
+ ),
+ };
+};
+
export const remapDuplicatedStepDestinations = <
TTrigger extends WorkflowTrigger,
>({
@@ -70,6 +84,6 @@ export const remapDuplicatedStepDestinations = <
};
}
- return remappedStep;
+ return remapStepSettingsVariables(remappedStep, clonedStepIdBySourceStepId);
}),
});
diff --git a/packages/twenty-server/src/modules/workflow/workflow-builder/utils/remap-duplicated-step-variables.util.ts b/packages/twenty-server/src/modules/workflow/workflow-builder/utils/remap-duplicated-step-variables.util.ts
new file mode 100644
index 00000000000..37ca75bd40e
--- /dev/null
+++ b/packages/twenty-server/src/modules/workflow/workflow-builder/utils/remap-duplicated-step-variables.util.ts
@@ -0,0 +1,54 @@
+import { isString } from '@sniptt/guards';
+import { isDefined, isPlainObject } from 'twenty-shared/utils';
+import {
+ CAPTURE_ALL_VARIABLE_TAG_INNER_REGEX,
+ extractRawVariableNamePart,
+} from 'twenty-shared/workflow';
+
+const remapVariableTags = (
+ text: string,
+ clonedStepIdBySourceStepId: Map<string, string>,
+): string => {
+ return text.replace(
+ CAPTURE_ALL_VARIABLE_TAG_INNER_REGEX,
+ (variableTag, rawVariableName: string) => {
+ const stepId = extractRawVariableNamePart({
+ rawVariableName: rawVariableName.trim(),
+ part: 'stepId',
+ });
+ const clonedStepId = clonedStepIdBySourceStepId.get(stepId);
+
+ if (!isDefined(clonedStepId)) {
+ return variableTag;
+ }
+
+ return variableTag.replace(stepId, clonedStepId);
+ },
+ );
+};
+
+export const remapDuplicatedStepVariables = <TValue>(
+ value: TValue,
+ clonedStepIdBySourceStepId: Map<string, string>,
+): TValue => {
+ if (isString(value)) {
+ return remapVariableTags(value, clonedStepIdBySourceStepId) as TValue;
+ }
+
+ if (Array.isArray(value)) {
+ return value.map((item: unknown) =>
+ remapDuplicatedStepVariables(item, clonedStepIdBySourceStepId),
+ ) as TValue;
+ }
+
+ if (isPlainObject(value)) {
+ return Object.fromEntries(
+ Object.entries(value).map(([key, item]) => [
+ remapVariableTags(key, clonedStepIdBySourceStepId),
+ remapDuplicatedStepVariables(item, clonedStepIdBySourceStepId),
+ ]),
+ ) as TValue;
+ }
+
+ return value;
+};