Skip to main content

Notifications

Announcements

No record found.

Business Central forum

Copy information from Customer table to SalesHeader table using AL in Business Central

Posted on by

Hi Business Central experts,

We have added one custom field to Business Central Customer table, and the same field to the SalesHeader table too, using AL. Now we hope to use the value from the customer table as a default for the value in the SalesHeader table: the value from the Customer table needs to be copied as soon as a customer id is chosen in the Sales Order screen.

Here's a forum post about NAV onpremises that explains how to do it:

https://community.dynamics.com/nav/f/34/t/262618

However, it seems I cannot do it in the same way: I can not find any Microsoft-written AL-code (and therefore no SalesHeader "OnValidate" trigger code either). Also it does not seem inline with the idea of writing extensions if I add my own code directly to a microsoft-written module.

Is there a current recipe on how to do this?
We have a cloud sandbox, plus I have a local docker sandbox too.

With best regards,

Wolter Kaper

Categories:
  • WolterKaper Profile Picture
    WolterKaper on at
    RE: Copy information from Customer table to SalesHeader table using AL in Business Central

    Here's a stylistic variation of the answer given by Kumar Jangiti.

    If you want the event trigger to be in a table extension instead of codeunit, then it can be done like below:

    tableextension 50106 MySalesHeader extends "Sales Header"

    {

       fields

       {

           //new fields go like this

           field(50107; "My New Field"; Boolean)

           {

           }

           //adding features to existing fields goes like this

           modify("Sell-to Customer No.")

           {

               trigger OnAfterValidate()

               var

                   RecCustomer: Record Customer;

               begin

                   RecCustomer.SetRange("No.", Rec."Sell-to Customer No.");

                   if RecCustomer.FindSet() then

                       Rec."My New Field" := RecCustomer."My New Field"

                   else

                       Rec."My New Field" := false

               end;

           }

       }

    }

    Both variations work equally well, as far as I can see. I thank Kumar Jangiti because most of the above was copied from his example.

  • Stefano Demiliani Profile Picture
    Stefano Demiliani 37,162 Most Valuable Professional on at
    RE: Copy information from Customer table to SalesHeader table using AL in Business Central

    The way to do as exactly like described above, so codeunit with an event subscriber. These are rules for event-based programming, valid also for C/AL if you don’t want to modify standard objects.

  • WolterKaper Profile Picture
    WolterKaper on at
    RE: Copy information from Customer table to SalesHeader table using AL in Business Central

    They're planning to put an answer in the docs:

    github.com/.../426

    The example given there by HornyId seems helpful. I am going to try it out.

    The example above by A.K. Jangiti is probably great too!

  • Verified answer
    Ajay Kumar Jangiti Profile Picture
    Ajay Kumar Jangiti 130 on at
    RE: Copy information from Customer table to SalesHeader table using AL in Business Central

    Hi,

    You can subscribe the OnAfterValidate event as per below example

    In this example customer table extended with "Customer Type" option field and the same field extended in sales header table

    to transfer the fields from Customer to Sales Header you have subscribe the OnAfterValidate event  

    codeunit 50100 SalesHeaderExt

    {

       trigger OnRun()

       begin      

       end;

       [EventSubscriber(ObjectType::Table,36,'OnAfterValidateEvent','Sell-to Customer No.',false,false)]

       local procedure ValidateCustomertype(var rec : Record "Sales Header";var xrec : Record "Sales Header";

       currFieldNo : Integer)  

    var

    RecCustomer : Record Customer;

    begin

    RecCustomer.SetRange("No.",rec."Sell-to Customer No.");

    IF RecCustomer.FindSet then

       rec."Customer Type" := RecCustomer."Customer Type"

       else

       rec."Customer Type" := 0;

    end;

    }

  • WolterKaper Profile Picture
    WolterKaper on at
    RE: Copy information from Customer table to SalesHeader table using AL in Business Central

    Can't find OnAfterValidate in the docs.

    docs.microsoft.com/.../dev-itpro

    There is an "OnValidate" event but it seems to be meant for newly added fields.

  • Stefano Demiliani Profile Picture
    Stefano Demiliani 37,162 Most Valuable Professional on at
    RE: Copy information from Customer table to SalesHeader table using AL in Business Central

    For an existing field, you ned to subscribe the OnAfterValidate event and write your code in this event.

  • WolterKaper Profile Picture
    WolterKaper on at
    RE: Copy information from Customer table to SalesHeader table using AL in Business Central

    I guess I need to write a table extension.

    I know how to do that and then add  a new field to the existing table, because that is the standard example.

    The example also tells how to add an "OnValidate" trigger to the NEW field.

    But how to do this for an existing field?

    E.g. how to find out the fields existing object ID? Or its field-name?

    An example would also be nice.

    Or a link to the AL manual page where it's explained.

  • Stefano Demiliani Profile Picture
    Stefano Demiliani 37,162 Most Valuable Professional on at
    RE: Copy information from Customer table to SalesHeader table using AL in Business Central

    To do this, just create an event subscriber to the OnAfterValidate event of the Sell to Customer No. field in Sales Header and in this subscriber function transfer your custom field to the Sales Header field you need.

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 Coming on 11/8!

In our never-ending quest to help the Dynamics 365 Community members get answers faster …

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... 291,269 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,198 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans