tag:infinitekind.tenderapp.com,2009-01-14:/discussions/moneydance-development/6144-transaction-listener-helpInfinite Kind: Discussion 2021-06-28T20:44:38Ztag:infinitekind.tenderapp.com,2009-01-14:Comment/492492792021-06-16T16:22:45Z2021-06-16T16:22:45ZTransaction Listener Help<div><p>I'm trying to impelement a transaction listener into an extension. Here is what I have:</p>
<pre>
<code>AccountBook book = extension.getUnprotectedContext().getCurrentAccountBook();
if(book !=null) {
myListener otl = new myListener();
book.getTransactionSet().addTransactionListener(otl);
}</code>
</pre>
<p>public class myListener implements TransactionListener {</p>
<p>public myListener() {<br>
System.err.println("**********<strong><em>\nTXN Listener Added\n</em></strong>**********\n" ); }</p>
<p>@Override public void transactionAdded(AbstractTxn txn) {<br>
System.err.println("**********<strong><em>\nTXN Added\n" ); System.err.println(txn.getAccount().toString() +"\n</em></strong>**********\n" ); }</p>
<p>@Override public void transactionModified(AbstractTxn txn) {<br>
System.err.println("**********<strong><em>\nTXN Modified\n" ); System.err.println(txn.getAccount().toString() +"\n</em></strong>**********\n" ); }</p>
<p>@Override public void transactionRemoved(AbstractTxn txn) {<br>
System.err.println("**********<strong><em>\nTXN Removed\n" ); System.err.println(txn.getDescription() +" " ); System.err.println(txn.getAccount().toString() +"\n</em></strong>**********\n" ); } }</p>
<p>In the console, I get the "listener added", but nothing when a transaction is added.</p>
<p>What am I missing?</p>
<p>Thanks</p></div>Dan Ptag:infinitekind.tenderapp.com,2009-01-14:Comment/492492792021-06-16T16:41:33Z2021-06-16T16:41:33ZTransaction Listener Help<div><p>Hi Dan,</p>
<p>Is anything being received in the transactionModified listener? That may be how changes (including additions) are notified now due to how the syncing system works. The TransactionsListener interface came long ago, before the syncing system was even an idea.</p>
<p>Thanks<br>
Sean</p>
<p>--<br>
Sean Reilly<br>
Developer, The Infinite Kind<br>
<a href="https://infinitekind.com">https://infinitekind.com</a></p></div>Sean Reillytag:infinitekind.tenderapp.com,2009-01-14:Comment/492492792021-06-16T16:54:43Z2021-06-16T16:54:43ZTransaction Listener Help<div><p>Yes, it appears Adds show as Modified.</p>
<p>Thanks, Sean.</p></div>Dan Ptag:infinitekind.tenderapp.com,2009-01-14:Comment/492492792021-06-17T21:29:09Z2021-06-18T10:49:34ZTransaction Listener Help<div><p>Cross post from email</p></div>Dan Ptag:infinitekind.tenderapp.com,2009-01-14:Comment/492492792021-06-18T04:07:21Z2021-06-18T04:07:21ZTransaction Listener Help<div><p>Perhaps you could describe what you are trying to achieve as there may be another way?</p>
<p>For example, would using the AccountListener and accountBalanceChanged() be of any use? Ie most txn modify the account balance.</p>
<p>(Not support, just a fellow user)</p></div>Stuart Beesley (Mr Toolbox)tag:infinitekind.tenderapp.com,2009-01-14:Comment/492492792021-06-18T11:00:05Z2021-06-18T11:00:05ZTransaction Listener Help<div><p>Thanks Stuart. I get a sweep transaction downloaded from Vanguard and it throws my balance off. I have tried everything to get it resolved and the only way I've found is to change the amount to zero. I have to leave the transaction or it will just download again.<br>
I wanted to see if I could have my extension receive the notifications for Vanguard transactions and then change the amount.<br>
I have the listener working in a test extension, but now I'm having trouble understanding the transaction data.<br>
Please note that this is for my education more than any practical use. More of an exercise in "I wonder if..."</p></div>Dan Ptag:infinitekind.tenderapp.com,2009-01-14:Comment/492492792021-06-18T15:12:20Z2021-06-18T15:12:20ZTransaction Listener Help<div><p>Happy to help if I can on your txn format question.</p>
<p></p></div>Stuart Beesley (Mr Toolbox)tag:infinitekind.tenderapp.com,2009-01-14:Comment/492492792021-06-18T15:49:39Z2021-06-18T15:49:39ZTransaction Listener Help<div><p>Ok, when the listener gives me the abstract transaction, I can extract the amount with txn.getValue(), but I can't find a method to change it.</p>
<p>In the following code, I tried to change the parameter for amt and locked up MD.</p>
<pre>
<code>public void transactionModified(AbstractTxn txn) {
String acct = txn.getAccount().toString();
if (acct.equals("Vanguard")) {
AbstractTxn stxn = txn.getOtherTxn(0);
String cat = stxn.getAccount().toString();
AccountListWindow.addMsg("*************\nTXN Modified in " + txn.getAccount().toString());
AccountListWindow.addMsg(txn.toString() );
boolean dl = txn.wasDownloaded();
long amt = txn.getValue();
AccountListWindow.addMsg("Split " + cat + "\n");
AccountListWindow.addMsg(acct + ", " + txn.getTransferType() + ", ");
AccountListWindow.addMsg(txn.getDescription() + "Downloaded? " + dl);
AccountListWindow.addMsg("$" + amt/100 );
if (cat.equals("Billfold")) {
AccountListWindow.addMsg("Got Billfold Split");
if (amt == 25000) {
AccountListWindow.addMsg("Have an amount of $250.00");</code>
</pre>
<p>// txn.setParameter("amt", 0); // txn.syncItem(); } } } AccountListWindow.addMsg("*************\n"); }</p>
<p>OUTPUT:<br>
TXN Modified in Vanguard<br>
[ParentTxn(cf3ab479-ca72-47c6-a2dd-92af4ccbf872) 20210615 desc=; val=25000; stat= ; #splits=1; chk=; acct=Vanguard; splits=SplitTxn: val=-25000; amt=25000; desc=; stat= ; cat=Billfold; tags=[(samt:-25000)(acctid:bb2e515d-5f43-4438-9e82-d576aed5f971)(invest.splittype:xfr)(obj_type:)(id:e06945a6-1f55-4e27-a446-69ee39c0c8b9)(pamt:25000)(desc:)]; ], ; ] Split Billfold</p>
<p>Vanguard, xfrtp_bank,<br>
Downloaded? true<br>
$250 Got Billfold Split<br>
Have an amount of $250.00</p>
<hr></div>Dan Ptag:infinitekind.tenderapp.com,2009-01-14:Comment/492492792021-06-18T16:17:55Z2021-06-18T16:17:55ZTransaction Listener Help<div><p>I’m away on holiday for a week and I have no access to a machine where I can check out the code required, sorry.</p>
<p>But in rough terms:<br>
- you cannot just update the split, you have to also get and save the parent. - I think the api is probably .setAmount() and use the fields to also set the parent amount.</p>
<p><a href="https://infinitekind.com/dev/apidoc/com/infinitekind/moneydance/model/SplitTxn.html">https://infinitekind.com/dev/apidoc/com/infinitekind/moneydance/mod...</a></p>
<p>So (from memory) something like:</p>
<p>parent = txn<br>
If txn != txn.getParent():<br>
parent = txn.getParent parent.setEditingMode()<br>
yourSplit.setAmount( etc, etc)<br>
parent.syncItem()</p>
<p>You may have to play with the above to get it right, but hopefully a pointer.</p></div>Stuart Beesley (Mr Toolbox)tag:infinitekind.tenderapp.com,2009-01-14:Comment/492492792021-06-18T16:24:28Z2021-06-18T16:24:28ZTransaction Listener Help<div><p>Thanks for the suggestions. I'll go in that direction.</p>
<p>Enjoy your holiday!</p></div>Dan Ptag:infinitekind.tenderapp.com,2009-01-14:Comment/492492792021-06-25T10:03:43Z2021-06-25T10:03:43ZTransaction Listener Help<div><p>Hi Dan,</p>
<p>Back now....</p>
<p>I just tried using the TransactionListener listener and.... nothing.... but then I couldn't get AccountListener to work either.... So I tried AccountBookListener and.... finally, an error in the log.....</p>
<p>I realised that I forgot to pass self into the listeners as the first parameter... But oddly, the first two showed no error (just silent).. I added self back in and hey presto, all the listeners are working... Including TransactionListener.. I received txn modified and deleted events (no added as this becomes modified)... So, I suspect the first two listeners are deep and provide no error in log.. Perhaps yours are erroring..?</p>
<p>My code attached.. Hope this helps. Let me know?</p></div>Stuart Beesley (Mr Toolbox)tag:infinitekind.tenderapp.com,2009-01-14:Comment/492492792021-06-26T17:17:05Z2021-06-26T17:20:59ZTransaction Listener Help<div><p>Thanks Stuart! I have it working pretty much the way I want. Your python is pretty straight forward on the listener setup and I have it working in java. My problem was/is understanding the parent and split transactions. The following works, but is probably not a clean way to do it.<br>
(txn is from the listener)</p>
<p>ParentTxn ptxn = txn.getParentTxn();</p>
<p>SplitTxn stxn = (SplitTxn) txn.getOtherTxn(0); // in my case only one slpit</p>
<p>ptxn.setEditingMode();</p>
<p>stxn.setEditingMode();</p>
<p>ptxn.setDescription("Sweep Transaction");</p>
<p>ptxn.setMemo("(Automatically set to 0)");</p>
<p>stxn.setAmount(0);</p>
<p>stxn.setParentAmount(0, 0);</p>
<p>ptxn.getParentTxn().syncItem();</p>
<p>At this point, I am satisfied with the way it works. I can enter a transaction manually with the required criteria and it changes immediately to 0.</p>
<p>Thanks again for all of your help.</p></div>Dan Ptag:infinitekind.tenderapp.com,2009-01-14:Comment/492492792021-06-26T18:01:37Z2021-06-26T18:01:37ZTransaction Listener Help<div><p>If it’s working then that’s the main thing.</p>
<p>A couple of comments:</p>
<ul>
<li>
<p>as you’ve eluded, txn.getOtherTxn(0) won’t work properly if your txn has more than a single split/category; but fine if you know this is always the case.</p>
</li>
<li>
<p>I think this is redundant: stxn.setEditingMode(); (might be useful if Sean were to comment on this as I believe .syncItem() and .setEditingMode() only need to be called on the parent - but I could be wrong)….</p>
</li>
<li>
<p>You can change ptxn.getParentTxn().syncItem(); to ptxn.syncItem(); (but your code will still work anyway)</p>
</li>
<li>
<p>you can probably set the amounts in one api call, but that doesn’t matter.</p>
</li>
</ul></div>Stuart Beesley (Mr Toolbox)tag:infinitekind.tenderapp.com,2009-01-14:Comment/492492792021-06-26T20:19:51Z2021-06-26T20:19:51ZTransaction Listener Help<div><p>Just FYI, you are correct on these:<br>
stxn.setEditingMode(); is not needed.<br>
stxn.setAmount(0); is not needed.<br>
ptxn.syncItem(); works fine.</p>
<p>You and Sean have been an immense help in educating me in so many ways. Thanks!</p>
<p>.</p></div>Dan Ptag:infinitekind.tenderapp.com,2009-01-14:Comment/492492792021-06-27T22:44:19Z2021-06-27T22:44:19ZTransaction Listener Help<div><p>Just confirming what Stuart said: you shouldn't call setEditingMode() or syncItem() on the split transaction. As far as syncing and saving go, a split is only part of the parent and not really a syncable item on its own. For a SplitTxn calling syncItem() should be a no-op. setEditingMode() should be the same, but it's be safer not to call them and perform any syncing actions only on the corresponding parents.</p>
<p>Thanks,<br>
Sean</p></div>Sean Reilly