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 :
Small and medium business | Business Central, N...
Unanswered

Find Sales Order No. From Posted Sales Credit Memo Line

(1) 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;
```
I have the same question (0)
  • Suggested answer
    Sumit Singh Profile Picture
    1,443 on at
    Find Sales Order No. From Posted Sales Credit Memo Line
    Alternative Strategy: Use "Sales Shipment Line" → "Order No."
    Since the Sales Credit Memo Line often references a Return Receipt Line, and that in turn references a Sales Shipment, you can try this path:
    1. From the Credit Memo Line, get the Return Receipt Line.
    2. From the Return Receipt Line, get the Item Ledger Entry.
    3. From the Item Ledger Entry, get the Sales Shipment Line using Document No. and Document Line No.
    4. From the Sales Shipment Line, retrieve the Order No..
    This is already partially in your code, but you may need to expand the fallback logic to include:
    • Cross-reference via Serial/Lot No. if available.
    • Use Tracking Entries if you’re using item tracking.
    • Use custom extension logic to store the originating Sales Order No. during shipment or return.
    Suggested Enhancement to Your Code
    Here’s a refined version of your fallback logic:
    if SalesOrderNo = '' then begin
        // Try to get Sales Shipment Line directly from Return Receipt Line
        if ReturnReceiptLine.Get(CreditMemoLine."Return Receipt No.", CreditMemoLine."Return Receipt Line No.") then begin
            SalesShipmentLine.Reset();
            SalesShipmentLine.SetRange("Item No.", ReturnReceiptLine."Item No.");
            SalesShipmentLine.SetRange("Qty. Shipped", ReturnReceiptLine.Quantity); // Optional filter
            SalesShipmentLine.SetRange("Shipment No.", ReturnReceiptLine."Shipment No."); // If available
            if SalesShipmentLine.FindFirst() then
                SalesOrderNo := SalesShipmentLine."Order No.";
        end;
    end;

    Note: This answer is based on my understanding, with Microsoft Copilot helping me organize and polish the wording.
    Mark the Boolean true “Does this answer your Question” if this is Helpful.
     

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 > Small and medium business | Business Central, NAV, RMS

#1
Jeffrey Bulanadi Profile Picture

Jeffrey Bulanadi 3,878

#2
Sohail Ahmed Profile Picture

Sohail Ahmed 3,583 Super User 2025 Season 2

#3
Jainam M. Kothari Profile Picture

Jainam M. Kothari 3,175 Super User 2025 Season 2

Featured topics

Product updates

Dynamics 365 release plans