Split by document type
A workflow without a document type that you start by hand on any document: accounting approves an incoming invoice, the management reviews a contract, and accounting acknowledges every other document. Because the workflow has no document type, the script reads no fields, only the document’s type.
The workflow
Section titled “The workflow”The workflow starts by hand. Its file workflow.yaml:
$schema: 'https://docs.acriva.de/schemas/workflow.schema.json'format: 1schemaVersion: 4sdk: '1.0.0'key: split-by-document-typename: Nach Dokumenttyp aufteilentemplate: customtrigger: {}fields: []start: splitsteps: - kind: script name: split label: Nach Dokumenttyp aufteilen script: split.ts hook: onDocumentArrived inputs: [] returns: - key: kind type: enumeration values: - invoice - contract - other branch: kind exits: - value: invoice next: invoice - value: contract next: contract - value: other next: other next: other - kind: humanTask name: invoice label: Freigabe Rechnung taskKind: approve assignees: [] fallback: kind: role role: Buchhaltung options: excludeRequester: true excludeActorsOfSteps: [] pickFile: false next: approved onRejected: rejected - kind: humanTask name: contract label: Prüfung Vertrag taskKind: review assignees: [] fallback: kind: role role: Geschäftsführung options: excludeRequester: false excludeActorsOfSteps: [] pickFile: false next: approved onRejected: rejected - kind: humanTask name: other label: Kenntnisnahme taskKind: acknowledge assignees: [] fallback: kind: role role: Buchhaltung options: excludeRequester: false excludeActorsOfSteps: [] pickFile: false next: done - kind: end name: approved label: Freigegeben outcome: approved - kind: end name: rejected label: Zurückgewiesen outcome: rejected - kind: end name: done label: Erledigt outcome: completedThe script split.ts
Section titled “The script split.ts”Hook: onDocumentArrived · Inputs: none · Return values: kind (invoice, contract, other)
import { defineScript } from '@acriva/workflow-sdk';
export default defineScript({ hook: 'onDocumentArrived', outputs: { kind: ['invoice', 'contract', 'other'] }, run(ctx) { // The workflow has no document type, so it starts by hand on any document and reads no fields, only the type. switch (ctx.document.type) { case 'incoming-invoice': return ctx.branch('invoice'); case 'contract': return ctx.branch('contract'); default: // Every other type, also one the workspace adds later. ctx.log('Document type', ctx.document.type); return ctx.branch('other'); } },});What it does
Section titled “What it does”A test runs each of these cases in the sandbox and compares the result.
A contract
Section titled “A contract”- Input: none
- Exit:
contract - Changes: none
A letter
Section titled “A letter”- Input: none
- Exit:
other - Changes: none
An incoming invoice
Section titled “An incoming invoice”- Input: none
- Exit:
invoice - Changes: none