Skip to main content

Notifications

Business Central forum
Suggested answer

Transferring a custom field From Sales Invoice To Posted Sales Invoice

Posted on by 43
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 43 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 60,993 Super User 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
  • Suggested answer
    Saurav.Dhyani Profile Picture
    Saurav.Dhyani 10,333 User Group Leader 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 43 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 10,333 User Group Leader 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

Helpful resources

Quick Links

Contextual content recommendations & SharePoint Q&A

Instantly access relevant sales materials…

Beyond the DRA Cloud Label Printing

Explore cloud-based label printing within Dynamics 365…

HR Infrastructure Customer merge

Important considerations for HR Customer migrations…

Leaderboard

#1
Andre Arnaud de Calavon Profile Picture

Andre Arnaud de Cal... 283,414 Super User

#2
Martin Dráb Profile Picture

Martin Dráb 223,394 Super User

#3
nmaenpaa Profile Picture

nmaenpaa 101,140

Featured topics

Product updates

Dynamics 365 release plans