Intercompany order connection

Legal Entity1
Legal Entity2
Legal Entity3

LE1 SO -> LE1 PO -> LE2 SO

LE1 SO -> LE1 PO Link:
salesLine.InventRefType == InventRefType::Purch
salesLine.InventRefTransId == purchLine.InventTransId
salesLine.InventRefId == purchLine.PurchId
LE1 PO -> LE1 SO Link:
purchLine.InventRefTransId == salesLine.InventTransId
purchLine.InventRefId == salesLine.SalesId

LE1 SO
salesLine.InterCompanyOrigin == InterCompanyOrigin::Source

LE1 PO -> LE2 SO Link
purchLine.InterCompanyOrigin == InterCompanyOrigin::Derived
purchLine.InterCompanyInventTransId == salesLine.InventTransId
LE2 SO -> LE1 PO Link
salesLine.InterCompanyOrigin == InterCompanyOrigin::Derived
salesLine.InterCompanyInventTransId == PurchLine.InventTransId

LE2 SO -> LE1 SO
SalesTable.InterCompanyOrder == NoYes::Yes
SalesTable.InterCompanyCompanyId == LE2
SalesTable.InterCompanyOriginalSalesId == SalesTable.SalesId
SalesTable.InterCompanyOriginalCustAccount

LE2 SO -> LE1 PO
SalesTable.InterCompanyOrder == NoYes::Yes
SalesTable.InterCompanyPurchId == PurchTable.PurchId

Intercompany links in UI
Path: Sales Order details / General / Related information / Related orders
Form: SalesTableReferences
Table: InterCompanyPurchSalesReference

Using the child sales line (LE2 SO), find the parent sales line (LE1 SO)
        SalesLine getSourceSalesLine(SalesLine _salesLine)
        {
            SalesLine sourceSalesLine;
            PurchLine sourcePurchLine;

            select firstonly crosscompany RecId from sourcePurchLine
                where sourcePurchLine.InventTransId == _salesLine.InterCompanyInventTransId
                join sourceSalesLine
                    where sourceSalesLine.DataAreaId == sourcePurchLine.DataAreaId
                       && sourceSalesLine.InventRefType == InventRefType::Purch
                       && sourceSalesLine.InventRefTransId == sourcePurchLine.InventTransId;

            return sourceSalesLine;
        }
Please be aware that intercompany order synchronization is written in the following way:
LE1 SO line created (after super)
LE1 PO line NOT created (synch executed before super)
LE2 SO line created (here you will not be able to select source SO line as PO line has not been inserted into the DB)

Find the root order line if the intercompany chain consists of more orders (LE1 SO -> LE1 PO -> LE2 SO -> LE2 PO -> LE3 SO):
        SalesLine salesLineUpdate = this; // this == SalesLine

        while (salesLineUpdate.InterCompanyInventTransId && salesLineUpdate.InterCompanyOrigin != InterCompanyOrigin::Source)
        {
            salesLineUpdate = getSourceSalesLine(salesLineUpdate);
        }
Using parent sales line (LE1 SO) find the child sales line (LE2 SO)
    SalesLine childSalesLine;
    PurchLine childPurchLine;

    if (this.InventRefTransId)
    {
        /*
        select firstonly crosscompany childPurchLine
            where  childPurchLine.DataAreaId        == this.DataAreaId
                && childPurchLine.ItemRefType       == InventRefType::Sales
                && childPurchLine.InventTransId     == this.InventRefTransId
            join forupdate  childSalesLine
                where childSalesLine.InventTransId == childPurchLine.InterCompanyInventTransId;
        // join select does not work as records are from different companies and on SQL level D365FO core sets T2.DATAAREAID = T1.DATAAREAID between joined datasources
        */
        select firstonly childPurchLine
            where  childPurchLine.DataAreaId        == this.DataAreaId
                && childPurchLine.ItemRefType       == InventRefType::Sales
                && childPurchLine.InventTransId     == this.InventRefTransId;

        select firstonly crosscompany childSalesLine
            where  childSalesLine.InventTransId             == childPurchLine.InterCompanyInventTransId
                && childSalesLine.InterCompanyInventTransId == childPurchLine.InventTransId;
    }



Support The Author

 If you found value in what I share, I've set up a Buy Me a Coffee page as a way to show your support.

Buy Me a Coffee

Post a Comment


All Comments


No comments. Be the first one to comment on this post.

Search

About

DaxOnline.org is free platform that allows you to quickly store and reuse snippets, notes, articles related to Dynamics 365 FO.

Authors are allowed to set their own "buy me a coffee" link.
Join us.

Blog Tags