Skip to content

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 starts by hand. Its file workflow.yaml:

$schema: 'https://docs.acriva.de/schemas/workflow.schema.json'
format: 1
schemaVersion: 4
sdk: '1.0.0'
key: split-by-document-type
name: Nach Dokumenttyp aufteilen
template: custom
trigger: {}
fields: []
start: split
steps:
- 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: completed

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');
}
},
});

A test runs each of these cases in the sandbox and compares the result.

  • Input: none
  • Exit: contract
  • Changes: none
  • Input: none
  • Exit: other
  • Changes: none
  • Input: none
  • Exit: invoice
  • Changes: none