Transaction Listener Help

Dan P's Avatar

Dan P

16 Jun, 2021 04:22 PM

I'm trying to impelement a transaction listener into an extension. Here is what I have:

AccountBook book = extension.getUnprotectedContext().getCurrentAccountBook();
if(book !=null) {
    myListener otl = new myListener();
    book.getTransactionSet().addTransactionListener(otl);
}

public class myListener implements TransactionListener {

public myListener() {
System.err.println("**********\nTXN Listener Added\n**********\n" ); }

@Override public void transactionAdded(AbstractTxn txn) {
System.err.println("**********\nTXN Added\n" ); System.err.println(txn.getAccount().toString() +"\n**********\n" ); }

@Override public void transactionModified(AbstractTxn txn) {
System.err.println("**********\nTXN Modified\n" ); System.err.println(txn.getAccount().toString() +"\n**********\n" ); }

@Override public void transactionRemoved(AbstractTxn txn) {
System.err.println("**********\nTXN Removed\n" ); System.err.println(txn.getDescription() +" " ); System.err.println(txn.getAccount().toString() +"\n**********\n" ); } }

In the console, I get the "listener added", but nothing when a transaction is added.

What am I missing?

Thanks

  1. Support Staff 1 Posted by Sean Reilly on 16 Jun, 2021 04:41 PM

    Sean Reilly's Avatar

    Hi Dan,

    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.

    Thanks
    Sean

    --
    Sean Reilly
    Developer, The Infinite Kind
    https://infinitekind.com

  2. 2 Posted by Dan P on 16 Jun, 2021 04:54 PM

    Dan P's Avatar

    Yes, it appears Adds show as Modified.

    Thanks, Sean.

  3. Dan P closed this discussion on 16 Jun, 2021 05:46 PM.

  4. Dan P re-opened this discussion on 17 Jun, 2021 09:29 PM

  5. 3 Posted by Dan P on 17 Jun, 2021 09:29 PM

    Dan P's Avatar

    Cross post from email

  6. 4 Posted by Stuart Beesley ... on 18 Jun, 2021 04:07 AM

    Stuart Beesley - JUST A FELLOW USER and Toolbox ‘guy’'s Avatar

    Perhaps you could describe what you are trying to achieve as there may be another way?

    For example, would using the AccountListener and accountBalanceChanged() be of any use? Ie most txn modify the account balance.

    (Not support, just a fellow user)

  7. 5 Posted by Dan P on 18 Jun, 2021 11:00 AM

    Dan P's Avatar

    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.
    I wanted to see if I could have my extension receive the notifications for Vanguard transactions and then change the amount.
    I have the listener working in a test extension, but now I'm having trouble understanding the transaction data.
    Please note that this is for my education more than any practical use. More of an exercise in "I wonder if..."

  8. 6 Posted by Stuart Beesley ... on 18 Jun, 2021 03:12 PM

    Stuart Beesley - JUST A FELLOW USER and Toolbox ‘guy’'s Avatar

    Happy to help if I can on your txn format question.

    

  9. 7 Posted by Dan P on 18 Jun, 2021 03:49 PM

    Dan P's Avatar

    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.

    In the following code, I tried to change the parameter for amt and locked up MD.

    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");
    

    // txn.setParameter("amt", 0); // txn.syncItem(); } } } AccountListWindow.addMsg("*************\n"); }

    OUTPUT:
    TXN Modified in Vanguard
    [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

    Vanguard, xfrtp_bank,
    Downloaded? true
    $250 Got Billfold Split
    Have an amount of $250.00


  10. 8 Posted by Stuart Beesley ... on 18 Jun, 2021 04:17 PM

    Stuart Beesley - JUST A FELLOW USER and Toolbox ‘guy’'s Avatar

    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.

    But in rough terms:
    - 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.

    https://infinitekind.com/dev/apidoc/com/infinitekind/moneydance/mod...

    So (from memory) something like:

    parent = txn
    If txn != txn.getParent():
    parent = txn.getParent parent.setEditingMode()
    yourSplit.setAmount( etc, etc)
    parent.syncItem()

    You may have to play with the above to get it right, but hopefully a pointer.

  11. 9 Posted by Dan P on 18 Jun, 2021 04:24 PM

    Dan P's Avatar

    Thanks for the suggestions. I'll go in that direction.

    Enjoy your holiday!

  12. Dan P closed this discussion on 24 Jun, 2021 11:40 AM.

  13. Stuart Beesley - JUST A FELLOW USER and Toolbox ‘guy’ re-opened this discussion on 25 Jun, 2021 10:03 AM

  14. 10 Posted by Stuart Beesley ... on 25 Jun, 2021 10:03 AM

    Stuart Beesley - JUST A FELLOW USER and Toolbox ‘guy’'s Avatar

    Hi Dan,

    Back now....

    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.....

    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..?

    My code attached.. Hope this helps. Let me know?

  15. 11 Posted by Dan P on 26 Jun, 2021 05:17 PM

    Dan P's Avatar

    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.
    (txn is from the listener)

    ParentTxn ptxn = txn.getParentTxn();

    SplitTxn stxn = (SplitTxn) txn.getOtherTxn(0); // in my case only one slpit

    ptxn.setEditingMode();

    stxn.setEditingMode();

    ptxn.setDescription("Sweep Transaction");

    ptxn.setMemo("(Automatically set to 0)");

    stxn.setAmount(0);

    stxn.setParentAmount(0, 0);

    ptxn.getParentTxn().syncItem();

    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.

    Thanks again for all of your help.

  16. 12 Posted by Stuart Beesley ... on 26 Jun, 2021 06:01 PM

    Stuart Beesley - JUST A FELLOW USER and Toolbox ‘guy’'s Avatar

    If it’s working then that’s the main thing.

    A couple of comments:

    • 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.

    • 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)….

    • You can change ptxn.getParentTxn().syncItem(); to ptxn.syncItem(); (but your code will still work anyway)

    • you can probably set the amounts in one api call, but that doesn’t matter.

  17. 13 Posted by Dan P on 26 Jun, 2021 08:19 PM

    Dan P's Avatar

    Just FYI, you are correct on these:
    stxn.setEditingMode(); is not needed.
    stxn.setAmount(0); is not needed.
    ptxn.syncItem(); works fine.

    You and Sean have been an immense help in educating me in so many ways. Thanks!

    .

  18. Support Staff 14 Posted by Sean Reilly on 27 Jun, 2021 10:44 PM

    Sean Reilly's Avatar

    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.

    Thanks,
    Sean

  19. Dan P closed this discussion on 28 Jun, 2021 08:44 PM.

Comments are currently closed for this discussion. You can start a new one.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac