hello guys,
this is a code on my form.
I have this code blog,I am getting qty field 0 but I wrote a job it shows a value but in here ıt shows zero.in table there is a value but in form it shows zero.I s it something blocking and making the qty field zero?
query q=new query(Inventtrans_ds.queryRun().query());
queryRun qr;
Inventtrans itr;
queryBuildDataSource qbd=q.dataSourceTable(Tablenum(InventTrans));
QueryBuildRange qbr=sysQuery::findOrCreateRange(qbd,Fieldnum(InventTrans,Qty));
QueryBuildRange qbr2=sysQuery::findOrCreateRange(qbd,Fieldnum(InventTrans,DatePhysical));
;
Qty=0;
Costvalue=0;
qtyreceipt=0;
costvaluereceipt = 0;
qtyIssue = 0;
costValueissue=0;
qbd.addSelectionField(Fieldnum(Inventtrans,Qty),SelectionField::Sum);
qbd.addSelectionField(Fieldnum(Inventtrans,CostamountPosted),SelectionField::Sum);
qbd.addSelectionField(Fieldnum(Inventtrans,CostAmountAdjustment),SelectionField::Sum);
qbr.value(">0");
qbr2.value(sysquery::valueNotEmptyString());
qr=new queryrun(q);
While(qr.next())
{
itr=qr.get(Tablenum(Inventtrans));
CostvalueReceipt+=itr.CostAmountAdjustment + itr.CostAmountPosted;
qtyReceipt+=itr.Qty;
}
qbr.value("<0");
qbr2.value(sysquery::valueNotEmptyString());
qr=new queryrun(q);
While(qr.next())
{
itr=qr.get(Tablenum(Inventtrans));
CostvalueIssue+=itr.CostAmountAdjustment + itr.CostAmountPosted;
qtyIssue+=itr.Qty;
}
CostValue =costvaluereceipt + costValueissue;
Qty = qtyreceipt+qtyissue;
}
Hi, may I remind you to provide descriptive titles and avoid using exclamation mark without reason. Your titles empty field!, table lookup! and table relation! show that’s an intentional pattern, not just a mistake.
Also, please use Insert > Insert code in the rich formatting view to insert source code with indentation.
Let me format your code for you, so we can read it.
Query q = new Query(InventTrans_ds.queryRun().query());
QueryRun qr;
InventTrans itr;
queryBuildDataSource qbd = q.dataSourceTable(tableNum(InventTrans));
QueryBuildRange qbr = SysQuery::findOrCreateRange(qbd, fieldNum(InventTrans, Qty));
QueryBuildRange qbr2 = SysQuery::findOrCreateRange(qbd, fieldnum(InventTrans, DatePhysical));
;
qty = 0;
costValue = 0;
qtyReceipt = 0;
costValueReceipt = 0;
qtyIssue = 0;
costValueIssue = 0;
qbd.addSelectionField(fieldNum(InventTrans, Qty), SelectionField::Sum);
qbd.addSelectionField(fieldNum(InventTrans, CostamountPosted), SelectionField::Sum);
qbd.addSelectionField(fieldNum(InventTrans, CostAmountAdjustment), SelectionField::Sum);
qbr.value(">0");
qbr2.value(SysQuery::valueNotEmptyString());
qr = new QueryRun(q);
while (qr.next())
{
itr = qr.get(tableNum(InventTrans));
costValueReceipt += itr.CostAmountAdjustment + itr.CostAmountPosted;
qtyReceipt += itr.Qty;
}
qbr.value("<0");
qbr2.value(SysQuery::valueNotEmptyString());
qr = new QueryRun(q);
while (qr.next())
{
itr = qr.get(tableNum(InventTrans));
costValueIssue += itr.CostAmountAdjustment + itr.CostAmountPosted;
qtyIssue += itr.Qty;
}
costValue = costValueReceipt + costValueIssue;
qty = qtyReceipt + qtyIssue;
Now tell us what you found when debugged your code before giving it up and coming here. Do your queries return any data at all?
By the way, it seems that you ignore best practices regarding letter cases (such as that local variable names should start with lower case and using the camel casing) and spaces. Although it has no impact on execution, it makes your code significantly less readable. Notice how I’ve refactored it.
You should also use descriptive names, e.g. dateRange instead of qbr2.
in query my table is not empty but fields are empty:
ueryBuildDataSource qbd = q.dataSourceTable(tableNum(InventTrans));
QueryBuildRange qbr = SysQuery::findOrCreateRange(qbd, fieldNum(InventTrans, Qty));
Inventttrans is coming with value but qty is empty
when tracing I checked the field and table buffers coming with data
What do you mean by “empty but with values”? It doesn’t make sense to me.
Also, neither dataSourceTable() nor findOrCreateRange() is fetching data; they merely define the query string. Decide which of the while loops you want to debug in the moment, put a breakpoint inside the loop, verify that the block is executed at all and then observe values of your three summed fields in the itr variable.
I say Table is not empty whan I trace I can see the recId of table,but Table’s fields are empty.
qeryBuildDataSource qbd = q.dataSourceTable(tableNum(InventTrans)); / /this line I can see the value
QueryBuildRange qbr = SysQuery::findOrCreateRange(qbd, fieldNum(InventTrans, Qty));//in here qty is showing zero
No, your understand of the code is wrong. Let me explain it once more with more details.
All what you do with classes like Query, QueryBuildDataSource and QueryBuildRange is used only for constructing the SQL statement that will later be sent to database, but it doesn’t communicate with database yet. That happens only when you actually execute the query with QueryRun.
You seems to believe that q.dataSourceTable(tableNum(InventTrans)) gets some data from database, but that’s not the case. It merely gives you a reference to a QueryBuildDataSource datasource defined in the Query object (q). It says only that the query contains this table (it will try to select data for it), but it doesn’t say anything about whether the query will return any data.
SysQuery::findOrCreateRange() either gives you you an existing QueryBuildRange object (if the query already contains it), or it creates a new one; it doesn’t fetch anything from database. By calling value(), you can add an extra WHERE condition to the SELECT statement sent to database by QueryRun.next().
Please read my previous reply again to understand how to check what data your query returns.
when I trace it I saw that the debug it not going in the while(qr.next )loop so thats why query is not executing.But I do not know why?
so my execute is not runnig the codes not executing queryrun.next ,it skips thats blog.Thats why it dows not works.But I dont know why it skips that part
It means that the database server doesn’t return any data for your conditions. I know neither your data nor your query (returned from InventTrans_ds.queryRun().query()), so I can’t tell you anything specific.
First of all, try removing your ranges and try it again; especially using valueNotEmptyString() for a date field looks suspicious. Next steps depend on whether the problem is caused by your ranges (which sounds likely) or not. If it is, identify which of your ranges is to blame. When you isolate the problem, fixing it shouldn’t be too difficult.
martin you are the best:) thank you so much It worked.like you said because of valueNotEmptyString(),I removed it
You’re welcome. 
Remember the distinction between defining the query and executing it; it’s very important.
Also, a very useful debugging technique is checking if you’ve defined the query string you wanted. You can get the query string by calling toString() of a data source, such as qbd.toString() (it’s also visible in debugger). Sometimes you’ll immediately see INVALID VALUE instead of the expected range value, sometimes you notice that a join uses different fields than you thought and so on.
thanks to you I understand the logic of queries:)