Table Methods in Ax 2012
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 .
let's overwrite initvalue in our custom Table
public void initValue()
{
super();
this.custGroupId = "10";
this.CustAccount = "1101";
}
After adding this method, open custom table through Table browser and press ctrl+n to create a new record. The field custGroupId will now have the default value 10 and custaccount = 1101.
modifiedField():
Each time the value of a field is changed the method modifiedField() is called . It is useful to initialize the values of other fields if the value of the current field is changed.
And overwrite this in modified method of custom table it works when custgroupid is modified the currency value goes empty.
public void modifiedField(fieldId _fieldId)
{
switch(_fieldId)
{
case fieldnum(CusTable, custGroupId):
this.CurrencyCode ="";
//this.CurrencyCode="";
// this.orig();
break;
default:
super(_fieldId);
}
}
After adding this method, open custom table using Table browser and try to modify custGroupId of an existing record, then you will notice that CurrencyCode is immediately set to blank.
validateField():
Method validateField() is used for validation only and will return true or false . If the return value is false, the application user will be prevented to continue changing a field value.
public boolean validateField(fieldId _fieldIdToCheck)
{
boolean ret;
ret = super(_fieldIdToCheck);
if (ret)
{
switch (_fieldIdToCheck)
{
case fieldnum(CusTable, cusname):
if (strlen(this.cusname) <= 3)
ret = checkFailed("Customer name must be longer than 3 characters.");
}
}
return ret;
}
After adding this method open custom table press Ctrl+N, in the new record try to enter less than 3 characters for field custName, Ax will throw warning message stating “Customer name must be longer than 3 characters.” And you will be asked to enter value again. Thus we validate the data to be entered for a specific field.
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().
ValidateDelete():
While deleting a record if we want to put any validation we can use this method. Here once I delete a record populating a info that deleted record.
public boolean validateDelete()
{
boolean ret;
ret = super();
info(this.AccountNum);
return ret;
}
public boolean validateDelete()
{
boolean ret;
ret = super();
info(this.AccountNum);
return ret;
}
ValidateWrite():
This method will get to fire when we update a record. here I am using to check mandatory field foraddress AccountNum
public boolean validateWrite()
{
boolean ret;
;
if(this.Address != "")
ret = super();
else
warning(" Please fill the address value");
return ret;
}
This method will get to fire when we update a record. here I am using to check mandatory field for
public boolean validateWrite()
{
boolean ret;
;
if(this.Address != "")
ret = super();
else
warning(" Please fill the address value");
return ret;
}
insert() and update():
Insert() and update() are rarely overridden. However, if you need to ensure a field has a certain value upon inserting a record, you can initialize your field before calling super() in insert(). Some special cases might also require overriding these methods; for example, if you need to synchronize the content of a saved record to another table.
Using X++ for entering data requires a bit more than using the user interface like forms. Only the table methods called will be executed.
find() :-
All tables should have at least one find method that selects and returns one record
from the table that matches the unique index specified by the input parameters.
The last input parameter in a find method should be a Boolean variable
'forupdate' or 'update' that is defaulted to
can update the record that is returned by the find method.
static CusTable find(CustAccount _custAccount,
boolean _forUpdate = false)
{
Custable cusTable;
;
if (_custAccount)
{
if (_forUpdate)
custable.selectForUpdate(_forUpdate);
select firstonly cusTable
index hint AccountIdx
where cusTable.CustAccount== _custAccount;
}
return cusTable;
}
exists() :-
As with the find method, there should also exist an exists method.
It basically works the same as the find method, except that it just returns true if a
record with the unique index specified by the input parameter(s) is found.
In the next example from the InventTable you can see that it returns true if the
input parameter has a value AND the select statement returns a value.
static boolean exist(ItemId itemId)
{
return itemId && (select RecId from inventTable
index hint ItemIdx
where inventTable.ItemId == itemId
).RecId != 0;
}
Display Method:
Indicates that the methods return value is to be displayed on a forms (or) Reports .The value cannot be altered in the form or report
Take the new method in a table, and then drag that method into the grid and set data source properties. In that field is non-editable.
We can create display method on the
1. Table methods
2. Form methods
3. Form data source methods
4. Report methods
5. Report design methods
Display Name names ()
{
CustTable custTable;
;
return CustTable::find(this.CustAccount).Name;
}