Thursday, January 19, 2023

Table Methods in Ax 2012

Methods are used for adding X++ code to your application. The code in methods is also referred to as business logic. Whenever records are changed, inserted or deleted from a table various default methods are executed.
We can change the default methods and by doing so we are overriding the default methods.To override a method go to the Methods node of a table, right click and choose Override Method. Below are few examples of Overriding commonly used Table methods:

1.initValue():

If we create a new record from the table browser or a form the table method initValue() is executed. It is used to set a default value for the fields
Example (1): Let’s override intiValue for MyFirstTable and set default value for custGroupId

public void initValue()
{
super();
this.custGroupId = "10";
}

After adding this method, open table MyFirstTable  through Table browser and press ctrl+n to create a new record. The field custGroupId will now have the default value 10.


2.modifiedField():
this method is hit when a selected field is modified ,here in my secenario if custgroupid is modified then i am changing the currency code to empty.


public void modifiedField(fieldId _fieldId)
{
switch(_fieldId)
{
case fieldnum(MyFirstTable, custGroupId):
    this.CurrencyCode="";
    break;
default:
    super(_fieldId);
}
}
3.validateWrite():

Method validateWrite() will just check mandatory fields and is triggered when the record . Checks made by validateWrite() are the same as the super() call in validateField().So if your condition is not related to the value an application user enters in a specific field, you should put the validation in validateWrite().



4.validate field():

The field data comes from the master table. When I enter different data at the field level, validation is triggered on the field.

 public boolean validateField(FieldId _fieldIdToCheck)
{
boolean ret;
ret = super(_fieldIdToCheck);
OMOperatingUnit omOperatingUnit;
if (ret)
{
switch (_fieldIdToCheck)
{
case fieldNum(YNV_CostCenterOwners, CostCenterOperatingUnitNumber):
select Name, OMOperatingUnitNumber from omOperatingUnit
where omOperatingUnit.OMOperatingUnitType == OMOperatingUnitType::OMCostCenter
&& omOperatingUnit.OMOperatingUnitNumber == this.CostCenterOperatingUnitNumber;
   if (!omOperatingUnit.OMOperatingUnitNumber)
{
ret = checkFailed(strFmt("The field value %1 not available in CostCenter", this.CostCenterOperatingUnitNumber));
}
break;
case fieldNum(YNV_CostCenterOwners, BusinessUnitOperatingUnitNumber):
select Name, OMOperatingUnitNumber from omOperatingUnit
where omOperatingUnit.OMOperatingUnitType == OMOperatingUnitType::OMBusinessUnit
&& omOperatingUnit.OMOperatingUnitNumber == this.BusinessUnitOperatingUnitNumber;
if (!omOperatingUnit.OMOperatingUnitNumber)
{
ret = checkFailed(strFmt("The field value %1 not available in BusinessUnit", this.BusinessUnitOperatingUnitNumber));
}
break;
}
}

return ret;
}

master Data:-


Output:-








                                                                                                                                 

No comments:

Post a Comment

Automating Consolidate Account Updates for New Voucher Transactions in D365 Finance and Operations

  Ensure the  Consolidate Account  field is automatically updated for all new voucher transactions based on the  Consolidate MainAccount set...