Skip to main content

Notifications

Announcements

No record found.

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

  • Suggested answer
    Anton Venter Profile Picture
    Anton Venter 18,783 Super User 2024 Season 2 on at
    RE: joins in x++

    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.

  • Verified answer
    Jonathan  Halland Profile Picture
    Jonathan Halland 11,304 on at
    RE: joins in x++

    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);
        }
    }
  • Gautam Profile Picture
    Gautam 3,945 on at
    RE: joins in x++

    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

  • Jonathan  Halland Profile Picture
    Jonathan Halland 11,304 on at
    RE: joins in x++

    What is the issue that you are experiencing...

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

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,235 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans