proper way to delete a transaction?

Jim's Avatar

Jim

29 Mar, 2021 10:15 PM

Hi, I did find deleteItem() in https://infinitekind.tenderapp.com/discussions/moneydance-development/2642-jython-delete-txn
and I get that. my question is if I want to delete a transaction, should I delete all the splits and also delete the parent? Or if I just find the parent and delete it that's fine. Just want to make sure I don't end up with some odd orphaned splits or anything like that.

Thanks!

  1. Support Staff 1 Posted by Sean Reilly on 29 Mar, 2021 10:30 PM

    Sean Reilly's Avatar

    Hi Jim,

    Great questions! To completely delete a transaction please get the parent and call deleteItem() on that. All transactions have a .getParentTxn() method so you can reliably call .getParentTxn().deleteItem().

    However in some cases you might not want to delete every part of a transaction, such as when you have a SplitTxn that is part of a multi-split transaction (such as most transactions in investment accounts). In cases like those you'd want to check if you have a split that is part of a multi-split transaction. So maybe something like the following:

    ptxn = txn.getParentTxn()
    if ptxn == txn: # txn is the parent, so delete it
      ptxn.deleteItem()
    else if ptxn.getOtherTxnCount()>1: # txn is the only split, so delete the parent
      ptxn.deleteItem()
    else:  # txn is one of multiple splits
      txn.deleteItem() # this will remove the split from the parent
      ptxn.syncItem() # but we still need to save the modified parent
    

    Thanks,
    Sean

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

  2. 2 Posted by J C on 30 Mar, 2021 02:05 AM

    J C's Avatar

    Thanks Sean!

    Your response of course triggers a thought I want to confirm. I'm
    getting  the transaction from a set like this:

    txns =
    moneydance_data.getTransactionSet().getTransactionsForAccount(BankAccountName)

    My understanding is that because I'm using the name of a bank account
    and not a 'category', then the txn set would only include parent
    transactions. Is that a correct assumption? I'm currently skipping this
    step: ptxn = txn.getParentTxn() , but wondering if I'm at any risk of
    buggering things up.

    (In my case I'm not looking to preserve any splits so it seems like just
    deleting the parent will also delete the splits.)

    I was also wondering ..if a ParentTxn has multiple splits and you
    ParentTxn.removeSplit() on one of them (and don't delete it), what
    happens to that split? Will it delete itself or will it get orphaned and
    hang around somehow/where?

    Thanks,

    Jim

  3. 3 Posted by Stuart Beesley ... on 30 Mar, 2021 04:20 AM

    Stuart Beesley (Mr Toolbox)'s Avatar

    You will (may) get both parents and splits in your txn set even when you select an account. This is because txfrs between accounts can be initiated from either account / ‘side’. Thus if you get a split it may be the other half of an account txfr or even the Xfr part of a investment BuyXfr or sell Xfr.

  4. 4 Posted by Stuart Beesley ... on 30 Mar, 2021 05:22 AM

    Stuart Beesley (Mr Toolbox)'s Avatar

    Just checked... Yup if you use ParentTxn.removeSplit() then you should call ParentTxn.syncItem() afterwards; that's it. But take care if you are deleting the last split - as Sean mentions above. Also take care; ideally use ParentTxn.setEditingMode() if you are going to make multiple changes to a parent before you call .syncItem().

  5. 5 Posted by Jim on 30 Mar, 2021 03:07 PM

    Jim's Avatar

    Thanks for the clarification Stuart!

    Jim

  6. Support Staff 6 Posted by Sean Reilly on 30 Mar, 2021 03:44 PM

    Sean Reilly's Avatar

    Hi Jim,

    Stuart's answer to the first question is correct.

    For the second question, a deleted split should only stick around if you keep a reference to it... so as long as you (and other extensions) don't hold onto it, it will be garbage collected by java and never seen again.

    Cheers,
    Sean

  7. System closed this discussion on 29 Jun, 2021 03:50 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