I'm currently working my way through Event Handlers in D365 with Example.
As I understand this code:
class EventHandlerCustTable
{
[DataEventHandler(tableStr(CustTable), DataEventType::Inserted)]
public static void CustTable_onInserted(Common sender, DataEventArgs e)
{
CustTable custTable = sender as CustTable;
// 90 is the Customer group code for Intercompany Cutomer
if(custTable.CustGroup=="90")
{
// 80 is the Customer group code for Other Customer
custTable.CustGroup="80" ;
Global::error("Created Customer is an Intercompany Cutomer and will be added to the Customer Group Other Customer");
// you can write your logic here.
}
}
}
... when the value of custTable.CustGroup
is "90", the value should be reset to "80" and the error message should appear.
However, when I execute the code, the value is not changed. In fact, the screenshot of the article itself does not display this value as changed, though like my own local experience, the error message is in fact displayed.
So:
The blog article linked to in the question gets it almost right. However, the wrong event is used. The onInserted
event is triggered after the record creation has already happened. This is why modifying fields in this event makes no sense in this case.
The event to use is onInserting
, which is triggered while the record creation is happening.
Note that in the blog, they also first refer to the onInserting
event:
To understand this concept better, let's take the example of On Inserting Event Handler with Table.
But then in the "Example" section, they suddenly switch to the onInserted
event.
The Microsoft documentation has a nice example for this event, see Add methods to tables through extension.