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

Notifications

Announcements

Community site session details

Community site session details

Session Id :
Small and medium business | Business Central, N...
Suggested Answer

Pass fields from sales order line to purchase order line on creating purchase order

(5) ShareShare
ReportReport
Posted on by 291
Hello, I want to pass the sales line document no. And sales line line no. To purchase line's sales order no. And sales order line no. Field respectively. This is to be done on the action of "create purchase order" I tried an event, but it is only filling up values in the last created purchase order from that sales order.
So, please suggest me with the code. 
 
 

[EventSubscriber(ObjectType::Codeunit, Codeunit::"Purch. Doc. From Sales Doc.", OnCreatePurchaseOrderOnAfterPurchaseHeaderSetFilters, '', true, true)]

 

    local procedure OnCreatePurchaseOrderOnAfterPurchaseHeaderSetFilters_process(var PurchaseHeader: Record "Purchase Header"; SalesHeader: Record "Sales Header")

 

    var

 

        pg: page "Purch. Order From Sales Order";

 

        PurchaseLine: Record "purchase line";

 

        SalesLine: Record "Sales line";

 

    begin

 

        SalesLine.Reset();

 

        SalesLine.setrange("Document No.", SalesHeader."No.");

 

        SalesLine.setrange("Document type", SalesHeader."Document Type");

 

        if SalesLine.FindSet() then begin

 

            repeat

 

                PurchaseLine.Reset();

 

                PurchaseLine.SetRange("Document No.", PurchaseHeader."No.");

 

                PurchaseLine.setrange("Document type", PurchaseHeader."Document Type");

 

                PurchaseLine.setrange("No.", SalesLine."No.");

 

                if PurchaseLine.Findfirst() then begin

 

                    PurchaseLine."AKI_SalesOrderNo." := SalesLine."Document No.";

 

                    PurchaseLine."AKI_SalesOrderLine No." := SalesLine."line No.";

 

                    PurchaseLine.Modify(false);

 

                end;

 

            until SalesLine.Next() = 0;

 

        end;

 

    end;

I have the same question (0)
  • Suggested answer
    TAHER Mehdi Profile Picture
    273 on at
    Instead of looping over all Sales Lines inside a header‑level event and trying to “find” matching Purchase Lines to update, we write the Sales references at the exact moment each Purchase Line is created. This avoids guesswork, race conditions, and mismatches, and guarantees the right Sales Line → Purchase Line linkage:
    
    [EventSubscriber(ObjectType::Codeunit, Codeunit::"Purch. Doc. From Sales Doc.", 'OnCopySalesLinesToPurchaseLinesOnBeforeInsert', '', true, true)]
    local procedure OnCopySalesLinesToPurchaseLinesOnBeforeInsert(
        var PurchaseLine: Record "Purchase Line";
        SalesLine: Record "Sales Line")
    begin
        PurchaseLine.Validate("AKI_SalesOrderNo.", SalesLine."Document No.");
        PurchaseLine.Validate("AKI_SalesOrderLine No.", SalesLine."Line No.");
    
     
  • Suggested answer
    OussamaSabbouh Profile Picture
    6,871 on at
    Hello,
     
    You’re using the wrong event. Your code runs at header level and tries to re-match lines by Item No., so only the last PO (or wrong line) gets updated. Instead, subscribe to the line-level event in codeunit “Purch. Doc. From Sales Doc.” that fires when each purchase line is created from a sales line, and set the Sales Order No./Line No. there. That guarantees a 1:1 match and fixes the issue.
     
    Regards,
    Oussama Sabbouh
  • Suggested answer
    YUN ZHU Profile Picture
    95,983 Super User 2025 Season 2 on at
    One more thing to add is that you should note the difference between the events for creating Purchase Orders and creating Purchase Invoices. You can look into the standard code.
     
    Thanks.
    ZHU

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

News and Announcements

Season of Giving Solutions is Here!

Quick Links

Responsible AI policies

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

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Small and medium business | Business Central, NAV, RMS

#1
OussamaSabbouh Profile Picture

OussamaSabbouh 1,688

#2
Khushbu Rajvi. Profile Picture

Khushbu Rajvi. 784 Super User 2025 Season 2

#3
YUN ZHU Profile Picture

YUN ZHU 595 Super User 2025 Season 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans