Ax 2012 tts error - runtime-error

HI i am facing an error while updating a record in the invent table. I am using the following sample code.
static void Job4(Args _args)
{
CsInvBOMSplitQtyCalcHelper bomCalc;
Qty qty;
InventTable inventTable;
InventTable updateInventTable;
BOM bom;
boolean result;
BOMId bomId;
BOMVersion bomVersion;
ItemId item = "1000M-3C-Pcs";
select firstOnly * from bomversion
where bomversion.Active == true
&& bomversion.ItemId == item
&& csIsLengthItem(item) == false;
if (0 != bomVersion.RecId)
{
select * from bom
where bom.BOMId == bomversion.BOMId
exists join inventTable
where bom.ItemId == inventTable.ItemId
&& inventTable.CsIsLengthItem == true;
}
if (0 != bom.RecId)
{
result = true;
bomCalc = CsInvBOMSplitQtyCalcHelper::construct(item);
qty = bomCalc.getAdvicedBOMSpoolQty();
}
ttsBegin;
while select forUpdate updateInventTable
where updateInventTable.ItemId == item
{
updateInventTable.CsInvBOMSplitQty = qty;
updateInventTable.update();
}
ttsCommit;
info(strFmt('%1, %2, %3', result, qty, inventTable.CsInvBOMSplitQty));
}
This is the error I get:
Please help me in resolving this issue.

The error is obviously not caused by this job (but maybe an earlier version).
Just run this small job to reset the TTS level:
static ttsAbort(Args args)
{
ttsabort;
}
TTS level errors are usually caused by programming errors, say calling return before ttsCommit.

Related

How to enable Security Logging AX2012 (SecurityTasks to SecurityRoles)

I want to log SecurityRole - SecurityTask changes, and since the EeUserRoleChangeLog logs User – SecurityRole changes (for Estonia, but commenting out language detection, it logs also for us here in Holland perfectly). I think that basis can be used to log those changes.
So I first created the table:
EeRoleTaskChangeLog with the following fields:
AddRemove EnumType: AddRemove
ChangedBy EDT: UserID
SecurityRole EDT: RefRecID
SecurityTask EDT: RefRecId
I then noticed that within the class SysSecRole, User-Role changes logging is triggered within method removeFromSelectedUser. So whenever someone changes something from the AOT or the front-end, all these changes are then logged within the EeUserRoleChangeLog.
So I extended SysSecTask with the method removeFromSelectedRole:
public void removeFromSelectedRole()
{
RefRecId SecurityRole;
SysSecTreeTasks taskTree;
SecurityRoleTaskGrant SecurityRoleTaskGrant;
taskTree = tree as SysSecTreeTasks;
securityRole = taskTree.getSecurityRole();
ttsbegin;
EePersonalDataAccessLogging::logRoleTaskChange(recId, 0, securityRole, AddRemove::Remove);
delete_from securityRoleTaskGrant where securityRoleTaskGrant.SecurityRole == securityRole && securityRoleTaskGrant.SecurityTask == recId;
ttscommit;
if (treeControl)
{
tree.deleteTreeItem(treeControl, idx);
}
}
However, it somehow doesn’t recognize the triggers and so it does not fill my table.
I hoped to add a delete/insert trigger to the SecuryRoleTaskGrant systemtable, but this is completely closed, so I have no idea how or where this method can be triggered.
So then I thought as a ‘semi-working’ work around, let’s only log changes made in the front-end, since that already fixes the problem for the regular workers. So I decided to find a similar trigger within Forms, and I found the form SysSecUserAddRoles with method assignRolesToSelectedUser. This method has the following trigger:
if (added)
{
EePersonalDataAccessLogging::logUserRoleChange(secRole.RecId,0, userId, AddRemove::Add);
}
I decided to add this trigger to a Form that allows workers to change Task-Roles: SysSecRoleAddTasks method addTasksFromRole, and somehow this also does not seem to fix the problem. It doesn’t fill the table. I use the following code, and front-end changes are still not logged within my created EeRoleTaskChangeLog table.
public int addTasksFromRole(SecurityRole _role, boolean _recursive = false)
{
SecurityRole childRole;
SecuritySubRole subRole;
SecurityTask task;
SecurityRoleTaskGrant srtGrant;
int nAdded = 0;
RefRecId securityRole;
boolean added;
if (_role.RecId == targetRoleId)
{
return 0;
}
startLengthyOperation();
while select task
join SecurityRole, SecurityTask from srtGrant
where srtGrant.SecurityRole == _role.RecId
&& srtGrant.SecurityTask == task.RecId
{
nAdded += this.addTask(task);
}
if (_recursive)
{
while select childRole
where childRole.RecId != targetRoleId
join SecurityRole, SecuritySubRole from subRole
where subRole.SecuritySubRole == childRole.RecId
&& subRole.SecurityRole == _role.RecId
{
this.addTasksFromRole(childRole, true);
}
}
endLengthyOperation();
if (added) //Added
{
EePersonalDataAccessLogging::logRoleTaskChange(securityTask.RecId,0, securityRole, AddRemove::Add);
}
return nAdded;
}
Why doesn’t this trigger the logging?
Do I need to provide any more information?
The SysSecRoleAddTasks Form has also the addTask method, I tried it here as well, but this again doesn’t trigger the logtable to be filled.
public int addTask(SecurityTask _task, boolean _trialAdd = false)
{
SecurityRoleTaskGrant srtGrant;
int nAdded = 0;
boolean added; //Added
RefRecId securityRole; //Added
select targetRole
where targetRole.RecId == targetRoleId; // make sure in sync
select forupdate srtGrant
where srtGrant.SecurityTask == _task.RecId
&& srtGrant.SecurityRole == targetRoleId;
if (!srtGrant)
{
if (!_trialAdd)
{
ttsbegin;
srtGrant.SecurityTask = _task.RecId;
srtGrant.SecurityRole = targetRoleId;
srtGrant.insert();
nAdded++;
myObjects.insert(_task.RecId, true);
if (added) //Added
{
EePersonalDataAccessLogging::logRoleTaskChange(securityTask.RecId,0, securityRole, AddRemove::Add);
}
ttscommit;
}
else if (targetRoleId != 0)
{
nAdded++;
}
//info('Added task "' + _task.Name + '" (' + int2str(appl.ttsLevel()) + ')');
}
/*
else
{
info('Not adding task "' + _task.Name + '"');
} */
return nAdded;
}
Any suggestions?
This question requires a lot of hands-on debugging I would think to solve, which I'm not sure many posters are going to do unfortunately. Have you tried just using the system's database log?
SecurityRole and SecurityTask are system tables, stored under \System Documentation\Tables\SecurityRole, so I didn't know where to find them in the DB log wizard, but I wrote a manual job that I think should work.
I've only tested it with UserInfo, which is another system table and it appears to have logged changes correctly.
static void AddTablesToDBLog(Args _args)
{
void logTable(TableId _tableId, DatabaseLogType _logType)
{
DatabaseLog dblog;
dblog.logTable = _tableId;
dblog.logField = 0;
dblog.logType = _logType;
dblog.insert();
}
ttsBegin;
logTable(tableNum(SecurityRole), DatabaseLogType::Insert);
logTable(tableNum(SecurityRole), DatabaseLogType::Update);
logTable(tableNum(SecurityRole), DatabaseLogType::Delete);
logTable(tableNum(SecurityTask), DatabaseLogType::Insert);
logTable(tableNum(SecurityTask), DatabaseLogType::Update);
logTable(tableNum(SecurityTask), DatabaseLogType::Delete);
ttsCommit;
SysFlushDatabaseLogSetup::main();
new MenuFunction(menuitemDisplayStr(SysDatabaseLogSetup), MenuItemType::Display).run(null);
info("Done");
}

VendTransOpen marking with CustVendOpenTransManager

I have a button on VendOpenTrans and implemented its clicked method.
I thought this would work but i get an exception and AX closes..
void clicked()
{
LedgerJournalTrans ledgerJournalTrans;
VendTransOpen vto;
super();
switch (originator.TableId)
{
case tableNum(LedgerJournalTrans):
ledgerJournalTrans = element.args().record();
}
for ( vto = vendTransOpen_ds.getFirst(0); vto; vto = vendTransOpen_ds.getNext() )
{
//vendTransOpen_ds.markRecord(vto, 1);
if (vto.RecId)
{
if (manager.getTransMarked(vto) == NoYes::No)
{
select Invoice from vendTrans
where vto.AccountNum == vendTrans.AccountNum &&
vto.RefRecId == vendTrans.RecId;
if (ledgerJournalTrans.Invoice == vendTrans.Invoice)
{
// Mark transaction for settlement
showError = NoYes::No;
manager.updateTransMarked(vto, NoYes::Yes);
showError = NoYes::Yes;
}
}
}
// Update dynamic controls & refresh form as auto-redraw is not triggered
element.updateDesignDynamic();
element.redraw();
}
vendTransOpen_ds.refreshEx(-2);
}
If i comment out the following lines it will work, basically marking all the lines in the grid.
//select Invoice from vendTrans
//where vto.AccountNum == vendTrans.AccountNum &&
//vto.RefRecId == vendTrans.RecId;
//if (ledgerJournalTrans.Invoice == vendTrans.Invoice)
//{
// Mark transaction for settlement
showError = NoYes::No;
manager.updateTransMarked(vto, NoYes::Yes);
showError = NoYes::Yes;
//}
So, to be more clear, what stays is: manager.updateTransMarked(vto, NoYes::Yes);
and this way, it works. As far as i see something happens when i add that select.
Using debug i was able to check it and i think the exception is thrown by the for loop ..
Is there any chance to get a hint about this ?
Try changing your for loop definition to this:
for (vto = vendTransOpen_ds.getFirst(0) ? vendTransOpen_ds.getFirst(0) : vendTransOpen_ds.cursor(); vto; vto = vendTransOpen_ds.getNext())
And change this:
select Invoice from vendTrans
where vto.AccountNum == vendTrans.AccountNum &&
vto.RefRecId == vendTrans.RecId;
if (ledgerJournalTrans.Invoice == vendTrans.Invoice)
{
To this:
if (ledgerJournalTrans.Invoice == vto.vendTrans().Invoice)

Bring Vendor Name as a column in LedgerTransAccount form

I am trying to bring the vendor name on each line in the TransAccountForm.
I have written a small piece of code which gets the vendor name when LedgerDimension from LedgerJournalTrans is available:
static void GetVendorName(Args _args)
{
CustAccount custAccount;
VendTable vendTable;
LedgerJournalTrans ledgerJournalTrans;
while select ledgerJournalTrans
{
if (ledgerJournalTrans.AccountType == LedgerJournalACType::Vend)
{
custAccount = DimensionStorage::ledgerDimension2AccountNum(LedgerJournalTrans.LedgerDimension);
select firstOnly vendTable
where vendTable.AccountNum == custAccount;
info(strFmt("Vendor Name: %1, Voucher: %2", DirPartyTable::getName(vendTable.Party), ledgerJournalTrans.Voucher));
}
}
}
But how can I get to run this code in order to bring that new column with the vendor name?
Thanks to Jan B. Kjeldsen I got to this solution:
One of the data source in LedgerTransAccount is GeneralJournalEntry on wich i add the display method.
With the help of SubLedgerVoucher i am able to get one line from LedgerJournalTrans.
public display Name VendorName()
{
Name ret;
LedgerJournalTrans ledgerJournalTrans;
select firstFast firstOnly ledgerJournalTrans
where ledgerJournalTrans.Voucher == this.SubledgerVoucher &&
ledgerJournalTrans.TransDate == this.AccountingDate;
ret = ledgerJournalTrans.AccountType == LedgerJournalACType::Vend ?
vendTable::find(DimensionStorage::ledgerDimension2AccountNum
(ledgerJournalTrans.LedgerDimension)).name() : '';
return ret;
}
After this, I just drag and drop this method on my form's grid.
You can add a display method to the ledgerJournalTrans table:
[SysClientCacheDataMethodAttribute(true)]
display VendName vendName()
{
return this.AccountType == LedgerJournalACType::Vend ? vendTable::find(DimensionStorage::ledgerDimension2AccountNum(this.LedgerDimension)).name() : '';
}
A one-liner.

How to check table is empty with count(*)

I am trying to check if a table is empty. I code this method:
public boolean checkIfNULL()
throws SQLException, ClassNotFoundException {
boolean flag=false;
System.out.println("Checking if table is empty...");
String sq = "select count(*) from TABLE1";
try {
Class.forName(typeDB);
c = DriverManager.getConnection(path);
stm = c.prepareStatement(sq);
PreparedStatement stm = c.prepareStatement(sq);
int rowsAffected = stm.executeUpdate();
if(rowsAffected == 0)
flag=true;
} catch (SQLException e) {
System.out.println(e.getMessage());
} finally {
if (stm != null) {
stm.close();
}
if (c != null) {
c.close();
}
}
return flag;
}
but sth wrong is hapenning and I get an error message
Query returns results
Exceptionn: java.sql.SQLException: [SQLITE_ERROR] SQL error or missing database (Connection is closed)
How I check the returning value of check?
UPDATE 1:
Instead of the query above, I tried also SELECT EXISTS(SELECT 1 FROM TABLE1)
but the same is happening..
UPDATE 2:
I used this:
ResultSet rs = stm.executeQuery();
if(!rs.next())
flag=true;
else
System.err.println("ERROR - The table has records...");
and it prints the ERROR - "The table has records...". How is this possible? I see the table through SQLite manager and it is empty!
You are executing a SELECT, so you need to use executeQuery, not executeUpdate. executeUpdate is for statements like UPDATE, DELETE and INSERT, executeQuery is for executing statements that return a result set (like SELECT).
You need to execute a select statement, and do:
try (ResultSet rs = stm.executeQuery()) {
rs.next(); // You always have a row, with the count
int count = rs.getInt(1);
flag = count == 0;
}
The code in your update won't work, because if you do a SELECT count(*) FROM table, then you always have one row (with the count).

how to read each value of a column in a table

Using x++ how to create a job to read each value of a column in a table in Microsoft dynamics AX 2009?
This code will display all the column values for a record.
static void Job1(Args _args)
{
DictTable dictTable = new DictTable(tableNum(CustTable));
DictField dictField;
int counter, fieldId;
CustTable custTable;
anytype value;
select firstonly custTable;
for (counter = 1; counter <= dictTable.fieldCnt(); counter++)
{
fieldId = dictTable.fieldCnt2Id(counter);
dictField = new DictField(tableNum(CustTable), fieldId);
if (!dictField.isSystem())
{
value = custTable.(fieldId);
if(value)
{
info(strFmt('%1 = %2',
dictField.label(),
any2str(value)));
}
}
}
}
For getting all the values for a specific column, please use Carlos Heuberger's code.
One possibility: in the AOT (Ctrl-D) right-click on Jobs and select New Job. In the new window enter something like (using the correct table and column name you want to read):
static void Job()
{
YourTable yourTable;
;
while select TheColumn from yourTable
{
// process yourTable.TheColumn
info(strFmt("value: %1", yourTable.TheColumn));
}
}
but thera are some other ways: Select Statements
Just a slight mod, took out any2str to make my table work:
strFmt('%1 = %2 \r\n', dictField.label(), value);

Resources