import { defineComponent, withApiName, validateCustomApiName, type ComponentDefinition } from './component'; import { fieldReferences, type ObjectScoped, manifestObject, manifestField, type ObjectReference, type DefinitionFieldReference, } from './custom-object'; import type { Field } from '../field'; import type { AnyCustomObjectDefinition, FieldValue } from '../../../core/platform/custom-objects/types'; import { Rule, type RuleProps, type RuleAction } from '../rule'; /** Flow actions accept shared writable field definitions as well as existing manifest fields. */ export type DefinedRuleAction = | (Omit & { targetField: ObjectScoped }) | (TObject extends AnyCustomObjectDefinition ? { [K in keyof TObject['fields']]: TypedRuleAction; }[keyof TObject['fields']] : never); export type DefineRecordTriggeredFlowProps = Omit< RuleProps, 'flowId' | 'apiName' | 'actions' > & { apiName: `${string}__c`; actions?: readonly DefinedRuleAction>[]; }; export function defineRecordTriggeredFlow( object: TObject, props: DefineRecordTriggeredFlowProps, ): Rule { validateCustomApiName(props.apiName); const { actions: inputActions, ...rest } = props; const actions = inputActions?.map((action) => ({ ...action, targetField: manifestField(object, action.targetField as DefinitionFieldReference), })); return recordTriggeredFlowComponent.register( withApiName( new Rule(manifestObject(object), { ...rest, flowId: rest.apiName, ...(actions != null ? { actions } : {}), }), rest.apiName, ), object, props, ); } type TypedRuleAction = TField extends { writable: true } ? { targetField: TField; fixedValue?: FieldValue; rqlFormula?: string; } : never; const recordTriggeredFlowComponent: ComponentDefinition = defineComponent({ type: 'CUSTOM_OBJECT_RECORD_TRIGGERED_FLOW', define: defineRecordTriggeredFlow, references: (_definition, object, props) => [ object, ...(props.actions ?? []).flatMap((action) => fieldReferences(object, action.targetField)), ], });