Outer joins with where condition x++ - axapta

I am trying to write a query that retrieves an item based on ItemId or item barcode.
This is using x++, Dynamics AX 2012 R2
select firstOnly * from inventTable
where (inventTable.ItemId == _upc || inventItemBarcode.itemBarCode == _upc)
outer join inventItemBarcode
where inventItemBarcode.itemId == inventTable.ItemId;
When this is translated into sql it comes out as...
FROM INVENTTABLE T1 LEFT
OUTER
JOIN INVENTITEMBARCODE T2 ON (((T2.PARTITION=?)
AND (T2.DATAAREAID=?))
AND (T1.ITEMID=T2.ITEMID))
WHERE (((T1.PARTITION=?)
AND (T1.DATAAREAID=?))
AND (T1.ITEMID=?))
You can see that is is totally omitting the OR condition in the last line.
I tried writing the query like this
select firstOnly * from inventTable
outer join inventItemBarcode
where
//join
inventItemBarcode.itemId == inventTable.ItemId
//ilc
&& (inventTable.ItemId == _upc
|| inventItemBarcode.itemBarCode == _upc);
But it puts the OR condition in the outer join ON and then returns me the first row in the InventTable.
Does anyone know how to make this work in X++?

If using AX 2012 you will have to use Query and QueryRun instead, then add your or-expression as a query expression using addQueryFilter.
static void Job117(Args _args)
{
str search = "5705050765989";
QueryRun qr = new QueryRun(new Query());
QueryBuildDataSource ds1 = qr.query().addDataSource(tableNum(InventTable));
QueryBuildDataSource ds2 = ds1.addDataSource(tableNum(InventItemBarcode));
str qstr1 = '((%1.%2 == "%5") || (%3.%4 == "%5"))';
str qstr2 = strFmt(qstr1, ds1.name(), fieldStr(InventTable,ItemId),
ds2.name(), fieldStr(InventItemBarcode,ItemBarCode),
queryValue(search));
qr.query().addQueryFilter(ds1, fieldStr(InventTable,ItemId)).value(qstr2);
ds2.joinMode(JoinMode::OuterJoin);
ds2.relations(true);
info(qstr2);
info(ds1.toString());
while (qr.next())
info(strFmt("%1", qr.getNo(1).RecId));
}
In prior AX versions you can make a view, then query the view using a query expressions by using the addRange method.

Related

How do i get the Count of InventSerialId from InventDim

How do i create a query or using select to get the count of InventSerialId base on a given Itemid, InventLocationId and where the inventSum.PhysicalInvent > 0 or inventSum.Picked > 0.
This is not directly possible using X++.
Consider:
static void _TestDim(Args _args)
{
ItemId itemId = '123';
InventSum inventSum;
InventDim inventDim;
Query q = new Query();
QueryBuildDataSource ds = q.addDataSource(tableNum(InventSum), 's');
QueryRun qr;
;
// ds.addRange(fieldNum(InventSum,ItemId)).value(queryValue(itemId));
ds.addRange(fieldNum(InventSum,Closed)).value(queryValue(NoYes::No));
ds.addGroupByField(fieldNum(InventSum,ItemId));
ds.addSelectionField(fieldNum(InventSum,PhysicalInvent),SelectionField::Sum);
ds.addSelectionField(fieldNum(InventSum,Picked),SelectionField::Sum);
q.addHavingFilter(ds, fieldStr(InventSum,PhysicalInvent), AggregateFunction::Sum).value('>0');
// q.addHavingFilter(ds, fieldStr(InventSum,Picked), AggregateFunction::Sum).value('((s.Picked >0)||(s.PhysicalInvent>0))'); // This is not allowed
ds = ds.addDataSource(tableNum(InventDim), 'd');
ds.joinMode(JoinMode::InnerJoin);
ds.relations(true);
ds.addGroupByField(fieldNum(InventDim,InventSerialId));
ds.addRange(fieldNum(InventDim,InventSerialId)).value('>""');
info(q.dataSourceNo(1).toString());
qr = new QueryRun(q);
while (qr.next())
{
inventSum = qr.getNo(1);
inventDim = qr.getNo(2);
info(strFmt('%1 %2: %3 %4', inventSum.ItemId, inventDim.InventSerialId, inventSum.PhysicalInvent, inventSum.Picked));
break;
}
}
Here you aggreate PhysicalInvent and picked, and you can apply a having-filter using the query method addHavingFilter.
However, you cannot have that combined with another having-filter using a SQL or-statement.
If you try with a query expression, you will get a run-time error.
What you can do is create two views with each filter, then combine them using a union view. This is tricky but doable.
The first should select positive PhysicalInvent and the second should select PhysicalInvent == 0 and positive Picked.

