Send every posted sales invoice and credit memo in the right format
You do not have to build this flow yourself. The template Rechnung als E-Dokument versenden (send invoice as e-document, code E-DOC.SEND-INVOICE) from the AutoFlow template gallery handles all customers with one flow: outside Germany it creates a PDF, for German customers a ZUGFeRD, and for the customers you marked with the XRECHNUNG profile an XRechnung. It takes the recipient from the customer's Document Layouts, falls back to the customer's e-mail, and sends the document by e-mail.
The flow is a manual flow on the table Sales Invoice Header. You start it from the posted invoice list or card, or let a small second flow start it automatically after posting. For credit memos you adapt a copy, see Credit memos.
When started manually for Sales Invoice Header
-> Create Element "Beleg" (the document to send)
-> Decision: Customer gets XRechnung? (Document Sending Profile = XRECHNUNG)
Yes -> Create E-Document (XRECHNUNG) -> Set Element
No -> Decision: Customer gets ZUGFeRD? (Bill-to Country/Region Code = DE)
Yes -> Create E-Document (ZUGFERD, report Standard Sales - Invoice) -> Set Element
No -> Create PDF Document (report Standard Sales - Invoice) -> Set Element
-> Send Email (recipient from Document Layouts, else customer e-mail; attachment = "Beleg")
Before you start
- AutoFlow E-Document for Germany is installed, next to AutoFlow Core and AutoFlow.
- The two services exist without a service integration: Set up e-invoicing in Business Central. The template expects the codes
XRECHNUNGandZUGFERD. If yours differ, you change them in the two Create E-Document steps after installing. STANDARDis the default Document Sending Profile andXRECHNUNGis assigned where needed; every German customer carries the Country/Region CodeDE: Customer setup.- An e-mail account is set up in Business Central for the Send Email step.
Install the template
- Open the template in the AutoFlow Portal: Rechnung als E-Dokument versenden, and choose Use this template. Or open AutoFlow Template Gallery in Business Central, find the template by its name or code
E-DOC.SEND-INVOICE, and choose Create flow from template. How templates are installed and later updated is described in Creating flows from templates. - The flow card opens with a draft. The template's language is German, so the step labels are German (xRechnung erstellen, E-Mail senden, …). Rename them if you like.
- Check the points under Customize the flow, then publish the flow. A flow from a template is never activated by itself.
What the template does
| Step | What it does |
|---|---|
| When started manually for Sales Invoice Header | The flow runs on one posted invoice, {{trigger_Rec}}. It is assigned to the Posted Sales Invoices list and the Posted Sales Invoice card. |
| Create Element Beleg | A document element that later holds the file to send, whichever branch created it. |
| Decision Kunde bekommt xRechnung? | {{trigger_Rec.Debitor.Belegsendeprofil}} = XRECHNUNG: the customer's Document Sending Profile, read through a reference SmartField from the invoice to the customer. |
Create E-Document with service XRECHNUNG | File name Rechnung {{trigger_Rec.Nr}}.xml. |
| Decision Kunde bekommt ZUGFeRD? | The invoice's Bill-to Country/Region Code is DE. Only this code leads to ZUGFeRD; an empty code goes to the PDF path, see the warning below. |
Create E-Document with service ZUGFERD | Report Standard Sales - Invoice, file name Rechnung {{trigger_Rec.Nr}}.pdf. |
| Create PDF Document | For every other customer: report Standard Sales - Invoice, same file name. |
| Set Element after each of the three | Puts the created Document output into Beleg. |
| Send Email | To: the Send To Email from the customer's Document Layouts if there is one, otherwise the customer's E-Mail (a SmartControls {#if …#} in the field). Subject: Rechnung {{trigger_Rec.Nr}} an {{trigger_Rec.RechAnName}}. Attachment: Beleg. |
DEBusiness Central refuses to create an XRechnung, PEPPOL or ZUGFeRD document when the bill-to country of the posted invoice is empty (Bill-to Country/Region Code must have a value). The template therefore sends only the code DE to ZUGFeRD; a German customer with an empty code silently gets a PDF. Fill DE on all German customers before you use the flow. The CRONUS demo data leaves the code empty on every domestic customer. Invoices that were posted before you fixed the customer keep the empty code; post new ones to test.
Customize the flow
Change what differs in your company, then publish. Updating the flow from the gallery later keeps your changes, see Updating from the gallery.
- Service codes. In both Create E-Document steps pick your own E-Document Service if it is not called
XRECHNUNGorZUGFERD. The step label follows the service. - Layout. In the ZUGFeRD step and in Create PDF Document choose your own report layout, if you use one.
- File names. Both Create E-Document steps and the PDF step have a file name pattern,
Rechnung {{trigger_Rec.Nr}}by default. - E-mail. Sender account, subject and body in Send Email. Keep the recipient expression unless you want a different lookup; its order is Document Layouts first, customer e-mail second.
- Pages. On the trigger, Start from pages lists the posted invoice list and card. Remove a page or add others as you need.
Always insert placeholders with the SmartField picker (Alt+S); the tokens above are the ones the template ships with.
Start the flow
From the posted invoice. After publishing, the pages Posted Sales Invoices and Posted Sales Invoice show the action Start Flow. On the card it runs for the invoice you are looking at. On the list it runs once per selected invoice, so you can send a whole batch, for example at the end of the day. See Start the flow from a page.
Automatically after posting. Create a second, small flow that starts the template flow for every posted invoice:
- Trigger: When a record is inserted with Table No. = Sales Invoice Header. It fires when the posted invoice is written.
- Step: Start flow. The template flow appears in the step selector under its own name. Set Record = the trigger record.
- Publish. The template flow is queued in the background for each posted invoice; the posting itself does not wait for the e-mail.
The Document Posted trigger does not fit here: it runs just before the posting, when the posted invoice does not exist yet.
Credit memos
Posted sales credit memos need a copy of the flow with a few changes. Copy the installed flow (or install the template a second time) and change:
| Invoice flow | Credit memo flow | |
|---|---|---|
| Trigger table | Sales Invoice Header | Sales Cr.Memo Header |
| Start from pages | Posted sales invoices | Posted sales credit memos |
| Reference SmartFields on the trigger (customer, Document Layouts) | from Sales Invoice Header, Document Layouts usage Sales Invoice | from Sales Cr.Memo Header, Document Layouts usage Sales Credit Memo |
| Report in the ZUGFeRD step and in Create PDF Document | Standard Sales - Invoice | Standard Sales - Credit Memo |
| File names and subject | Rechnung {{…Nr}} | Gutschrift {{…Nr}} |
Everything else stays the same: the profile decision, the country decision and the recipient lookup. Pick every placeholder again with the picker in the new flow, because the SmartFields are defined per flow. For automatic sending, add a second starter flow on Sales Cr.Memo Header.
Variant: Business Central sends
With a service that has a connector, use it in the XRechnung or ZUGFeRD step. The step then creates and sends; the status is shown on the E-Documents page in Business Central. Remove Send Email from that path, or keep it if the customer should also get the file by e-mail. The workflow from step 3 of the setup must be enabled. The recipient of the connector is taken from Business Central, not from the flow.
Test it
- Post a sales invoice for one customer of each kind: outside Germany, German with the default profile (
STANDARD), German withXRECHNUNG. - Post one invoice for a customer with the address only in the Document Layouts, and one with the address only in the customer's E-Mail field.
- Select the invoices on Posted Sales Invoices and choose Start Flow.
- Open the flow's execution log and check which branch ran and which recipient Send Email used.
- Open E-Documents in Business Central: XRechnung and ZUGFeRD documents are listed there.
- Check the e-mails, their recipients and attachments (XML or PDF).
- Post a credit memo and repeat with the credit memo flow.
Set Test Mode on the AutoFlow Setup page in a sandbox, so test e-mails do not reach real customers.
Common errors
| Message or symptom | Cause | Fix |
|---|---|---|
| Missing IBAN or bank account | Company information incomplete | Add the bank account and IBAN. |
| Missing buyer reference or Leitweg-ID | Public-sector customer without it | Fill E-Invoice Routing No. on the customer. |
| Bill-to Country/Region Code must have a value | Country/Region Code is empty on the customer (the CRONUS demo data leaves it empty on domestic customers) | Set DE on the customer. Already posted invoices keep the empty code, so post a new one to test. |
| Missing bill-to address or city | Customer data incomplete | Complete the customer card. |
| E-Mail must have a value in Sales Header | The posted invoice's Sell-to E-Mail is empty. BC copies it from the customer when the document is created, so filling the customer afterwards does not help for existing documents | Fill E-Mail on the customer, then create and post a new invoice (or fill the e-mail on the open invoice before posting). |
The E-Document Service 'XRECHNUNG' does not exist (or ZUGFERD) | Your services have other codes | Pick your service in the two Create E-Document steps. |
| Send Email fails with empty recipients | Send To Email is empty in the Document Layouts and the customer has no E-Mail | Fill one of the two. |
| E-mail goes to the wrong address | An old Send To Email in the Document Layouts wins over the customer's E-Mail | Correct or clear the Document Layouts line. |
| Step fails for a credit memo | The invoice flow ran, or the credit memo copy still uses the report Standard Sales - Invoice | Use the credit memo flow with Standard Sales - Credit Memo, see Credit memos. |
| German customer gets a ZUGFeRD instead of an XRechnung | Profile is not XRECHNUNG (empty, STANDARD or spelled differently) | Check the profile code on the customer (XRECHNUNG, upper case). |
| Customer outside Germany gets a PDF although you expected a ZUGFeRD | Only the code DE leads to ZUGFeRD | By design. Check Country/Region Code on the customer. |
| Start Flow is missing on the posted invoice page | The flow is not published, or the page was removed under Start from pages | Publish the flow and check the trigger's page list. |