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)

joins in x++

(0) ShareShare
ReportReport
Posted on by 3,945

hi all ,

i have written one job , where i am joinning SalesTable and SalesLine 
Please have a look.

static void QueryExample(Args _args)

{
Query query;
QueryRun queryRun;
QueryBuildDatasource datasource;
CustAccount custAccount;
SalesId salesId = "000761";
SalesTable salesTable;
SalesLine salesLine;
InventDim inventDim;
;

query = new Query();

// Add SalesTable main datasource
datasource = query.addDataSource(tableNum(SalesTable));

//Add range
datasource.addRange(fieldNum(salesTable,SalesId)).value(salesId);

// Add child datasource "SalesLine" to previously created DS
datasource = datasource.addDataSource(tableNum(SalesLine));

// Set the join mode

datasource.joinMode(JoinMode::InnerJoin);

// Indicate you don't want to use relations automatically
datasource.relations(false);

// Add link between parent field and child field
datasource.addLink(fieldNum(SalesTable, SalesId),
fieldNum(SalesLine, SalesId));

queryRun = new QueryRun(query);

while (queryRun.next())
{
salesLine = queryRun.get(tableNum(SalesLine));
salesTable = queryRun.get(tableNum(salesTable));
//inventDim = queryRun.get(tableNum(InventDim));
info("Sales Id from sales Table:"+salesTable.SalesId);
info("Sales Id from Sales Line :"+salesLine.SalesId);
info("Item Id :"+salesLine.ItemId);
info("UnitPrice :"+int2str(salesLine.CostPrice));
info("Quantity :"+int2str(salesLine.SalesQty));
info("Total Cost :"+int2str(salesLine.LineAmount));
//info("Location Id :"+inventDim.inventDimId);

}
}

This code is working fine.

Now i want to join one more Table that InventDim to SalesLine , so for that i have added some codes but its not working . Please have a look to the code which i have further added :-

datasource = datasource.addDataSource(tableNum(InventDim));

datasource.addLink(fieldNum(SalesLine,InventDimId),fieldNum(InventDim, InventDimId));

Please tell me what is need to do here .
Thanks

*This post is locked for comments

I have the same question (0)
  • Jonathan  Halland Profile Picture
    11,310 on at

    What is the issue that you are experiencing...

    You may need to redeclare your join mode and relations(false) with the new datasource object

  • Gautam Profile Picture
    3,945 on at

    hi Halland  

    since i have added these  lines, in output nothing i am getting

    datasource = datasource.addDataSource(tableNum(InventDim));

    datasource.addLink(fieldNum(SalesLine,InventDimId),fieldNum(InventDim, InventDimId));

    inventDim = queryRun.get(tableNum(InventDim));

    Thanks

  • Verified answer
    Jonathan  Halland Profile Picture
    11,310 on at

    Hi Gautam.

    If I run your code exactly as provided on our system it works correctly, perhaps check your data that the salesline was created correctly...

    static void QueryExample(Args _args)
    {
        Query query;
        QueryRun queryRun;
        QueryBuildDatasource datasource;
        CustAccount custAccount;
        SalesId salesId = "SALESID"
        SalesTable salesTable;
        SalesLine salesLine;
        InventDim inventDim;
        ;
    
        query = new Query();
    
        // Add SalesTable main datasource
        datasource = query.addDataSource(tableNum(SalesTable));
    
        //Add range
        datasource.addRange(fieldNum(salesTable,SalesId)).value(salesId);
    
        // Add child datasource "SalesLine" to previously created DS
        datasource = datasource.addDataSource(tableNum(SalesLine));
    
        // Set the join mode
    
        datasource.joinMode(JoinMode::InnerJoin);
    
        // Indicate you don't want to use relations automatically
        datasource.relations(false);
    
        // Add link between parent field and child field
        datasource.addLink(fieldNum(SalesTable, SalesId), fieldNum(SalesLine, SalesId));
        
        datasource = datasource.addDataSource(tableNum(InventDim));
        datasource.addLink(fieldNum(SalesLine,InventDimId),fieldNum(InventDim, InventDimId));
    
        queryRun = new QueryRun(query);
    
        while (queryRun.next())
        {
            salesLine = queryRun.get(tableNum(SalesLine));
            salesTable = queryRun.get(tableNum(salesTable));
            inventDim = queryRun.get(tableNum(InventDim));
            //inventDim = queryRun.get(tableNum(InventDim));
            info("Sales Id from sales Table:"+salesTable.SalesId);
            info("Sales Id from Sales Line :"+salesLine.SalesId);
            info("Item Id :"+salesLine.ItemId);
            info("UnitPrice :"+int2str(salesLine.CostPrice));
            info("Quantity :"+int2str(salesLine.SalesQty));
            info("Total Cost :"+int2str(salesLine.LineAmount));
            info("Location Id :"+inventDim.inventDimId);
        }
    }
  • Suggested answer
    Anton Venter Profile Picture
    20,345 Super User 2025 Season 2 on at

    Hi, that means it's working, you are not getting any results because there are no records in the SalesLine table with matching records in the InventDim table (InventDimId field). Check the data in these tables.

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
Martin Dráb Profile Picture

Martin Dráb 4 Most Valuable Professional

#1
Priya_K Profile Picture

Priya_K 4

#3
MyDynamicsNAV Profile Picture

MyDynamicsNAV 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans