How to get the tables from AOT query in ax 2012 - axapta

I have drop down in one page, I am selecting AOT query in first page then i will click on next button, then it has to show tables related to that query

If you have a query name, you can loop through all of its datasources like this:
str queryName = "ActivityListOpen";
int i, dbcount;
QueryBuildDataSource qbds;
Query query = new Query(queryName);
dbcount = query.dataSourceCount();
for (i = 1; i <= dbcount; i++)
{
qbds = query.dataSourceNo(i);
info(qbds.name());
}
You can also use table() method on QueryBuildDataSource to retrieve table Id.

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.

Convert Linq to SQL

I have researched on the net and most result are converting from sql to linq and seldom have linq to sql.
this is the code which I want to convert to SQL :
using (CommerceEntities db = new CommerceEntities())
{
try
{
var query = (from ProductOrders in db.OrderDetails
join SelectedProducts in db.Products on ProductOrders.ProductID
equals SelectedProducts.ProductID
group ProductOrders by new
{
ProductId = SelectedProducts.ProductID,
ModelName = SelectedProducts.ModelName
} into grp
select new
{
ModelName = grp.Key.ModelName,
ProductId = grp.Key.ProductId,
Quantity = grp.Sum(o => o.Quantity)
} into orderdgrp where orderdgrp.Quantity > 0
orderby orderdgrp.Quantity descending select orderdgrp).Take(5);
RepeaterItemsList.DataSource = query;
RepeaterItemsList.DataBind();
}
catch (Exception exp)
{
throw new Exception("ERROR: Unable to Load Popular Items - " +
exp.Message.ToString(), exp);
}
}
You can attempt to run the LINQ statement in LinqPad. For examples on how to use LinqPad, check the answer here.
It has a tab to show the generated SQL statement.
Here's an article on logging in LINQ to SQL. It lets you specify a TextWriter to which to send the query.
Basically, you can write something like this:
db.Log = new System.IO.StreamWriter("linq-to-sql.log") { AutoFlush = true };
... where db is your data context.
In SQL you'd write something like this (although the produced code will look a lot different, since it is auto-generated):
SELECT TOP 5 Products.ModelName, Products.ProductID, SUM(OrderDetails.Quantity) qty
FROM OrderDetails
INNER JOIN Products ON OrderDetails.ProductID = Products.ProductID
GROUP BY Products.ProductID, Products.ModelName
HAVING qty > 0
ORDER BY qty DESC

Trying to join firstonly using AX query object

My request is that a client wants to put in a date range (typically a month) and pull all general ledger journals that have at least one line posted in that date range. They will post journals in March for January's period for example, and want to know which journals have that data.
The basic idea is LedgerJournalTable to a firstonly LedgerJournalTrans. I'm not the best with the query object. Why isn't my query working? It's returning multiple journals instead of just one. I was thinking I could group by and be OK, but I'd think this would work.
static void Job38(Args _args)
{
Query q;
QueryRun queryRun;
QueryBuildDatasource qbd;
QueryBuildDatasource qbd2;
QueryBuildRange qbr;
LedgerJournalTable ledgerJournalTable;
;
info(strfmt("%1", date2strxpp(str2date('10/01/2011', 213))));
q = new Query();
qbd = q.addDataSource(tablenum(LedgerJournalTable));
qbd2 = qbd.addDataSource(tableNum(LedgerJournalTrans));
qbd2.relations(true);
qbd2.firstOnly(true);
qbd2.joinMode(JoinMode::InnerJoin);
qbr = qbd2.addRange(fieldNum(LedgerJournalTrans, TransDate));
qbr.value(strfmt('(TransDate > %1) && (TransDate < %2)', Date2StrXpp(str2date('10/01/2011', 213)), Date2StrXpp(str2date('10/31/2011', 213))));
queryRun = new QueryRun(q);
while (queryRun.next())
{
ledgerJournalTable = queryRun.get(tableNum(LedgerJournalTable));
info(strfmt("%1", ledgerJournalTable.JournalNum));
}
}
Have you tried JoinMode::ExistsJoin?

