Sub-Schemas from Extensions
A sub-schema is an XML schema linked to a main schema (Parent Schema Code) and identified by its Type Code, the document type code it writes (BT-3). mseDoc365 itself uses this for corrected invoices (384). An extension can use the same mechanism for other document type codes, for example 386 (prepayment invoice) or 389 (self-billed invoice), and can override which code a document gets.
Everything runs in codeunit mseBC mseDoc365 Sub Schema Mgt (5563636).
How the schema is picked
After a processor has prepared the data with its main schema, SelectSubSchema runs:
- Decide the type code (
GetSubSchemaTypeCode):OnBeforeGetSubSchemaTypeCode– setIsHandledto replace the standard rule.- Standard rule (only when not handled): a sales credit memo applied to an invoice gets 384.
OnAfterGetSubSchemaTypeCode– change the result.
OnBeforeSelectSubSchema– setIsHandledto choose the schema record yourself.- If the type code is not empty and the main schema has a sub-schema with this type code (same document type and direction), the sub-schema replaces the main schema. Otherwise the main schema stays.
An empty type code always means: use the main schema.
Events
[IntegrationEvent(false, false)]
local procedure OnBeforeGetSubSchemaTypeCode(var SourceRecRef: RecordRef; XMLSchema: Record "mseBC mseDoc365 XML Schema"; var TypeCode: Code[10]; var IsHandled: Boolean)
[IntegrationEvent(false, false)]
local procedure OnAfterGetSubSchemaTypeCode(var SourceRecRef: RecordRef; XMLSchema: Record "mseBC mseDoc365 XML Schema"; var TypeCode: Code[10])
[IntegrationEvent(false, false)]
local procedure OnBeforeSelectSubSchema(var SourceRecRef: RecordRef; var XMLSchema: Record "mseBC mseDoc365 XML Schema"; TypeCode: Code[10]; var IsHandled: Boolean)
| Parameter | Meaning |
|---|---|
SourceRecRef | The posted document, e.g. Sales Cr.Memo Header or Sales Invoice Header. Check SourceRecRef.Number() first. |
XMLSchema | The main schema chosen by the processor. In OnBeforeSelectSubSchema it can be replaced. |
TypeCode | The document type code, e.g. 384, 386. Empty = main schema. |
IsHandled | OnBeforeGetSubSchemaTypeCode: skip the standard rule. OnBeforeSelectSubSchema: skip the lookup; XMLSchema is used as you left it. |
Which event to use
- To add a type code for documents the standard rule does not cover, use
OnAfterGetSubSchemaTypeCode. - To replace the standard rule (e.g. never send 384 for some customers), use
OnBeforeGetSubSchemaTypeCodewithIsHandled. SetIsHandledonly for the documents you decide; it skips the standard rule for that document. - To pick a schema that is not a sub-schema of the main schema, use
OnBeforeSelectSubSchema.
Adding a new document type code
Example: send prepayment invoices as 386.
- Create the sub-schema. Copy the invoice schema (XRECHNUNG-OUT, document type SALES-INVOICE) with Add other XML Schemas, open the copy and set Parent Schema Code = XRECHNUNG-OUT and Type Code = 386. Set the type code element (BT-3) in its lines to the fixed value 386. Repeat for XRECHNUNG-UBL-OUT if you send by PEPPOL.
- Call the selection from the processor. Only the credit memo processor calls
SelectSubSchemain the standard. For another document, subscribe to that processor'sOnFinalizePrepareDataand call it; for sales invoices this is codeunit mseBC mseDoc365 Sales Inv.Docs. - Decide the type code in
OnAfterGetSubSchemaTypeCode.
codeunit 50100 "Prepayment Invoice 386"
{
// 1. Let mseDoc365 pick a sub-schema for sales invoices, after the invoice data is prepared.
[EventSubscriber(ObjectType::Codeunit, Codeunit::"mseBC mseDoc365 Sales Inv.Docs", 'OnFinalizePrepareData', '', false, false)]
local procedure SelectSubSchemaForInvoice(var SalesInvoiceHeaderOutbound: Record "Sales Invoice Header"; var mseDoc365XMLSchema: Record "mseBC mseDoc365 XML Schema"; var mseDoc365eDoc: Record "mseBC mseDoc365 eDoc"; var mseDoc365Address: Record "mseBC mseDoc365 Address" temporary; var TempSalesInvoiceLineBuffer: Record "Sales Invoice Line" temporary; var TempmseDoc365DocumentBuffer: Record "mseBC mseDoc365 Document" temporary; SchemaCode: Code[20]; DocumentType: Code[20]; var mseDoc365RecRefInstncs: Codeunit "mseBC mseDoc365 RecRefInstncs")
var
SubSchemaMgt: Codeunit "mseBC mseDoc365 Sub Schema Mgt";
SourceRecRef: RecordRef;
begin
SourceRecRef.GetTable(SalesInvoiceHeaderOutbound);
SubSchemaMgt.SelectSubSchema(SourceRecRef, mseDoc365XMLSchema);
end;
// 2. Decide the type code: a prepayment invoice is 386.
[EventSubscriber(ObjectType::Codeunit, Codeunit::"mseBC mseDoc365 Sub Schema Mgt", 'OnAfterGetSubSchemaTypeCode', '', false, false)]
local procedure AskFor386(var SourceRecRef: RecordRef; XMLSchema: Record "mseBC mseDoc365 XML Schema"; var TypeCode: Code[10])
var
SalesInvoiceHeader: Record "Sales Invoice Header";
begin
if SourceRecRef.Number() <> Database::"Sales Invoice Header" then
exit;
SourceRecRef.SetTable(SalesInvoiceHeader);
if SalesInvoiceHeader."Prepayment Invoice" then
TypeCode := '386';
end;
}
If the invoice's main schema has no 386 sub-schema, the invoice goes out with the main schema as before.
More examples
Replace the standard rule – credit memos of one customer always stay 381:
[EventSubscriber(ObjectType::Codeunit, Codeunit::"mseBC mseDoc365 Sub Schema Mgt", 'OnBeforeGetSubSchemaTypeCode', '', false, false)]
local procedure KeepCreditNoteForCustomer(var SourceRecRef: RecordRef; XMLSchema: Record "mseBC mseDoc365 XML Schema"; var TypeCode: Code[10]; var IsHandled: Boolean)
var
SalesCrMemoHeader: Record "Sales Cr.Memo Header";
begin
if SourceRecRef.Number() <> Database::"Sales Cr.Memo Header" then
exit;
SourceRecRef.SetTable(SalesCrMemoHeader);
if SalesCrMemoHeader."Bill-to Customer No." = '10000' then begin
TypeCode := '';
IsHandled := true;
end;
end;
Choose the schema record yourself:
[EventSubscriber(ObjectType::Codeunit, Codeunit::"mseBC mseDoc365 Sub Schema Mgt", 'OnBeforeSelectSubSchema', '', false, false)]
local procedure PickSchemaMyself(var SourceRecRef: RecordRef; var XMLSchema: Record "mseBC mseDoc365 XML Schema"; TypeCode: Code[10]; var IsHandled: Boolean)
var
MySchema: Record "mseBC mseDoc365 XML Schema";
begin
if TypeCode <> '389' then
exit;
if MySchema.Get('MY-SELF-BILLING', XMLSchema."Document Type") then begin
XMLSchema := MySchema;
XMLSchema.SetRecFilter();
IsHandled := true;
end;
end;
Rules
- The type code only selects the sub-schema; the code written to BT-3 comes from the sub-schema's lines.
- Keep the sub-schema in the same document type and direction as its main schema.
- Subscribers run in no fixed order. If several extensions set the type code for the same document, the last one wins.
- The processor's data preparation is not changed by a sub-schema; only the schema used to write the XML changes.