Wednesday, October 17, 2018

How to get all related table ids from code

We can loop all relations on a table by code.

static void wzhGetRelatedTableNames(Args _args)
    wblInExtCodeValueTable          wblInExtCodeValueTable;
    int                             mapId;
    TableName                       relatedTableName;
    TableId                         relatedTableId;
    Set                             tablesIdsSet    = new Set(Types::Integer);
    Set                             tablesNamesSet  = new Set(Types::String);
    TableId                         tableId         = tableName2id(tableStr(wblInExtCodeValueTable));
    Dictionary                      dictionary      = new Dictionary();
    SysDictTable                    dictTable       = dictionary.tableObject(tableId);
    DictRelation                    dictRelation    = new DictRelation(wblInExtCodeValueTable.TableId);
    int                             mapCnt          = dictTable.relationCnt();
    container                       ret ;            
    str                             relationName;
    //create a maps of literals for all tables from the table relations
    // so that we could get tables names based on their ids
    // and if any new relation will be added to multiple external codes table
    // it is present automatically in this view
    for (mapId=1; mapId <= mapCnt; mapId++)
        // elaborate if any table present many times
        relationName        = dictTable.relation(mapId);
            relatedTableId      = dictRelation.externTable();
            relatedTableName    = tableId2pname(relatedTableId);
            info(strFmt("Table %1 - %2", relatedTableId, relatedTableName));
    ret = [tablesIdsSet.pack(), tablesNamesSet.pack()];

It can be useful in cases when we need, say, to open a form with a related record.

Here you can find a more elaborated example.

No comments: