Skip to main content

Notifications

Small and medium business | Business Central, N...
Answered

Transferring a custom field From Sales Invoice To Posted Sales Invoice

Posted on by 45
Hi Guys,
 
I created a basic custom field on the Sales Header that needs to be transferred over the Sales Invoice header table. The custom field on the sales header gets populated by the same custom field on the customer table. I use the OnAfterGetCurrRecord Table Extension trigger to fetch the value from the customer custom field over to the Sales Header. This event is triggered both on the Sale Order Page as well as the Sales Invoice Page. Below is the Code:
 
tableextension 50208 /Customer Ext./ extends Customer
{
    fields
    {
        field(50200; /FIN Number/; Integer)
        {
            Caption = 'FIN Number';
            DataClassification = CustomerContent;
        }
    }
    fieldgroups
    {
        addlast(Brick; /FIN Number/) { }
    }
}
 
tableextension 50201 /Sales Header Ext./ extends /Sales Header/
{
    fields
    {
        field(50200; /FIN Number/; Integer)
        {
            Caption = 'FIN No.';
        }
    }
}
 
tableextension 50207 /Sales Invoice Header Ext./ extends /Sales Invoice Header/
{
    fields
    {
        field(50200; /FIN Number/; Integer)
        {
            Caption = 'FIN No.';
        }
    }
}
 
pageextension 50202 /Sales Order Ext./ extends /Sales Order/
{
    layout
    {
        addafter(/Sell-to Customer Name/)
        {
            field(/FIN Number/; Rec./FIN Number/)
            {
                ApplicationArea = All;
                caption = 'FIN No.';
                Enabled = false;
            }
        }
    }
 
    trigger OnAfterGetCurrRecord()
    var
        Cust: Record /Customer/;
    begin
        if Cust.get(rec./Sell-to Customer No./) then
            rec./FIN Number/ := cust./FIN Number/
        else
            rec./FIN Number/ := 0;
    end;
}
 
pageextension 50211 /Sales Invoice Ext./ extends /Sales Invoice/
{
    layout
    {
        addafter(/Sell-to Customer Name/)
        {
            field(/FIN Number/; Rec./FIN Number/) { ApplicationArea = All; Caption = 'FIN No.'; Enabled = false; }
        }
    }
 
    trigger OnAfterGetCurrRecord()
    var
        Cust: Record /Customer/;
    begin
        if Cust.get(rec./Sell-to Customer No./) then
            rec./FIN Number/ := cust./FIN Number/
        else
            rec./FIN Number/ := 0;
    end;
}
 
For some reason that I cannot seem to figure out or probably something in my code that I am missing when I invoice a sales order the information gets transferred correctly to the Sales Invoice header table. But When I post a Sales Invoice the FIN Number field does not transfer to the Sales Invoice Header table. 
 
What am I missing? I can confirm that the trigger events populated the fields on the sales order table correctly. So there is a value that should get transferred over to the sales invoice header. This happens for the Sales Order, but not when using the Sales Invoice. Both pages that I use the trigger on are based on the same Sales Header table. 
 
Any help or insights would be much appreciated. 
 
 
 
Categories:
  • Zafar LIT Profile Picture
    Zafar LIT 45 on at
    Transferring a custom field From Sales Invoice To Posted Sales Invoice
    @Yun Zhu
    Thank you for the great link and info. This speeds up any things that I do not yet fully grasp. 

    @Saurav
    Thank you for the tip on using the integration events. Using that actually solved the problem. For some reason I cannot mark your reply as the answer, the website keeps freezing on me. Probably something was not okay with using the afterGetCurrRecord Page extension method. 
  • Suggested answer
    YUN ZHU Profile Picture
    YUN ZHU 71,565 Super User 2024 Season 2 on at
    Transferring a custom field From Sales Invoice To Posted Sales Invoice
    Hi, hope the following helps.
    Dynamics 365 Business Central: How to flow/pass custom field values
     
    Thanks.
    ZHU
  • Verified answer
    Saurav.Dhyani Profile Picture
    Saurav.Dhyani 17,959 Super User 2024 Season 2 on at
    Transferring a custom field From Sales Invoice To Posted Sales Invoice
    Hi,
     
    it seems your field IDs (50200) are same in Sales Header and Sales Invoice Header.
     
    You don't have to do anything. Microsoft uses transfer fields for copying data from sales header to Posted Documents. Your field should flow without any custom code.
     
    You can surely write a better code to move value of field from customer to sales Header using the right event.
     
    codeunit 50100 MyCodeunit
    {
        [EventSubscriber(ObjectType::Table, Database::"Sales Header", 'OnAfterCopySellToCustomerAddressFieldsFromCustomer', '', false, false)]
        local procedure CopyCustomFieldValue(SellToCustomer: Record Customer; var SalesHeader: Record "Sales Header")
        begin
            SalesHeader."FIN Number" := SellToCustomer."FIN Number";
        end;
    }
     
    With above code you can remove all code that you have written on two pages (Sales order and Sales Invoice) in OnAfterGetCurrRecord.
     
    Let me know if you have any further questions.
     
    Regards,
    Saurav Dhyani
  • Zafar LIT Profile Picture
    Zafar LIT 45 on at
    Transferring a custom field From Sales Invoice To Posted Sales Invoice
    @Saurav
     
    All my text and code that I added suddenly disappeared so my question was not yet ready to be posted. I have corrected it now. 
  • Suggested answer
    Saurav.Dhyani Profile Picture
    Saurav.Dhyani 17,959 Super User 2024 Season 2 on at
    Posting From Sales Invoice To Posted Sales Invoice
    Hi,
     
    When you post a Sales Invoice or Invoice a Sale order this will automatically create a posted sales invoice. 
     
    Do you have any specific questions?
     
    Regards,
    Saurav Dhyani

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

November Spotlight Star - Khushbu Rajvi

Congratulations to a top community star!

Forum Structure Changes Complete!

🔔 Be sure to subscribe to the new forums you are interested in to stay up to date! 🔔

Dynamics 365 Community Platform update – Oct 28

Welcome to the next edition of the Community Platform Update. This is a status …

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 290,900 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 229,275 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans