Saturday, January 11, 2014

Simple way of using Query and Query Run in X++

Static void QueryAndQueryRun(Args _args)
{
     Query query;
     Query queryrun;
     PurchTable purchtable;

     query = new Query();
     query.addDatasource(tablenum(PurchTable)).addRange(fieldNum(PurchTable,PurchId)).Value
     (queryValue(PurchId00001); 


     /*Used to display the sql query as string eg Select * from purchtable where purchtable.PurchId == "PurchId00001"*/
     info(strfmt("%1",query.tostring()));
     
     queryrun = new QueryRun(query);
     
     /*query run has next() used to iterate through the query even though in this example we return only one matching record from purchtable */
     while(queryrun.next())
    {
          //query run has get() used return table buffer of the query. 
          purchtable = queryrun.get(tablenum(PurchTable));
          
          //throws info message of purchid. 
          info(strfmt("%1",purchtable.PurchId));          
    }    
}

No comments:

Post a Comment

Beautiful Fish Tank