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)

QueryBuildDatasource addlink error between Purchline and vendInvoiceTrans

(0) ShareShare
ReportReport
Posted on by 916

Hi , can you please help me , why the last line on my code don't works ?

static void SAUInvoicedPO(Args _args)
{
Query query;
QueryRun qRun;
QueryBuildDatasource datasource;

VendInvoiceTrans vendInvoiceTrans;
VendInvoiceJour vendInvoiceJour;
PurchLine purchLine;
AccountingDistribution accountDistribution;

//VendInvoiceTrans
query = new Query();
datasource = query.addDataSource(tableNum(VendInvoiceTrans));

//VendInvoiceJour
datasource = datasource.addDataSource(tableNum(VendInvoiceJour));
datasource.joinMode(JoinMode::InnerJoin);
datasource.relations(true);
datasource.addRange(fieldnum(VendInvoiceJour,PurchId)).value("=Test1");
datasource.addRange(fieldnum(VendInvoiceJour,InvoiceDate)).value(queryRange(19\4\2016,20\04\2016));

//PurchLine
datasource = datasource.addDataSource(tableNum(PurchLine));
datasource.joinMode(JoinMode::InnerJoin);
datasource.relations(false);
datasource.addLink(fieldNum(vendInvoiceJour,PurchId), fieldNum(purchLine, PurchId));
datasource.addLink(fieldNum(vendInvoiceTrans, PurchaseLineLineNumber), fieldNum(purchLine,LineNumber));

//ACCOUNTINGDISTRIBUTION
datasource = datasource.addDataSource(tableNum(AccountingDistribution));
datasource.joinMode(JoinMode::InnerJoin);
datasource.addLink(fieldNum(accountingDistribution, SOURCEDOCUMENTLINE), fieldNum(purchLine,SOURCEDOCUMENTLINE));

qRun = new QueryRun(query);
while(qRun.next())
{
vendInvoiceTrans = qRun.get(tablenum(VendInvoiceTrans));
vendInvoiceJour = qRun.get(tablenum(VendInvoiceJour));
purchLine = qRun.get(tablenum(PurchLine));
accountDistribution = qRun.get(tablenum(AccountingDistribution));


info(strfmt('%1 ,%2 , %3, %4 %5 , %6, %7 %8',vendInvoiceJour.LedgerVoucher,
vendInvoiceTrans.PurchID,
vendInvoiceTrans.ItemId,
vendInvoiceTrans.Qty,
vendInvoiceTrans.PurchUnit
,PurchLine.REMAINPURCHFINANCIAL,
PurchLine.REMAINPURCHPHYSICAL
,accountDistribution.LEDGERDIMENSION

));


}
}

I have tested that on SQL and it 's works perfeclty

SELECT *

INTO #tmp

From MicrosoftDynamicsAX.dbo.VENDINVOICETRANS AS VENDINVOICETRANS

INNER JOIN MicrosoftDynamicsAx.dbo.VENDINVOICEJOUR AS VENDINVOICEJOUR
ON VENDINVOICEJOUR.PARTITION = @Partition
AND VENDINVOICEJOUR.PURCHID = VENDINVOICETRANS.PURCHID
AND VENDINVOICEJOUR.INVOICEID = VENDINVOICETRANS.INVOICEID
AND VENDINVOICEJOUR.INVOICEDATE = VENDINVOICETRANS.INVOICEDATE
AND VENDINVOICEJOUR.NUMBERSEQUENCEGROUP = VENDINVOICETRANS.NUMBERSEQUENCEGROUP
AND VENDINVOICEJOUR.INTERNALINVOICEID = VENDINVOICETRANS.INTERNALINVOICEID

INNER JOIN MicrosoftDynamicsAx.dbo.purchline AS PurchLine
On PurchLine.PARTITION = @Partition
AND PurchLine.PURCHID = VENDINVOICEJOUR.PURCHID
AND PurchLine.LINENUMBER = VENDINVOICETRANS.PURCHASELINELINENUMBER

INNER JOIN MicrosoftDynamicsAx.dbo.ACCOUNTINGDISTRIBUTION AS AccDISTRIBUTION
ON AccDISTRIBUTION.PARTITION = @Partition
AND AccDISTRIBUTION.SOURCEDOCUMENTLINE = PurchLine.SOURCEDOCUMENTLINE

WHERE VENDINVOICETRANS.PURCHID ='Test1'

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    reachnaidu Profile Picture
    890 on at

    Can you set relations to false and see if it works.

    Go through this sample and see if it works.

     query = new Query();

       qbds1 = query.addDataSource(tableNum(jmgtermreg));

       qr    = qbds1.addRange(fieldnum(jmgtermreg,jobactive));

       qr.value(queryvalue(1));

       qbds6 = qbds1.addDataSource(tableNum(EmplTable));

       qbds6.relations(false);

       qbds6.joinMode(joinmode::InnerJoin);

       qbds6.addLink(fieldnum(jmgtermreg,emplid),fieldnum(EmplTable,emplid));

       qbds7 = qbds6.addDataSource(tableNum(Dirpartytable));

       qbds7.relations(false);

       qbds7.joinMode(joinmode::InnerJoin);

       qbds7.addLink(fieldnum(EmplTable,partyid),fieldnum(Dirpartytable,partyid));

  • Verified answer
    Community Member Profile Picture
    on at

    Try to debug it, if you inspect query variable can see Transact SQL,

    NAME:
    query
    VALUE:
    Query object 1bdf37e0: SELECT * FROM VendInvoiceTrans(VendInvoiceTrans_1) JOIN * FROM VendInvoiceJour(VendInvoiceJour_1) ON VendInvoiceTrans.PurchID = VendInvoiceJour.PurchId AND VendInvoiceTrans.InvoiceId = VendInvoiceJour.InvoiceId AND VendInvoiceTrans.InvoiceDate = VendInvoiceJour.InvoiceDate AND VendInvoiceTrans.numberSequenceGroup = VendInvoiceJour.numberSequenceGroup AND VendInvoiceTrans.InternalInvoiceId = VendInvoiceJour.InternalInvoiceId AND ((PurchId=N'Test1')) AND ((InvoiceDate>={ts '2016-04-19 00:00:00.000'} AND InvoiceDate<={ts '2016-04-20 00:00:00.000'})) JOIN * FROM PurchLine(PurchLine_1) ON VendInvoiceJour.PurchId = PurchLine.PurchId AND VendInvoiceJour.CostLedgerVoucher = PurchLine.LineNumber
    TYPE:
    Query

    You have to change the order of datasources, cause addlink method is linking purchline field with vendinvoicejour field with same id of VendInvoiceTrans.PurchaseLineLineNumber

    Regards.

  • ALAhmed Profile Picture
    916 on at

    Yes this the result of debug ,so i don't know how can order my query to fixe the addlink of datasource.addLink(fieldNum(vendInvoiceTrans, PurchaseLineLineNumber), fieldNum(purchLine,LineNumber));

    For datasource PurchLine , i have two addlink of different datasource

  • André Arnaud de Calavon Profile Picture
    300,911 Super User 2025 Season 2 on at

    Hi Alex,

    Can you indicate what exactly you mean with "don't works"? Do you get a compilation error or is the result set different?

  • Community Member Profile Picture
    on at

    You can add relation between two fields without using addlink method, using querybuildrange object.

    Here you can find an example:

    theaxapta.blogspot.com.es/.../conditional-joins-in-x.html

  • ALAhmed Profile Picture
    916 on at

    Yes ,if i discomment the red line i get this error

    Cannot select a record in Posted vendor invoice lines (VendInvoiceTrans).

    The SQL database has issued an error.

    SQL error description: [Microsoft][SQL Server Native Client 11.0][SQL Server]Error converting data type nvarchar to bigint.

    i want get the some result like i get from the SQL query that i posted too. i put my job on the question

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