Friday, January 26, 2007

Dynamic analog of abstract macro

Palle Algemark have published an article witch solution of the same task which have been solved by abstract macro in my latest post. He uses table map and reflection code to achieve flexibility.

It is good, especially when you have ready to use map for all concrete tables. It is more difficult to create such map id you haven't.

For example you can not just declare fact, that you have same name for a field in each table; uou should: either repeatedly add declaration of all field mappings, or generate it by reflection code, or generate it in xpo.

The following dynamic code uses only list of tables and the fact, that ItemID has the same name:

static void Test_DictTab(Args _args)
{
container tables = [
tableNum(InventTable),
tableNum(InventJournalTable),
tableNum(InventJournalTrans)
];
int i;

void processTable(TableID _tableID)
{
SysDictTable table = SysDictTable::newTableId(_tableID);
Common record = table.makeRecord();
FieldID itemField = table.fieldName2Id(fieldStr(InventTable, ItemID));
;
while select count(recID) from record
where record.(itemField) == '001'
{
info(strFmt("%1 = %2", table.label(), record.RecId));
}
}
;
for(i=1; i<=conLen(tables); i++)
processTable(conPeek(tables, i));
}

2 comments:

  1. Ok, you want it ...

    Let's play hard with macros :-P

    First define macro named : test_crpt_chrtable

    And the content of this macro is ...

    #define.arg1(%2)
    #if.arg1(1)
    #test_crpt_all(%1a,%3,%4,%5,%6,%7,%8,%9,%10,%11,%12,%13,%14,%15,%16,%17,%18,%19,%20)
    #endif
    #if.arg1(2)
    #test_crpt_all(%1b,%3,%4,%5,%6,%7,%8,%9,%10,%11,%12,%13,%14,%15,%16,%17,%18,%19,%20)
    #endif
    #if.arg1(3)
    #test_crpt_all(%1c,%3,%4,%5,%6,%7,%8,%9,%10,%11,%12,%13,%14,%15,%16,%17,%18,%19,%20)
    #endif
    #if.arg1(4)
    #test_crpt_all(%1d,%3,%4,%5,%6,%7,%8,%9,%10,%11,%12,%13,%14,%15,%16,%17,%18,%19,%20)
    #endif
    #if.arg1(5)
    #test_crpt_all(%1e,%3,%4,%5,%6,%7,%8,%9,%10,%11,%12,%13,%14,%15,%16,%17,%18,%19,%20)
    #endif
    #if.arg1(6)
    #test_crpt_all(%1f,%3,%4,%5,%6,%7,%8,%9,%10,%11,%12,%13,%14,%15,%16,%17,%18,%19,%20)
    #endif
    #if.arg1(7)
    #test_crpt_all(%1g,%3,%4,%5,%6,%7,%8,%9,%10,%11,%12,%13,%14,%15,%16,%17,%18,%19,%20)
    #endif
    #if.arg1(8)
    #test_crpt_all(%1h,%3,%4,%5,%6,%7,%8,%9,%10,%11,%12,%13,%14,%15,%16,%17,%18,%19,%20)
    #endif
    #if.arg1(9)
    #test_crpt_all(%1i,%3,%4,%5,%6,%7,%8,%9,%10,%11,%12,%13,%14,%15,%16,%17,%18,%19,%20)
    #endif
    #if.arg1(10)
    #test_crpt_all(%1j,%3,%4,%5,%6,%7,%8,%9,%10,%11,%12,%13,%14,%15,%16,%17,%18,%19,%20)
    #endif
    #if.arg1(11)
    #test_crpt_all(%1k,%3,%4,%5,%6,%7,%8,%9,%10,%11,%12,%13,%14,%15,%16,%17,%18,%19,%20)
    #endif
    #if.arg1(12)
    #test_crpt_all(%1l,%3,%4,%5,%6,%7,%8,%9,%10,%11,%12,%13,%14,%15,%16,%17,%18,%19,%20)
    #endif
    #if.arg1(13)
    #test_crpt_all(%1m,%3,%4,%5,%6,%7,%8,%9,%10,%11,%12,%13,%14,%15,%16,%17,%18,%19,%20)
    #endif
    #if.arg1(14)
    #test_crpt_all(%1n,%3,%4,%5,%6,%7,%8,%9,%10,%11,%12,%13,%14,%15,%16,%17,%18,%19,%20)
    #endif
    #if.arg1(15)
    #test_crpt_all(%1o,%3,%4,%5,%6,%7,%8,%9,%10,%11,%12,%13,%14,%15,%16,%17,%18,%19,%20)
    #endif
    #if.arg1(16)
    #test_crpt_all(%1p,%3,%4,%5,%6,%7,%8,%9,%10,%11,%12,%13,%14,%15,%16,%17,%18,%19,%20)
    #endif
    #if.arg1(17)
    #test_crpt_all(%1q,%3,%4,%5,%6,%7,%8,%9,%10,%11,%12,%13,%14,%15,%16,%17,%18,%19,%20)
    #endif
    #if.arg1(18)
    #test_crpt_all(%1r,%3,%4,%5,%6,%7,%8,%9,%10,%11,%12,%13,%14,%15,%16,%17,%18,%19,%20)
    #endif
    #if.arg1(19)
    #test_crpt_all(%1s,%3,%4,%5,%6,%7,%8,%9,%10,%11,%12,%13,%14,%15,%16,%17,%18,%19,%20)
    #endif
    #if.arg1(20)
    #test_crpt_all(%1t,%3,%4,%5,%6,%7,%8,%9,%10,%11,%12,%13,%14,%15,%16,%17,%18,%19,%20)
    #endif
    #if.arg1(21)
    #test_crpt_all(%1u,%3,%4,%5,%6,%7,%8,%9,%10,%11,%12,%13,%14,%15,%16,%17,%18,%19,%20)
    #endif
    #if.arg1(22)
    #test_crpt_all(%1v,%3,%4,%5,%6,%7,%8,%9,%10,%11,%12,%13,%14,%15,%16,%17,%18,%19,%20)
    #endif
    #if.arg1(23)
    #test_crpt_all(%1w,%3,%4,%5,%6,%7,%8,%9,%10,%11,%12,%13,%14,%15,%16,%17,%18,%19,%20)
    #endif
    #if.arg1(24)
    #test_crpt_all(%1x,%3,%4,%5,%6,%7,%8,%9,%10,%11,%12,%13,%14,%15,%16,%17,%18,%19,%20)
    #endif
    #if.arg1(25)
    #test_crpt_all(%1y,%3,%4,%5,%6,%7,%8,%9,%10,%11,%12,%13,%14,%15,%16,%17,%18,%19,%20)
    #endif
    #if.arg1(26)
    #test_crpt_all(%1z,%3,%4,%5,%6,%7,%8,%9,%10,%11,%12,%13,%14,%15,%16,%17,%18,%19,%20)
    #endif

    ------------------------------------------------
    After that ... define macro named : test_crpt_all

    And the content of this macro is ...

    #define.arg1(%1)
    #define.arg2(%2)
    #if.Empty(%2)
    %1
    #endif
    #ifnot.Empty(%2)
    #test_crpt_chrtable(%1,%2,%3,%4,%5,%6,%7,%8,%9,%10,%11,%12,%13,%14,%15,%16,%17,%18,%19,%20)
    #endif

    ------------------------------------------------

    Finally ...

    Create a job like this ...

    static void test_crpt(Args _args)
    {

    ;

    #test_crpt_all(,9,14,6,15)('test');

    }

    ------------------------------------------------

    What will do this job ? heheheh

    Sorry but I'm going crazy ... it's friday and this week was so hard :D

    Kind Regards,

    Mkz.
    ------------------------
    http://www.trucosax.com
    A Dynamics Ax tricks site in Spanish language :)

    ReplyDelete
  2. Another sample ...

    static void test_crpt2(Args _args)
    {
    ;
    #test_crpt_all(,2,15,24)::#test_crpt_all(,9,14,6,15)('test');
    }

    Kind Regards,

    Mkz.
    -----------------------
    http://www.trucosAx.com
    A Dynamics Ax tricks site in Spanish language :)

    ReplyDelete