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...
Answered

Create Purchase Order from Sales Order - Copy Additional Fields

(3) ShareShare
ReportReport
Posted on by 701
We are using the Create Purchase Order from Sales Order function in BC online (Release 26.1).  We have some additional fields on the Sales Header that we want copied over to the Purchase Header.  I believe the CodeUnit 1314 "Purch. Doc. From Sales Doc." is what is called to create the Purchase Order Header and Lines.
 
But I am not sure where I can add some AL code to include the desired fields from the Sales Header to be copied to the Purchase Header.
 
Any thoughts?
I have the same question (0)
  • Suggested answer
    Mohamed Amine Mahmoudi Profile Picture
    26,554 Super User 2025 Season 2 on at
     
    You can use this events subscriber :
    Header :
    [EventSubscriber(ObjectType::Codeunit, Codeunit::"Purch. Doc. From Sales Doc.", 'OnCreatePurchaseHeaderOnBeforeInsert', '', false, false)]
        local procedure OnCreatePurchaseHeaderOnBeforeInsert(var PurchaseHeader: Record "Purchase Header"; SalesHeader: Record "Sales Header"; Vendor: Record Vendor)
        begin
            // Copy custom fields from SalesHeader to PurchaseHeader
            PurchaseHeader."My Custom Field 1" := SalesHeader."My Custom Field 1";
            PurchaseHeader."My Custom Field 2" := SalesHeader."My Custom Field 2";
            // Add as needed
        end;
     
    Lines :
     
    [EventSubscriber(ObjectType::Codeunit, Codeunit::"Purch. Doc. From Sales Doc.", 'OnCopySalesLinesToPurchaseLinesOnBeforeInsert', '', false, false)]
        local procedure OnCopySalesLinesToPurchaseLinesOnBeforeInsert(var PurchaseLine: Record "Purchase Line"; SalesLine: Record "Sales Line")
        begin
            // Copy custom fields from SalesLine to PurchaseLine
            PurchaseLine."My Custom Field 1" := SalesLine."My Custom Field 1";
            PurchaseLine."My Custom Field 2" := SalesLine."My Custom Field 2";
            // Add as needed
        end;
     
     
    Best regards,
    Mohamed Amine MAHMOUDI
  • ME-31032107-0 Profile Picture
    701 on at
     
    I think you are suggesting the creation of a new Codeunit, which I understand.  What action or event would call this new Codeunit?
     
    Thanks.
  • Suggested answer
    Mohamed Amine Mahmoudi Profile Picture
    26,554 Super User 2025 Season 2 on at
     
    Yes you need to create a new codeunit like this :
    codeunit 50100 MyPurchFromSalesSubscriber
    {
    	[EventSubscriber(ObjectType::Codeunit, Codeunit::"Purch. Doc. From Sales Doc.", 'OnCreatePurchaseHeaderOnBeforeInsert', '', false, false)]
        local procedure OnCreatePurchaseHeaderOnBeforeInsert(var PurchaseHeader: Record "Purchase Header"; SalesHeader: Record "Sales Header"; Vendor: Record Vendor)
        begin
            // Copy custom fields from SalesHeader to PurchaseHeader
            PurchaseHeader."My Custom Field 1" := SalesHeader."My Custom Field 1";
            PurchaseHeader."My Custom Field 2" := SalesHeader."My Custom Field 2";
            // Add as needed
        end;
    	
    	[EventSubscriber(ObjectType::Codeunit, Codeunit::"Purch. Doc. From Sales Doc.", 'OnCopySalesLinesToPurchaseLinesOnBeforeInsert', '', false, false)]
        local procedure OnCopySalesLinesToPurchaseLinesOnBeforeInsert(var PurchaseLine: Record "Purchase Line"; SalesLine: Record "Sales Line")
        begin
            // Copy custom fields from SalesLine to PurchaseLine
            PurchaseLine."My Custom Field 1" := SalesLine."My Custom Field 1";
            PurchaseLine."My Custom Field 2" := SalesLine."My Custom Field 2";
            // Add as needed
        end;
    }
    You need also to write your custom fields
     
    Best regards,
    Mohamed Amine MAHMOUDI
  • ME-31032107-0 Profile Picture
    701 on at
     
    I understand the creation and coding of the new Codeunit.  What I am missing is where/how to call the new CodeUnit.  Am I extending the Page "Purch. Order From Sales Order", or the table "Purchase Header", or somewhere else?
     
    Thanks.
  • Verified answer
    ME-31032107-0 Profile Picture
    701 on at
    Actually, I found this link that worked for me.  Codeunit is a little different than what was posted here.
     
     

    codeunit 50133 SOtoPO

    {

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

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

       begin

           PurchaseHeader."Vendor Order No." := SalesHeader."External Document No.";

           PurchaseHeader.Modify(true);

       end;

    }

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,810

#2
Khushbu Rajvi. Profile Picture

Khushbu Rajvi. 810 Super User 2025 Season 2

#3
YUN ZHU Profile Picture

YUN ZHU 676 Super User 2025 Season 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans