Search through blog..

Saturday, October 27, 2012

Microsoft Dynamics Ax Table Method

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:

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.

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.

Example (2): Let’s now override modifiedField method for MyFirstTable and target is to set CurrencyCode to null when CustGroupId is modified.
public void modifiedField(fieldId _fieldId)
{
switch(_fieldId)
{
case fieldnum(MyFirstTable, custGroupId):
    this.CurrencyCode="";
    break;
default:
    super(_fieldId);
}
}
After adding this method, open table MyFirstTable using Table browser and try to modify custGroupId of an existing record, then you will notice that CurrencyCode is immediately set to blank.

ModifiedField() receives the field number of the active field as parameter. A switch statement is used to check which field is active. If none of the checked fields are active the super() call is executed instead.

orig():
A nice feature in Dynamics Ax is when a field value is modified, it is possible to re-call the value before the field was modified. This is made possible using orig() method. The field values can  retain their last committed value. The method orig() is used to get the stored value. Orig() will return an instance of the current table.
A single field value from orig() is retained by specifying the field. And When the record is committed orig() will be updated.
Syntax: print this.orig().custCurrencyCode;

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.

Example (3): Let’s override validateField for MyFirstTable to verify the condition that CustName must be have >3 characters.
public boolean validateField(fieldId _fieldIdToCheck)
{
    boolean ret;
    ret = super(_fieldIdToCheck);
    if (ret)
    {
    switch (_fieldIdToCheck)
    {
    case fieldnum(MyFirstTable, custName):
        if (strlen(this.custName) <= 3)
        ret = checkFailed("Customer name must be longer than 3 characters.");
    }
    }
    return ret;
}
After adding this method, open table MyFirstTable using Table browser and 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():
When deleting a record the method validateDelete() is first executed. If true, the method delete() will be called.

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.

Example (4): In this example, let’s see how to use the table methods to insert a record in MyFirstTable.
static void DataDic_InsertRecord(Args _args)
{
MyFirstTable myFirstTable;
;
ttsbegin;
myFirstTable.initValue();
myFirstTable.accountNum = "100";
myFirstTable.custName = "Alt. customer id 100";
myFirstTable.CurrencyCode = "USD";
if (myFirstTable.validateWrite())
myFirstTable.insert();
ttscommit;
}
InitValue() is called and will set the value of the field custGroupId. The record will only be inserted if validateWrite() is true. As all mandatory fields have a value, the record will be inserted.

Instead of calling insert() you could call write(). This will update an existing record, but if the record does not exist, a new record will be inserted.

The select keywords delete_from, insert_recordset and update_recordset make only one call to the database from the client when processing multiple records.

find() and exist():
You will find the methods find() and exist() on most tables. These are not overridden methods. The methods are usually created to fetch a single record using a unique index.It is recommend adding these two methods when creating a new table as sooner or later you will need these methods. 

Tip:
To get an overview of which methods are executed, you can override the methods you want to check and add a line printing a text in the InfoLog in each overridden method.
Alternatively, override a method and set a breakpoint at super(). When the breakpoint is executed the methods called can be seen in the stack trace window in the debugger.



 

7 comments:

Shankar said...

Thank you Ajit.

Unknown said...

thank you sir
i m new to ax 2012
i want override method at form
datasource and form method

Unknown said...

THANK YOU AJIT..VERY effective document..please update more.very easy to understand the concept...:)

Unknown said...

thnx..

Unknown said...

I have 2 fields 1.Start KM 2.End KM, I need to add this fields to 3.Total field in table, what code will be there ? please.

Ajit said...

Hi Zeeshan, From what I understand you would like to add (Start KM + End KM) and would like to see the result in third field (Total). You can use display fields for this purpose - Go through this TechNet link - https://msdn.microsoft.com/en-us/library/gg845841.aspx
Do let me know if you would need more help. Thanks.

Unknown said...

Hi Ajit do you provide on line training. Thanks