Saturday, April 27, 2019

How to publish your own Event in Business Central using AL Code?

Hi,

Publishing your own event means in a scenario where you are not able to find standard events , you can also define your event and publish it for use.

For Example:

In Job Journal Line page you have no event in posting date field.

Lets understand this exercise :

Step 1 :

Create a new Code unit and type teventint

Type your procedure Name and Parameters inside the procedure.


codeunit 50101 JobJournalLinePublisher
{
trigger OnRun()
begin

end;

[IntegrationEvent(true, true)]
procedure OnAfterValidatePostinDateinJobJournalLine(VAR JobJournalLine: Record "Job Journal Line"; var PostDate: Date);
begin

end;

var
myInt: Integer;
}

Step 2.  Define the place where you want to call this Publisher Event.

For me I need it on Page Job Journal Line After validation of No. Field.

pageextension 50100 MyExtension extends "Job Journal"
{
layout
{
// Add changes to page layout here
modify("No.")
{
trigger OnBeforeValidate();
var
Publisher: Codeunit 50101;
begin
Publisher.OnAfterValidatePostinDateinJobJournalLine(Rec, Rec."Posting Date");
end;
}

}

actions
{
// Add changes to page actions here
}

var
myInt: Integer;
}

Screen for reference:

Step 3. Define the Procedure defination and Subscribe.

codeunit 50102 JobJournalLineSubscribers
{
EventSubscriberInstance = StaticAutomatic;

[EventSubscriber(ObjectType::Codeunit, Codeunit::JobJournalLinePublisher, 'OnAfterValidatePostinDateinJobJournalLine', '', true, true)]
procedure OnAfterValidatePostinDateinJobJournalLine(VAR JobJournalLine: Record "Job Journal Line"; var PostDate: Date);
begin
JobJournalLine.VALIDATE(Description, JobJournalLine.Description + ' ' + FORMAT(PostDate));
end;
}

Screenshot for reference:

I hope with this reference you can define your own publisher event in business central.

3 comments:

  1. If this taken example is for only understanding the event publisher in AL then its ok.
    But i think we can use ONBeforeValidateEvnt standard event for acheive the same thing.

    ReplyDelete
  2. HOW Pedro Jerome GRANTED ME A BUSINESS LOAN!!!
    Hello everyone, I am Arnnette Speak from the USA and want to use this medium to express gratitude to Pedro Jerome for fulfilling his promise by granting me a loan, i was stuck in a financial situation and needed to refinance and pay my bills as well as start up a Business. I tried seeking for loans from various loan firms both private and corporate organizations but never succeeded and most banks declined my credit request. But as God would have it, i was introduced by a friend named Esther to Pedro Jerome LOAN FIRM and undergone the due process of obtaining a loan from the company, to my greatest surprise within 48hrs just like my friend Esther said, i was also granted a loan of $387,000.00 USD; So my advice to everyone who desires a loan, "if you must contact any firm with reference to securing a loan online with low interest rate of 3% and better repayment plans/schedule, please contact Pedro Jerome LOAN FIRM. Besides, he doesn't know that I am doing this but due to the joy in me, I'm so happy and wish to let people know more about this great company who truly gives out loans, it is my prayer that GOD should bless them more as they put smiles on peoples faces. You can contact them via email on {pedroloanss@gmail.com / whatsapp text: +1 863 231 0632 }

    ReplyDelete