LinqtoSql using .first() with asp.net gridview

I am using entity framework with the following linq query
IQueryable<Order_Details> query = (from ord in ctx1.Order_Details
where ord.OrderID == 1
select ord).ToArray();
gv1.DataSource = query;
gv1.DataBind();
I get a result ok, return a row with orderId of 1
When I use the following,
var query = (from ord in ctx1.Order_Details
where ord.OrderID == 1
select ord).First() as IQueryable<Order_Details>;
gv1.DataSource = query;
gv1.DataBind();
I don't recive any results in the gridview
First() returns the Order_Detail record itself, not a collection/array, and therefore is not IQueryable; so using as IQueryable<Order_Details> will return null because it can't be cast. Casting to IQueryable<> can only work for an enumerable of some sort.
If you have to use First, bind to the gridview like this:
var query = (from ord in ctx1.Order_Details
where ord.OrderID == 1
select ord).First();
gv1.DataSource = new[] { query };
gv1.DataBind();
You're trying to cast a single Order_Details object to IQueryable<Order_Details>
Since that's not a valid cast, null is being returned, and your grid is not showing any results.
Your GridView is expecting an IEnumerable, so your original code was correct. Why did you try to change it?

Flex: sqlite last_insert_rowid over multiple insert calls

I have file with multiple SQL statements in it to be executed.
INSERT INTO reports (a,b,c) VALUES (1,2,3);
INSERT INTO units (report_id, e, f, g) VALUES ( (SELECT last_insert_rowid() FROM reports), 4, 5, 6);
INSERT INTO elements (report_id, h, i, j) VALUES ( (SELECT last_insert_rowid() FROM reports), 7, 8, 9);
The FROM reports section of the sub-selection does nothing.
What ends up happening is:
A row is inserted into reports and the reports.id field is autoincremented
A row is inserted into units with report_id being equal to the reports id
A row is inserted into elements with report_id being equal to units.id of the last row inserted
This is works as described in the sqlite documentation.
My issue is that I want all the queries subsequent to the report insert to use report.id.
Is there anyway I can get this to work on the database end without having to resort to a solution in as3?
There is a way to do this, but it is in AS3 using parameters.
What is done is instead of using the SELECT last_insert_row() function in each call, replace it with a parameter.
INSERT INTO elements (report_id, h, i, j) VALUES (#id, 7, 8, 9);
Now in my code I have to split the file into an array so that each individual queries is process separately (this is how AS3 implements sqlite's API).
var sqlArray:Array = sql.split(";\n\n");
Now what I do is execute the first statement for importing the report itself.
statement.text = sqlArray[0];
statement.execute();
Now the fun part. You need to get the id back out. So we run another query.
statement.text = "SELECT last_insert_rowid() as ID";
statement.execute();
var id:int = statement.getResult().data[0].id;
Now we can loop through the rest of the queries using the id as a parameter.
for(var i:int = 1; i < sqlArray.length - 1; i++) {
/**
* start at 1 because we already inserted the report
* end at length -1 because our last entry is empty because of how split works on our data
**/
statement.text = sqlArray[i];
statement.parameters['#ID'] = id;
statement.execute();
}
This is a little more complicated, but not much and it ends up working.
Everything rolled together into a single function (omitting a lot of class overhead) would be:
function importFromSQLString(sql:String):void {
try{
connection.begin();
var sqlArray:Array = sql.split(";\n\n");
statement.text = sqlArray[0];
statement.execute();
statement.text = "SELECT last_insert_rowid() as ID";
statement.execute();
var id:int = statement.getResult().data[0].id;
for(var i:int = 1; i < sqlArray.length - 1; i++) {
statement.text = sqlArray[i];
statement.parameters['#ID'] = id;
statement.execute();
}
connection.commit();
statement.clearParameters();
} catch (e:Error) {
connection.rollback(); //cleanup if there was a failure
}
}

Resources