How to bind select query as datasource in report?

I have a select statement that I want to bind as data source in a report.
I have not found a way to design an appropriate AOT query.
This is how it looks like in X++
public void insertData(date data = today())
{
BHNEmployeesOnDay ins;
EmplTable tbl;
CompanyInfo info;
BHNEmplAgreements Agreemnt;
BHNEmplAgreements Agreemnt2;
BHNEMPLHISTORYCOMPANY history;
BHNEMPLHISTORYCOMPANY history_test;
BHNDIVISIONTABLE division;
BHNPOSITIONTABLE position;
SysCompanyUserInfo sys1;
SysUserInfo sys2;
UserInfo usrInfo;
Date infinity = mkdate(1,1,1900);
;
delete_from ins;
while select * from tbl
join Info where info.dataAreaId == tbl.dataAreaId && info.BLX_companyForDW == 1
join sys1 where sys1.EmplId==tbl.EmplId && sys1.dataAreaId == tbl.dataAreaId
join sys2 where sys1.UserId==sys2.Id
join usrInfo where usrInfo.id==sys1.UserId
exists join history_test
where history_test.EmplId==tbl.EmplId && history_test.dataAreaId==tbl.dataAreaId
join Agreemnt where Agreemnt.HistoryId==history_test.HistoryId
&& (agreemnt.DateTo >= data || agreemnt.DateTo==infinity)
{
select firstonly *
from history order by history.DateFrom desc, Agreemnt2.DateFrom desc
where history.EmplId==tbl.EmplId && history.dataAreaId==tbl.dataAreaId
join Agreemnt2 where Agreemnt2.HistoryId==history.HistoryId
&& Agreemnt2.DateFrom<=data && (Agreemnt2.DateTo >= data || Agreemnt2.DateTo==infinity)
join division where division.DivisionId==agreemnt.DivisionId
join position where position.PositionId==agreemnt.PositionId;
ins.adddRecord(tbl.EmplId, tbl.Name_BHN, tbl.BirthDate, division.Name, position.FullName);
}
}
Currently I generate data into a table [during run() method of the report], then simply select from that table. So far only 1 person uses this report so it's not a problem, but if two people run the same report simultaneously, I'm gonna get dirty reads.
I know it's bad approach, but I'm out of ideas. I thought of making a View on T-SQL side and try to select from it - but I was told that it might not be detected or simply not transferred to other instances of our AX during export, so it has to be done on AX side.
How can I solve this?
Just in case this is query in T-SQL SQL query on pastebin
You could overwrite the report's fetch method and just use your X++ code as is to get the records and then use the report's send method to process them.
See here for an example.
The example uses a query object but you could easily swap that with your own X++ code - you just eventually have to call send for the records you want to be processed by the report.
Update:
For example you could just fetch any record of SalesTable and call send.
In this example a member variable salesTable is assumed so that you can access the current record in a display method in case you need it.
public boolean fetch()
{
boolean ret;
//ret = super();
;
select firstOnly salesTable;
this.send(salesTable);
return true;
}

Replacing EXISTS JOIN with JOIN

I am having trouble with the AX2012 class (default AX2012 class and code, no modifications have been made on it): CustVendTransDetails in the method calcCashDiscounts
The following query is giving me the error The select list for the INSERT statement contains fewer items than the insert list. The number of SELECT values must match the number of INSERT columns.:
if (TaxParameters::canApplyCashDiscOnInvoice_ES())
{
insert_recordset tmpValue
(CustVendTransRefRecId, AmountMST)
select CustVendTransRefRecId
from _custVendAccountStatementIntTmpProcessing
exists join custVendTransLoc
where
custVendTransLoc.RecId == _custVendAccountStatementIntTmpProcessing.CustVendTransRefRecId
exists join firstOnly subledgerVoucherGeneralJournalEntry
where
subledgerVoucherGeneralJournalEntry.Voucher == custVendTransLoc.Voucher &&
subledgerVoucherGeneralJournalEntry.AccountingDate == custVendTransLoc.TransDate
exists join generalJournalEntry
where
generalJournalEntry.RecId == subledgerVoucherGeneralJournalEntry.GeneralJournalEntry &&
generalJournalEntry.Ledger == Ledger::current()
join AccountingCurrencyAmount from generalJournalAccountEntry
where
generalJournalAccountEntry.GeneralJournalEntry == generalJournalEntry.RecId &&
(generalJournalAccountEntry.PostingType == LedgerPostingType::CustCashDisc ||
generalJournalAccountEntry.PostingType == LedgerPostingType::VendCashDisc);
update_recordSet _custVendAccountStatementIntTmpProcessing setting
UtilizedCashDisc = tmpValue.AmountMST,
PossibleCashDisc = tmpValue.AmountMST
join tmpValue
where
tmpValue.CustVendTransRefRecId == _custVendAccountStatementIntTmpProcessing.CustVendTransRefRecId;
}
I understand why, but I am not sure how to solve this problem. Will it be a problem to replace the exist join with a normal join?
Replacing the exist join with join, does solve my problem, but I am not sure what difference it will make to the data? Because it is only is selecting 1 field?
You can try to switch the order of joins:
insert_recordset tmpValue (CustVendTransRefRecId, AmountMST)
select CustVendTransRefRecId
from _custVendAccountStatementIntTmpProcessing
join AccountingCurrencyAmount from generalJournalAccountEntry // Moved up
where generalJournalAccountEntry.PostingType == LedgerPostingType::CustCashDisc ||
generalJournalAccountEntry.PostingType == LedgerPostingType::VendCashDisc
exists join custVendTransLoc
where
custVendTransLoc.RecId == _custVendAccountStatementIntTmpProcessing.CustVendTransRefRecId
exists join firstOnly subledgerVoucherGeneralJournalEntry
where
subledgerVoucherGeneralJournalEntry.Voucher == custVendTransLoc.Voucher &&
subledgerVoucherGeneralJournalEntry.AccountingDate == custVendTransLoc.TransDate
exists join generalJournalEntry
where
generalJournalEntry.RecId == subledgerVoucherGeneralJournalEntry.GeneralJournalEntry && &&
generalJournalEntry.RecId == generalJournalAccountEntry.GeneralJournalEntry && // Moved from join
generalJournalEntry.Ledger == Ledger::current();
Replacing the exist join with join will not fix your issue. Exist is a way to join to essentially inner join to a table without returning any fields.
The query should return CustVendTransRefRecId from _custVendAccountStatementIntTmpProcessing and AccountingCurrencyAmount from generalJournalAccountEntry which is exactly what the insert is expecting.
I expect the query isn't actually returning anything. Check the criteria it is using and check the data.

Unable to create a constant value of type 'X.Models.Game'. Only primitive types or enumeration types are supported in this context

