microsoft / AL

Home of the Dynamics 365 Business Central AL Language extension for Visual Studio Code. Used to track issues regarding the latest version of the AL compiler and developer tools available in the Visual Studio Code Marketplace or as part of the AL Developer Preview builds for Dynamics 365 Business Central.
MIT License
748 stars 245 forks source link

Event request. OnAfterCopyFromSalesHeader in T81.CopyFromSalesHeader #884

Closed DmitryKatson closed 6 years ago

DmitryKatson commented 7 years ago

Hi team. Please add OnAfterCopyFromSalesHeader event in T81.CopyFromSalesHeader . We need this if we add a new field in Sales header and want to store it in customer ledger entry.

I think the same should be in T81.CopyFromPurchHeader

gluboinc commented 7 years ago

Hi dkatson I can see you're event-request has been accepted, so maybe me commenting in this thread is wrong :) - sorry then.

I was trying the same as you - I think. Add a field in: Customer table/page, On to Sales Header, On to CU 80 On to Sales Invoice Header, On to Item Journal Line, CU22 On to Gen. Journal Line, CU12 On to Cust. Ledger Entry, and on to Item Ledger Entry.

Doing NAV for more than +20 years this is how I'm raised , and how it's done so far :)

So my approach to V2 extensions / Visual Studio- AL Code was the same - I expected to do the same tasks in Extensions, and maybe this is wrong and to long a way to accomplish the task....sure MS will do "best practice" at some time :)

My solution was this codeunit below - adding the table/page extensions is not included - this I know you know :) (Please know - the entire project I am happy to submit to you/MS as sample - if I maybe did it somehow right :))

codeunit 50130 HandleChainMgt {

// Transfer Field from Customer to Sales Header [EventSubscriber(ObjectType::Table,36,'OnAfterValidateEvent','Sell-to Customer No.',false,false)] procedure onvalidatesellto(var Rec : Record "Sales Header") var Cust : Record Customer; begin with rec do begin if "Sell-to Customer No." = '' then exit; if Cust.get("Sell-to Customer No.") then "Chain Office Code" := Cust."Chain Office Code"; end; end;

// Pre post check [EventSubscriber(ObjectType::Codeunit,80,'OnBeforePostSalesDoc','',false,false)] procedure check(VAR SalesHeader : Record "Sales Header") begin if SalesHeader."Chain Office Code" = '' then Error('Please fill in Chain Code') end;

// Copy form Sales Header to Sales Invoice Header [EventSubscriber(ObjectType::Codeunit,80,'OnBeforeSalesInvHeaderInsert','',false,false)] procedure copytoposteddoc(VAR SalesInvHeader : Record "Sales Invoice Header";VAR SalesHeader : Record "Sales Header") begin SalesInvHeader."Chain Office Code" := SalesHeader."Chain Office Code"; end;

// copy from Sales Header to Item Jnl Line on Posting [EventSubscriber(ObjectType::Table,83,'OnAfterCopyItemJnlLineFromSalesHeader','',false,false)] procedure onaftercopyitemjnlline(VAR ItemJnlLine : Record "Item Journal Line";SalesHeader : Record "Sales Header")

begin ItemJnlLine."Chain Office Code" := SalesHeader."Chain Office Code" end;

// Transfer Item Jnl Value to Item Ledger [EventSubscriber(ObjectType::Codeunit,22,'OnAfterInitItemLedgEntry','',false,false)] procedure copytoledgentry(VAR NewItemLedgEntry : Record "Item Ledger Entry";ItemJournalLine : Record "Item Journal Line") begin NewItemLedgEntry."Chain Office Code" := ItemJournalLine."Chain Office Code"; end;

// transfer field to gen jnl. line [EventSubscriber(ObjectType::Table,81,'OnAfterCopyGenJnlLineFromSalesHeader','',false,false)] procedure copytogenjnl(SalesHeader : Record "Sales Header";VAR GenJournalLine : Record "Gen. Journal Line") begin GenJournalLine."Chain Office Code" := SalesHeader."Chain Office Code"; end;

[EventSubscriber(ObjectType::Table,21,'OnAfterCopyCustLedgerEntryFromGenJnlLine','',false,false)] procedure copytoledger(VAR CustLedgerEntry : Record "Cust. Ledger Entry";GenJournalLine : Record "Gen. Journal Line") begin CustLedgerEntry."Chain Office Code" := GenJournalLine."Chain Office Code"; end; }

thpeder commented 7 years ago

@gluboinc it is not wrong at all, actually we are happy to see the community helping each other.

And as you pointed out in your code, this is already possible with the event OnAfterCopyGenJnlLineFromSalesHeader and likewise for purchase header.

DmitryKatson commented 7 years ago

@gluboinc thank you. Your code it correct, but from my point of view this one we don't need // Copy form Sales Header to Sales Invoice Header [EventSubscriber(ObjectType::Codeunit,80,'OnBeforeSalesInvHeaderInsert','',false,false)] procedure copytoposteddoc(VAR SalesInvHeader : Record "Sales Invoice Header";VAR SalesHeader : Record "Sales Header") begin SalesInvHeader."Chain Office Code" := SalesHeader."Chain Office Code"; end;

cos we have transferfields here image

And I found why I've created this issue. I create extension for NAV2017 (10.0.13682) and there are no such events image

and in NAV2018 (11.0.18712) they exist image

@thpeder thank you also for help.

So I'm wondering if Microsoft adds new events - they added only in NAV2018 or in NAV2017 also?

gluboinc commented 7 years ago

Hi Dkatson, you are quite right. The event to copy from Sales Header to Sales Invoice is not needed. I was just not sure transferfields would include any tableextension fields. Now I know :) And you have a point for NAV2017 or NAV2018 in case of new events added. I am using 2018 newest release where the requested event was avaliable. Only MS knows when/were they add events, maybe @thpeder can comment on this? Have a great weekend, Best Regards Morten

AlexanderYakunin commented 7 years ago

Hi! It was discussed multiple times in different places. We do not backport integration events, we add them for latest version as they primarily serve Extensions story for D365FF. You can add your events in On Prem code base for earlier versions (like NAV 2017) if needed as part of your customizations.

DmitryKatson commented 7 years ago

@AlexanderYakunin , thanks! Unfortunately, this is not possible, because if I will add Integration event in code base, I will not be able to make extension v.1 (generate .navx file)... are there any plans to add additional events also in NAV2017?

DmitryKatson commented 7 years ago

@gluboinc thanks! Have great weekend