web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics AX (Archived)

Convert X++ select statement to query (URGENT)

(0) ShareShare
ReportReport
Posted on by

I need to convert the following X++ select statement (working code) into Query to permanently filter PO form (already attempted, looking for other ways to implement/optimize/best practice):

while select purchReqTable where purchReqTable.ApprovalGroupId == 'HSE'
{
    select purchReqLine where purchReqLine.PurchReqId == purchReqTable.PurchReqId;

    if(purchReqLine.RecId>0)
    {
        while select purchTable where purchTable.PurchId == purchReqLine.PurchId
        {
            i++;
            info(int2str(i) + ') ' + purchTable.PurchId);
        }
    }
}

info('Total ' + int2str(i));

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Martin Dráb Profile Picture
    237,878 Most Valuable Professional on at

    Assuming that you want to filter the PurchTable datasource, you need something like this:

    QueryBuildDataSource purchTableDs = purchTable_ds.queryBuildDataSource();
    QueryBuildDataSource reqLineDs    = purchTableDs.addDataSource(tableNum(PurchReqLine));
    QueryBuildDataSource reqTableDs   = reqLineDsÄ›.addDataSource(tableNum(PurchReqTable));
    
    reqLineDs.relations(true);
    reqLineDs.joinMode(JoinMode::ExistsJoin);
    reqTableDs.relations(true);
    
    reqTableDs.addRange(fieldNum(PurchReqTable, ApprovalGroupId)).value(queryValue('HSE'));
    
  • Suggested answer
    Muthusamy Profile Picture
    4 on at

    Hi,

    You have the table relation of

    1. PurchReqLine.PurchReqTable = PurchReqTable.RecId

    2. PurchLine.PurchReqLineRefId  = PurchReqLine.LineRefId

    Create the join query and loop it.

    You can get the PurchId from PurchLine Table otherwise you can link PurchTable with PurchLine Table and get the data.

    Hope this relation will help you to write query.

    Thanks

  • Community Member Profile Picture
    on at

    Query returned empty after fixing error from purchTable_ds.queryBuildDataSource() and initialization error

  • Community Member Profile Picture
    on at

    @Martin

  • Verified answer
    Community Member Profile Picture
    on at

    Working Code (moved editted code from executeQuery to init method of PurchTable DataSource):

       //super();

       Query  query = new Query();

       QueryBuildDataSource purchTableDs;

       QueryBuildDataSource reqLineDs;

       QueryBuildDataSource reqTableDs;

       ;

       purchTableDs = query.addDataSource(tableNum(PurchTable));

       reqLineDs    = purchTableDs.addDataSource(tableNum(PurchReqLine));

       reqTableDs   = reqLineDs.addDataSource(tableNum(PurchReqTable));

       reqLineDs.relations(true);

       reqLineDs.joinMode(JoinMode::ExistsJoin);

       reqTableDs.relations(true);

       reqTableDs.addRange(fieldNum(PurchReqTable, ApprovalGroupId)).value(queryValue('HSE'));

       purchTable_ds.query(query);

    Many thanks to you all + @Martins

  • Martin Dráb Profile Picture
    237,878 Most Valuable Professional on at

    I wouldn't drop the standard query as you do; I would modify it as shown in my code snippet. You risk breaking a lot of things configured in datasources and done by code running before your code.

  • Community Member Profile Picture
    on at

    I agree with you... but the existing form (PurchTable) was duplicated (and renamed - HSE Purchse Orders) specifically for this purpose (HSE requirements).

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics AX (Archived)

#1
Priya_K Profile Picture

Priya_K 4

#1
Martin Dráb Profile Picture

Martin Dráb 4 Most Valuable Professional

#3
Ali Zaidi Profile Picture

Ali Zaidi 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans