Cost center mapping from a data table
Like Cost center from the vendor, only the mapping isn’t in the script but in a data table kostenstellenZuordnung: one row per mapping with the vendor, the cost center and the G/L account, which accounting changes without a new version of the workflow, or uploads from Excel. The script queries every row of the vendor. With exactly one it sets the cost center and the G/L account, two fields the workflow declares itself; with none or several the approval gets a hint, so the invoice is allocated or split by hand. The vendor must be in the table as it is recognized on the invoice. When the table has one row per vendor, a card sets the invoice’s allocation with no script: Allocation from a data table.
The workflow
Section titled “The workflow”The workflow starts for every document of the type incoming-invoice. Its file workflow.yaml:
$schema: 'https://docs.acriva.de/schemas/workflow.schema.json'format: 1schemaVersion: 4sdk: '1.0.0'key: cost-center-mappingname: Kostenstellen-Zuordnung aus einer Datentabelletemplate: customtrigger: documentType: incoming-invoicefields: - key: costCenter kind: text labelDe: Kostenstelle labelEn: Cost center documentTypes: - incoming-invoice - key: glAccount kind: text labelDe: Sachkonto labelEn: G/L account documentTypes: - incoming-invoicestart: allocatesteps: - kind: script name: allocate label: Kostenstelle zuordnen script: allocate.ts hook: onDocumentArrived inputs: - kind: field key: vendorName type: text - kind: table key: kostenstellenZuordnung returns: [] exits: [] next: approve - kind: humanTask name: approve label: Freigabe taskKind: approve assignees: - when: null assignee: kind: costCenterResponsible fallback: kind: role role: Buchhaltung options: excludeRequester: true excludeActorsOfSteps: [] pickFile: false next: approved onRejected: rejected - kind: end name: approved label: Freigegeben outcome: approved - kind: end name: rejected label: Zurückgewiesen outcome: rejectedThe script allocate.ts
Section titled “The script allocate.ts”Hook: onDocumentArrived · Inputs: vendorName, data table kostenstellenZuordnung · Return values: none
import { defineScript } from '@acriva/workflow-sdk';
export default defineScript({ hook: 'onDocumentArrived', async run(ctx) { const vendor = ctx.document.fields.vendorName; if (vendor == null) return; // Every row of the vendor: a vendor may be mapped to more than one cost center. const [mapping, ...others] = await ctx.tables.query('kostenstellenZuordnung', { lieferant: vendor, }); if (mapping === undefined) { ctx.hint( `Für „${vendor}“ gibt es keine Kostenstellen-Zuordnung. Bitte kontieren Sie die Rechnung von Hand.`, ); return; } if (others.length > 0) { const costCenters = [mapping, ...others].map((row) => row.kostenstelle).join(', '); ctx.hint( `„${vendor}“ ist mehreren Kostenstellen zugeordnet (${costCenters}). Bitte teilen Sie die Rechnung von Hand auf.`, ); return; } if (mapping.kostenstelle) ctx.setField('costCenter', mapping.kostenstelle); if (mapping.sachkonto) ctx.setField('glAccount', mapping.sachkonto); },});What it does
Section titled “What it does”A test runs each of these cases in the sandbox and compares the result.
A vendor without a mapping
Section titled “A vendor without a mapping”- Input:
vendorNameNeue Firma AG - Data table
kostenstellenZuordnung: 3 rowsnummerZ-001,lieferantTelekom Deutschland GmbH,kostenstelle4400,sachkonto4920nummerZ-002,lieferantDeutsche Bahn AG,kostenstelle4300,sachkonto4663nummerZ-003,lieferantDeutsche Bahn AG,kostenstelle4100,sachkonto4663
- Exit: the default next step
- Changes: hint on the next task: “Für „Neue Firma AG“ gibt es keine Kostenstellen-Zuordnung. Bitte kontieren Sie die Rechnung von Hand.”
A vendor with one mapping
Section titled “A vendor with one mapping”- Input:
vendorNameTelekom Deutschland GmbH - Data table
kostenstellenZuordnung: 3 rowsnummerZ-001,lieferantTelekom Deutschland GmbH,kostenstelle4400,sachkonto4920nummerZ-002,lieferantDeutsche Bahn AG,kostenstelle4300,sachkonto4663nummerZ-003,lieferantDeutsche Bahn AG,kostenstelle4100,sachkonto4663
- Exit: the default next step
- Changes: field
costCenter=4400; fieldglAccount=4920
A vendor with several mappings
Section titled “A vendor with several mappings”- Input:
vendorNameDeutsche Bahn AG - Data table
kostenstellenZuordnung: 3 rowsnummerZ-001,lieferantTelekom Deutschland GmbH,kostenstelle4400,sachkonto4920nummerZ-002,lieferantDeutsche Bahn AG,kostenstelle4300,sachkonto4663nummerZ-003,lieferantDeutsche Bahn AG,kostenstelle4100,sachkonto4663
- Exit: the default next step
- Changes: hint on the next task: “„Deutsche Bahn AG“ ist mehreren Kostenstellen zugeordnet (4300, 4100). Bitte teilen Sie die Rechnung von Hand auf.”