web
You’re offline. This is a read only version of the page.
close
Skip to main content
Community site session details

Community site session details

Session Id :
Microsoft Dynamics 365 | Integration, Dataverse...
Unanswered

Find Sales Order No. From Posted Sales Credit Memo Line

(0) ShareShare
ReportReport
Posted on by 2
I am trying to retrieve the Sales Order Number from a Posted Sales Credit Memo Line. However, I encountered an issue where the Sales Order Number cannot be obtained because we are using Warehouse Management. As a result, the “Appl.-to Item Entry” or “Appl.-from Item Entry” fields are empty, which prevents me from linking the Credit Memo Line back to the original Sales Order. Below is the code I have implemented, but it still does not allow me to retrieve the Sales Order Number.
 
```AL
local procedure GetSourceSalesOrderNo(CreditMemoLine: Record "Sales Cr.Memo Line"): Code[20]
var
    ReturnReceiptLine: Record "Return Receipt Line";
    InboundILE: Record "Item Ledger Entry";
    OutboundILE: Record "Item Ledger Entry";
    ItemAppEntry: Record "Item Application Entry";
    SalesShipmentLine: Record "Sales Shipment Line";
    SalesOrderNo: Code[20];
begin
    SalesOrderNo := '';
    // ============================
    // Path 1: With Return Receipt
    // ============================
    if (CreditMemoLine."Return Receipt No." <> '') and 
       (CreditMemoLine."Return Receipt Line No." <> 0) then begin
        
        if ReturnReceiptLine.Get(CreditMemoLine."Return Receipt No.", CreditMemoLine."Return Receipt Line No.") then begin
            InboundILE.Reset();
            InboundILE.SetRange("Document No.", ReturnReceiptLine."Document No.");
            InboundILE.SetRange("Document Line No.", ReturnReceiptLine."Line No.");
            if InboundILE.FindFirst() then begin
                ItemAppEntry.Reset();
                ItemAppEntry.SetRange("Inbound Item Entry No.", InboundILE."Entry No.");
                if ItemAppEntry.FindFirst() then begin
                    if OutboundILE.Get(ItemAppEntry."Outbound Item Entry No.") then begin
                        SalesOrderNo := OutboundILE."Order No.";
                        // Fallback: if blank, try Sales Shipment Line
                        if SalesOrderNo = '' then begin
                            SalesShipmentLine.Reset();
                            SalesShipmentLine.SetRange("Document No.", OutboundILE."Document No.");
                            SalesShipmentLine.SetRange("Line No.", OutboundILE."Document Line No.");
                            if SalesShipmentLine.FindFirst() then
                                SalesOrderNo := SalesShipmentLine."Order No.";
                        end;
                    end;
                end;
            end;
        end;
    end;
    // ============================
    // Path 2: Direct Credit Memo (no return receipt)
    // ============================
    if (SalesOrderNo = '') then begin
        InboundILE.Reset();
        InboundILE.SetRange("Document Type", InboundILE."Document Type"::"Sales Credit Memo");
        InboundILE.SetRange("Document No.", CreditMemoLine."Document No.");
        InboundILE.SetRange("Document Line No.", CreditMemoLine."Line No.");
        if InboundILE.FindFirst() then begin
            ItemAppEntry.Reset();
            ItemAppEntry.SetRange("Inbound Item Entry No.", InboundILE."Entry No.");
            if ItemAppEntry.FindFirst() then begin
                if OutboundILE.Get(ItemAppEntry."Outbound Item Entry No.") then begin
                    SalesOrderNo := OutboundILE."Order No.";
                    // Fallback: check Sales Shipment Line if Order No. is still empty
                    if SalesOrderNo = '' then begin
                        SalesShipmentLine.Reset();
                        SalesShipmentLine.SetRange("Document No.", OutboundILE."Document No.");
                        SalesShipmentLine.SetRange("Line No.", OutboundILE."Document Line No.");
                        if SalesShipmentLine.FindFirst() then
                            SalesOrderNo := SalesShipmentLine."Order No.";
                    end;
                end;
            end;
        end;
    end;
    exit(SalesOrderNo);
end;
```
Categories:
I have the same question (0)

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Mansi Soni – Community Spotlight

We are honored to recognize Mansi Soni as our August 2025 Community…

Congratulations to the July Top 10 Community Leaders!

These are the community rock stars!

Leaderboard > Microsoft Dynamics 365 | Integration, Dataverse, and general topics

#1
Mansi Soni Profile Picture

Mansi Soni 243 Super User 2025 Season 2

#2
Muhammad Shahzad Shafique Profile Picture

Muhammad Shahzad Sh... 158 Most Valuable Professional

#3
Abhilash Warrier Profile Picture

Abhilash Warrier 95 Super User 2025 Season 2

Product updates

Dynamics 365 release plans