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
- Add a new class in your project and give it a proper name as "LedgerJournalTrans_Handler" or "LedgerJournalTrans_EventHandler"
- Search the "LedgerJournalTrans" in the Tables node of Application Explorer and right click on the table then click on "Open designer"
- 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"
- Now come to the class "LedgerJournalTrans_Handler" and "Paste" then your class look like as shown below
- 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');
}
No comments:
Post a Comment