Loop all tables

September 16, 2010 1 comment

There is a simple trick to loop all tables that are available in Ax. Just use the Dictionary-class that contains all information about tables.

Small example to list all tablenames + their corresponding Id.

1
2
3
4
5
6
7
8
9
10
static void JeDoe_listTables(Args _args)
{
    Dictionary  dictionary = new Dictionary();
    int         i;
    ;
    for (i=1 ; i<=dictionary.tableCnt() ; i++)
    {
        info(strfmt('%1;%2', dictionary.tableCnt2Id(i), tableid2name( dictionary.tableCnt2Id(i) )));
    }
}

When you combine this with a DictTable / DictField-object, there are lots of possibilities.

A few things you should know about temporary tables

May 10, 2010 2 comments

How do you set a table temporary:
  • You can set the property ‘temporary’ in the AOT to yes –> this table is always temporary
  • You can declare a buffer of a table and call the method setTemp() or setTempData(), from that moment on the buffer contains temporary data.
    CustTable custTable;
    ;
    custTable.setTmp();
    if (custTable.isTmp())
    {
        // Do something with your temporary table
    }
Things to know:

My opinion: Temporary tables are very useful and can help you to easily manipulate data you don’t need store permanently, but watch out where and how you use them.

List of mandatory fields on a table

February 22, 2010 No comment

When you want a list of all the mandatory field on table X, yould check all the properties on the table fields, or you could be lazy and write a small job that does the trick for you. Just replace TableId with your tablenum.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
static void CheckMandatoryFieldsOnTable(Args _args)
{
    DictTable dictTable;
    DictField dictField;
    int i;
    TableId tableId = tablenum(custtable);
    ;
    dictTable = new DictTable(tableId);
    for (i=1 ; i<=dictTable.fieldCnt() ; i++)
    {
        dictField = new DictField(tableId, dictTable.fieldCnt2Id(i));
        if (dictField.mandatory())
        {
            info(dictField.name());
        }
    }
}

Moving DB-objects to other layers … without losing data

February 14, 2010 No comment

Most of us have already been confronted with the problem of tables being inserted in the wrong layer, and when they already contain data it is very difficult to transfer these objects to the correct layer without losing data.

I found the following article on Axaptapedia that helps you with this issue: http://www.axaptapedia.com/Move_DB_objects_to_another_layer

Select a record from a table when you only have the tableId

July 16, 2009 1 comment

The following code can provide you a generic way to update a table when you only have the tableId.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public Common findRecord(TableId _tableId, RecId _recId, Boolean _forUpdate = false)
{
    Common      common;
    DictTable   dictTable;
    ;
    dictTable = new DictTable(_tableId);
    common = dictTable.makeRecord();
 
    common.selectForUpdate(_forUpdate);
 
    select common
    where common.RecId == _recId;
 
    return common;
}

If you want, you can even update fields in this common record. You can Access/edit these fields by using their Name or FieldNum. The method below will update a specific field in a table.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public void updateValue(TableId _tableId, RecId _recId, str _field, AnyType _value)
{
    Common      common;
    Int         fieldId;
    ;
    ttsbegin;
    common = findRecord(_tableId, _recId, true);
    fieldId = fieldname2id(_tableId,_field);
 
    if (fieldId &amp;&amp; _value)
    {
        common.(fieldId) = _value;
        common.update();
    }
    ttscommit;
}