Heygar
Heygar

Reputation: 553

How to make lookup of all fields in a table in ax 2012?

enter image description here

I have a form where the user can select any available table in the AOT in a dropdown/combobox (no. 1 where it says salestable)

Now i want to make a lookup in no.2 of all fields in the table selected in no. 1. I need to get this just based on the table name... I wish for the lookup to contain both the system name of the field and the label of the field. Is this possible? I have tried a good deal of solutions but i haven't been able to get it yet.

Any suggestions would be greatly appreciated.

Upvotes: 2

Views: 9928

Answers (3)

Heygar
Heygar

Reputation: 553

This is my final solution:

I made a regular table (TableFieldLookupTmp) containing the fields: tableId, label and field. I insert into this table from dicttable loop.

If the table already has been added before, i just do the lookup. It works fast.

Thanks for the help, guys.

public void lookup()

{

SysTableLookup SysTableLookup;

TableFieldLookupTmp TableFieldLookupTmp;

QueryBuildDataSource queryBuildDataSource;
QueryBuildRange     queryBuildRange;
Query query         = new Query();

tableName           tableName = 'SalesTable';
int tmpTableid =    tablename2id(tableName);
int i;

DictField           dictField;
DictTable dictTable = new DictTable(tableName2id(tableName)); 

if(!(select TableFieldLookupTmp where TableFieldLookupTmp.TablenumId == tmpTableid).recid)
{
    ttsBegin;
    if (dictTable)
    {
        for (i=1; i<=dictTable.fieldCnt(); i++)
        {
            dictField = new DictField(dictTable.id(), dictTable.fieldCnt2Id(i));

            if(subStr(dictField.name(),0,4) == "DEL_")
                continue;
            else
            {
                TableFieldLookupTmp.Label = dictField.label();
                TableFieldLookupTmp.field = dictField.name();
                TableFieldLookupTmp.TableNumId = tmpTableid;
                TableFieldLookupTmp.insert();
            }
        }
    }
    else
    {
        error(strFmt("Table '%1' not found!", tableName));
    }
    ttsCommit;

}


sysTableLookup = SysTableLookup::newParameters(tableNum(TableFieldLookupTmp), this);

sysTableLookup.addLookupField(fieldNum(TableFieldLookupTmp, field));
sysTableLookup.addLookupField(fieldNum(TableFieldLookupTmp, label));

queryBuildDataSource = query.addDataSource(tableNum(TableFieldLookupTmp));
queryBuildDataSource.addSortField(fieldNum(TableFieldLookupTmp, field), SortOrder::Ascending);
queryBuildRange = queryBuildDataSource.addRange(fieldNum(TableFieldLookupTmp, TablenumId));
queryBuildRange.value(int2str(tmpTableid));

//queryBuildRange.value('!DEL_*');

sysTableLookup.parmQuery(query);

sysTableLookup.performFormLookup();

//super();

}

Upvotes: 2

Alex Kwitny
Alex Kwitny

Reputation: 11564

There are two main ways to do it I can think of off hand. Reflection and using the SysModelELements table. The first way will give you the name & label, the second will not give you labels reliably.

The first job demonstrates this with reflection. The second shows it with the SysModelElements tables, and is faster, but won't give you the label. You can use a DictField object to enumerate the label (as shown in the second method).

The first method is the most reliable as the second could have some cases I haven't thought about such as table views or maps.

Method 1:

static void Job96(Args _args)
{
    str             tableName = 'SalesTable';
    DictTable       dictTable;
    DictField       dictField;
    int             i;

    dictTable = new DictTable(tableName2id(tableName));

    if (dictTable)
    {
        for (i=1; i<=dictTable.fieldCnt(); i++)
        {
            dictField = new DictField(dictTable.id(), dictTable.fieldCnt2Id(i));
            info(strFmt("Field Name: '%1'; Field Label: '%2'; IsSystem: %3",
                        dictField.name(),
                        dictField.label(),
                        dictField.isSystem()));
        }
    }
    else
    {
        error(strFmt("Table '%1' not found!", tableName));
    }
}

Method 2

static void Job97(Args _args)
{
    tableName           tableName = 'SalesTable';
    SysModelElement     tables;
    SysModelElement     fields;
    DictField           dictField;

    while select tables
        where tables.ElementType            == UtilElementType::Table       &&
              tables.Name                   == tableName
        join fields
            where fields.ElementType        == UtilElementType::TableField  &&
                  fields.ParentModelElement == tables.RecId
    {
        dictField = new DictField(tables.AxId, fields.AxId);

        info(strFmt("Field Name: '%1'; Field Label: '%2'; IsSystem: %3",
                        dictField.name(),
                        dictField.label(),
                        dictField.isSystem()));
    }
}

Upvotes: 1

AlexS
AlexS

Reputation: 21

Take a look at the forms SysQueryForm and SysQueryFieldLookUp. There you can see, how the standard handles this Problem.

Upvotes: 0

Related Questions