I've got a complex query written in SQL which works.
SELECT Instruments.Id as [Id], Instruments.ShareCode as [Share Code], Instruments.Name AS [Short Name], Instruments.Description as [Share Name],
InstrumentGames.Instrument_Id,
InstrumentGames.Game_Id, Games.Name AS [Game Name],
Entries.Name AS [Entry Name], AspNetUsers.UserName, AspNetUsers.Id as [User_Id],
Sectors.Name AS Sector_Id, Sectors.ShortName AS Sector
FROM AspNetUsers INNER JOIN
Entries ON AspNetUsers.Id = Entries.User_Id INNER JOIN
Games ON Entries.Game_Id = Games.Id INNER JOIN
InstrumentGames ON Games.Id = InstrumentGames.Game_Id INNER JOIN
Instruments ON InstrumentGames.Instrument_Id = Instruments.Id INNER JOIN
Sectors ON Instruments.Sector_Id = Sectors.Id
WHERE Instruments.Listed = 'true' and InstrumentGames.Game_Id = 2 and Entries.User_Id = 'd28d6552-7d98-476c-82cb-063e7ef45cb6'
I'm using Entity code first models and trying to convert what I have in SQL to a linq query.
I've come up with:
public static Models.Instrument GetShare(string shareSearchCriteria,
Models.Game selectedGame,
string userId)
{
var _db = new JSEChallenge.Models.ApplicationDbContext();
var records = (from instru in _db.Instruments
from e in _db.Entries
where (instru.ShareCode.Contains(shareSearchCriteria) ||
instru.Name.Contains(shareSearchCriteria) ||
instru.Description.Contains(shareSearchCriteria))
where (instru.Listed == true &&
instru.Games.Contains(selectedGame) &&
e.User_Id == userId)
select instru).ToList();
return records.FirstOrDefault();
}
But I keep getting this error:
Unable to create a constant value of type 'X.Models.Game'. Only primitive types or enumeration types are supported in this context.
I think the issue is the m2m table InstrumentGames. In my SQL query I can join it easily but in my C# I cannot. The way I usually find m2m records is syntax like instru.Games.Contains(selectedGame)
Unfortunately I still cannot get this to work.
How do I implement this kind of query in Linq?
Not sure if it will work, but try instru.Games.Any(q => q.Id == selectedGame.Id) instead of instru.Games.Contains(selectedGame).
Hope this helps!

LINQ - EF join difficulty

I have two tables:
Phase :
long ID
string Name
and another Activity :
long ID
string Name
long PhaseID
I already know the name of the phases and I want to get the activity for those particular phases. Do i add PhaseName to the activity table or do I do it through join in LINQ?
Maybe something like this?
var query = from a in entities.Activities
join p in entities.Phases on a.PhaseId equals p.Id
where p.Name == "Preplanning"
... and here im not sure how to finish this query..
Thanks for your help!
The code that you've provided will use an Inner Join to find all Activities where the Phase with Name "Preplanning" exists.
To finish your query you need to add a select clause.
var query = from a in entities.Activities
join p in entities.Phases on a.PhaseId equals p.Id
where p.Name == "Preplanning"
select a.Name
will return IEnumerable<string> of all activity names.
Just select activity, as you want:
var query = from a in entities.Activities
join p in entities.Phases on a.PhaseId equals p.Id
where p.Name == "Preplanning"
select a;
Here is how query expression should look like:
A query expression must begin with a from clause and must end with a select or group clause. Between the first from clause and the last select or group clause, it can contain one or more of these optional clauses: where, orderby, join, let and even additional from clauses. You can also use the into keyword to enable the result of a join or group clause to serve as the source for additional query clauses in the same query expression.
Same as puzzling image:
With method syntax you don't need to end query with something special:
var query = entities.Phases
.Where(p => p.Name == "Preplanning")
.Join(entities.Activities, p => p.Id, a => a.PhaseId, (p,a) => a);
No need to do a join if you only need data from one of the tables. You can apply a filter instead:
var q = entities.Activities.Where(a =>
entities.Phases.Any(p => a.PhaseId == p.Id && p.Name == "Preplanning"));

Resources