The open alternative to Salesforce, designed for AI.
TypeScript crmcrm-systemcustomergood-first-issuegraphqlhacktoberfestjavascriptmarketingmonoreponestjsopen-sourcepostgresqlreactreactjssalestypescriptweb
Resolve select form fields of application workflows by universal iden… (#28658)
Arnav Singhe6b4af48d082
diff --git a/packages/twenty-docs/developers/extend/apps/logic/workflows.mdx b/packages/twenty-docs/developers/extend/apps/logic/workflows.mdxindex 3ec376b74fb..bb7f811832e 100644--- a/packages/twenty-docs/developers/extend/apps/logic/workflows.mdx+++ b/packages/twenty-docs/developers/extend/apps/logic/workflows.mdx@@ -101,6 +101,7 @@ Application workflows support these step types: `LOGIC_FUNCTION`, `AI_AGENT`, `HTheir inputs match the steps of the [workflow builder](/user-guide/workflows/capabilities/workflow-actions), with universal identifiers in place of names:- Record steps take `objectUniversalIdentifier` instead of an object name.- Filters and sorts point at fields with `fieldMetadataUniversalIdentifier`.+- Form fields of type `RECORD` name their object with `settings.objectUniversalIdentifier`. Fields of type `SELECT` and `MULTI_SELECT` offer the options of an existing field of the same type, named with `settings.selectedFieldUniversalIdentifier`.- To run your own code, use a `LOGIC_FUNCTION` step. The builder's Code step is not available.- Email and calendar steps run only if your app's role has the matching permission flag: `SystemPermissionFlag.SEND_EMAIL_TOOL` for `SEND_EMAIL` and `DRAFT_EMAIL`, `SystemPermissionFlag.CREATE_CALENDAR_EVENT_TOOL` for `CREATE_CALENDAR_EVENT`. The workspace grants it by installing the app, and the step can then use any connected account in the workspace, like a workflow built in the app.- Connected account ids are local to each workspace, so don't hardcode one. Pass the account as a variable. Email steps also take a workspace member id, such as `{{trigger.properties.after.assigneeId}}`, which resolves to that member's first account able to send.diff --git a/packages/twenty-server/src/engine/core-modules/application/application-manifest/converters/__tests__/from-workflow-step-manifest-to-action-or-throw.util.spec.ts b/packages/twenty-server/src/engine/core-modules/application/application-manifest/converters/__tests__/from-workflow-step-manifest-to-action-or-throw.util.spec.tsindex f264a6a408a..4a653bd6e58 100644--- a/packages/twenty-server/src/engine/core-modules/application/application-manifest/converters/__tests__/from-workflow-step-manifest-to-action-or-throw.util.spec.ts+++ b/packages/twenty-server/src/engine/core-modules/application/application-manifest/converters/__tests__/from-workflow-step-manifest-to-action-or-throw.util.spec.ts@@ -12,6 +12,7 @@ const REFERENCE_ID = '22222222-2222-4222-8222-222222222222';const FIELD_ID = '33333333-3333-4333-8333-333333333333';const LOCAL_ID = '44444444-4444-4444-8444-444444444444';const RECORD_FILTER_ID = '55555555-5555-4555-8555-555555555555';+const SELECT_FIELD_ID = '66666666-6666-4666-8666-666666666666';const references = {logicFunctionIdByUniversalIdentifier: new Map([[REFERENCE_ID, LOCAL_ID]]),agentIdByUniversalIdentifier: new Map([[REFERENCE_ID, LOCAL_ID]]),@@ -41,6 +42,17 @@ const references = {relationTargetObjectMetadataUniversalIdentifier: null,},],+ [+ SELECT_FIELD_ID,+ {+ id: 'stage-id',+ name: 'stage',+ objectUniversalIdentifier: REFERENCE_ID,+ type: FieldMetadataType.SELECT,+ settings: null,+ relationTargetObjectMetadataUniversalIdentifier: null,+ },+ ],]),};const record = {@@ -111,6 +123,13 @@ const inputs = {type: 'RECORD',settings: { objectUniversalIdentifier: REFERENCE_ID },},+ {+ id: SELECT_FIELD_ID,+ name: 'stage',+ label: 'Stage',+ type: 'SELECT',+ settings: { selectedFieldUniversalIdentifier: SELECT_FIELD_ID },+ },],HTTP_REQUEST: {method: 'POST',@@ -241,11 +260,47 @@ describe('application workflow actions', () => {).toMatchObject({loadBalance: { objectNameSingular: 'company', fieldName: 'owner' },});- expect(convert(WorkflowActionType.FORM).settings.input).toMatchObject([- { settings: { objectName: 'company' } },+ expect(convert(WorkflowActionType.FORM).settings.input).toEqual([+ { ...inputs.FORM[0], settings: { objectName: 'company' } },+ {+ ...inputs.FORM[1],+ settings: { selectType: 'EXISTING_FIELD', selectedFieldId: 'stage-id' },+ },]);});+ it('rejects a select form field whose source field is unavailable or of another type', () => {+ const formStepWith = (formField: Record<string, unknown>) =>+ workflowStepManifestSchema.parse({+ universalIdentifier: STEP_ID,+ name: 'Form',+ type: WorkflowActionType.FORM,+ input: [{ id: 'stage', name: 'stage', label: 'Stage', ...formField }],+ nextStepIds: [],+ });++ expect(() =>+ fromWorkflowStepManifestToActionOrThrow({+ step: formStepWith({+ type: 'SELECT',+ settings: { selectedFieldUniversalIdentifier: RECORD_FILTER_ID },+ }),+ index: 0,+ references,+ }),+ ).toThrow('missing field');+ expect(() =>+ fromWorkflowStepManifestToActionOrThrow({+ step: formStepWith({+ type: 'MULTI_SELECT',+ settings: { selectedFieldUniversalIdentifier: SELECT_FIELD_ID },+ }),+ index: 0,+ references,+ }),+ ).toThrow('must reference a MULTI_SELECT field');+ });+it('accepts record filters declared without an id or a value', () => {const step = workflowStepManifestSchema.parse({universalIdentifier: STEP_ID,diff --git a/packages/twenty-server/src/engine/core-modules/application/application-manifest/converters/from-workflow-step-manifest-to-action-or-throw.util.ts b/packages/twenty-server/src/engine/core-modules/application/application-manifest/converters/from-workflow-step-manifest-to-action-or-throw.util.tsindex ee0956b1d51..fab7044442e 100644--- a/packages/twenty-server/src/engine/core-modules/application/application-manifest/converters/from-workflow-step-manifest-to-action-or-throw.util.ts+++ b/packages/twenty-server/src/engine/core-modules/application/application-manifest/converters/from-workflow-step-manifest-to-action-or-throw.util.ts@@ -131,6 +131,37 @@ export const fromWorkflowStepManifestToActionOrThrow = ({}case 'FORM':input = step.input.map((formField) => {+ if (formField.type === 'SELECT' || formField.type === 'MULTI_SELECT') {+ const { selectedFieldUniversalIdentifier, ...settings } =+ formField.settings ?? {};+ if (typeof selectedFieldUniversalIdentifier !== 'string') {+ throw new ApplicationException(+ 'Select fields require the universal identifier of the field whose options they offer',+ ApplicationExceptionCode.INVALID_INPUT,+ {+ userFriendlyMessage: msg`The workflow select field must reference an existing field.`,+ },+ );+ }+ const selectedField = field(selectedFieldUniversalIdentifier);+ if (selectedField.type !== formField.type) {+ throw new ApplicationException(+ `Select field ${formField.name} must reference a ${formField.type} field`,+ ApplicationExceptionCode.INVALID_INPUT,+ {+ userFriendlyMessage: msg`The workflow select field must reference a field of the same type.`,+ },+ );+ }+ return {+ ...formField,+ settings: {+ selectType: 'EXISTING_FIELD',+ ...settings,+ selectedFieldId: selectedField.id,+ },+ };+ }if (formField.type !== 'RECORD') {return formField;}diff --git a/packages/twenty-shared/src/application/__tests__/workflowManifestType.spec.ts b/packages/twenty-shared/src/application/__tests__/workflowManifestType.spec.tsindex e0f714d084e..47ba9108320 100644--- a/packages/twenty-shared/src/application/__tests__/workflowManifestType.spec.ts+++ b/packages/twenty-shared/src/application/__tests__/workflowManifestType.spec.ts@@ -168,6 +168,45 @@ describe('workflow manifest step types', () => {expect(result.success).toBe(false);expect(result.error?.issues[0]?.message).toContain('Unsupported step type');});++ it.each(['SELECT', 'MULTI_SELECT'])(+ 'requires the field a %s form field takes its options from',+ (type) => {+ const withFormField = (settings: Record<string, unknown>) => {+ const definition = structuredClone(workflow);+ Object.assign(getStep(definition, 2), {+ type: 'FORM',+ input: [+ { id: 'stage', name: 'stage', label: 'Stage', type, settings },+ ],+ });+ return definition;+ };++ expect(+ workflowManifestSchema.safeParse(+ withFormField({+ selectedFieldUniversalIdentifier:+ '77777777-7777-4777-8777-777777777777',+ }),+ ).success,+ ).toBe(true);++ const result = workflowManifestSchema.safeParse(+ withFormField({ selectedFieldId: 'a-workspace-specific-id' }),+ );+ expect(result.success).toBe(false);+ expect(result.error?.issues[0]?.path).toEqual([+ 'version',+ 'steps',+ 2,+ 'input',+ 0,+ 'settings',+ 'selectedFieldUniversalIdentifier',+ ]);+ },+ );});describe('workflow manifest manual trigger', () => {diff --git a/packages/twenty-shared/src/application/workflowStepManifestType.ts b/packages/twenty-shared/src/application/workflowStepManifestType.tsindex 49b4938ab04..baa475efc32 100644--- a/packages/twenty-shared/src/application/workflowStepManifestType.ts+++ b/packages/twenty-shared/src/application/workflowStepManifestType.ts@@ -190,6 +190,20 @@ const workflowStepManifestOptions = ['Record picker fields require an object universal identifier',});}+ if (+ (field.type === 'SELECT' || field.type === 'MULTI_SELECT') &&+ !z+ .uuid()+ .safeParse(field.settings?.selectedFieldUniversalIdentifier)+ .success+ ) {+ context.addIssue({+ code: 'custom',+ path: ['settings', 'selectedFieldUniversalIdentifier'],+ message:+ 'Select fields require the universal identifier of the field whose options they offer',+ });+ }}),),),