Showing posts with label Pre Event Handler. Show all posts
Showing posts with label Pre Event Handler. Show all posts

Monday, October 28, 2019

Pre Event Handler

In Dynamics 365 F&O we are not allowed to add our custom logic in between the core logic but we can execute our custom logic before and after the core logic and this can be achieved by using the Event handlers.
Microsoft® recommends usage of event handlers in every possible place because it is the best and safest way to customize any existing functionality to accommodate user requirements.

The are two types of Event Handlers

Pre-Event Handler    (Executed before the core logic)

Post-Event Handler   (Executed after the core logic)
Click here for more details about event handlers.

In this blog i will show you how can we create a Pre-Event handler step by step.

Examples of Pre-Event Handler:

Let’s take an example of table "LedgerJournalTrans" it has a method of "Insert" and we want to verify the value of field "AmountCurCredit" that it should not be greater than 3000.

So here we can use the pre event handler which will be executed before the core insert method.
You can use the Pre-Event Handler for following purposes
1) Validation
2) update value
3) Assign values etc..

please follow the following steps
  1. Add a new class in your project and give it a proper name as "LedgerJournalTrans_Handler" or "LedgerJournalTrans_EventHandler"
  2. Search the "LedgerJournalTrans" in the Tables node of Application Explorer and right click on the table then click on "Open designer"

  3. Open the "Methods" node of table then find the "insert" method and right click on the method then go to "Copy event handler method" then click on "Pre-event handler"

  4. Now come to the class "LedgerJournalTrans_Handler" and "Paste" then your class look like as shown below

  5. Now add the following code in your method and your final class look like as shown in the screenshot below
    LedgerJournalTrans      ledgerJournalTrans = args.getThis();
    if(ledgerJournalTrans.AmountCurCredit > 3000)
    {
                Global::error('CreditCard amount must be less than 3000');
    }

Testing:
Now, to test your code,
go to General Ledger | Journal Entries | General Journal


create a new journal, click on lines, and create a new line with the credit amount "3001".
On saving, the system should throw the error
"Credit Amount must be less than 3000"
,
as shown in the following screenshot:



Monday, October 21, 2019

Update method parameters using Pre Event Handler

Update method parameters using pre event handler

Some time we need to update methods parameters value. In this blog i will show you that how can we achieve this using "Pre Event Handler" in Microsoft Dynamics 365 Finance and Operations.

Example:
Suppose we have multiple designs for "FreeTextInvoice" Report and we always want to pick the Design from "PrintManagement".
For achieve this requirement i am using out of the box form and create the "pre-event handler" of it's method to updates its parameters value

Form Name : CustFreeInvoice
Method Name : printInvoice



Please follow the following steps
  • Go to AOT using Visual Studio and Search "CustFreeInvoice" in Application Object Tree
  • Right click on CustFreeInovice inside the Forms node and click on "Open designer"
  • Search "printInvoice" in the Searchbar of the form
  • Right click on the "printInvoice" method then click on "Copy event handler method" then click on "Pre-event handler"
  • Create new class as "CustFreeInvoiceHandler" and Paste event handler in this class it looks like as shown below
  • Now add the following code to update the value of the parameter





Monday, July 22, 2019

Event Handlers

In Dynamics 365 F&O we are not allowed to add our custom logic in between the core logic but we can execute our custom logic before and after the core logic and this can be achieved by using the Event handlers.
Microsoft® recommends usage of event handlers in every possible place because it is the best and safest way to customize any existing functionality to accommodate user requirements.

The are two types of Event Handlers

Pre-Event Handler    (Executed before the core logic)

Post-Event Handler   (Executed after the core logic)


You can use Event Handler for following elements

1) Tables
2) Classes
3) Forms

Examples of Pre-Event Handler:

Let’s take an example of table "LedgerJournalTrans" it has a method of "Insert" and we want to verify the value of field "AmountCurCredit" that it should not be greater than 3000.

So here we can use the pre event handler which will be executed before the core insert method.
You can use the Pre-Event Handler for following purposes
1) Validation
2) update value
3) Assign values etc..

click here for more detail about "Creating a Pre-Event Handler"

Example of Post-Event Handler:

Let’s take the same example as above but here we just need to warn the user after the record is "Inserted" so we use the post event handler which throws the warning that the limit is exceeded.
So here we can use the Post Event Handler.

You can use the Post-Event Handler for following purposes
1) Update Values
2) Warnings etc..

click here for more details about "Creating a Post Event Handler"






Post Event Handler

In Dynamics 365 F&O we are not allowed to add our custom logic in between the  core logic  but we can execute our custom logic before ...