Wednesday 3 December 2014

Dynamic Query creation and execution

X++ code to create dynamic query :
EcoResProductTranslation   ecores;
    InventTable inventtable;
     Query query = new Query();
    QueryRun    qr;
    QueryBuildDataSource    invent,ecorestrans;
    //SysTableLookup sysTableLookup = sysTableLookup::newParameters(tableNum(EcoResProductTranslation), this);
    ecorestrans = query.addDataSource(tableNum(EcoResProductTranslation));
    invent  =   ecorestrans.addDataSource(tableNum(InventTable));
    invent.addLink(fieldNum(InventTable,Product),fieldNum(EcoResProductTranslation,Product));
    qr=new QueryRun(query);
    while(qr.next())
    {
        ecores = qr.get(tableNum(EcoResProductTranslation));
        inventtable = qr.get(tableNum(InventTable));
        info(strFmt("%1-%2",inventtable.ItemId,ecores.Name));
    }
    pause;

Below is the select query statement
EcoResProduct   ecores;
    EcoResProductTranslation    ecorestrans;
    InventTable inventtable;
    while select * from ecorestrans
    join *  from inventtable   where  inventtable.Product   ==  ecorestrans.Product
    {
       info(strFmt("%1-%2",inventtable.ItemId,ecorestrans.Name));
    }