update cash balance after adding split transaction
The ofx file download from my financial institute uses "Buy" as the action. I would like to change it to "BuyXfr". I used the following code to do it:
amount = txn.getOtherTxn(0).getAmount()
incSplit = SplitTxn(txn)
incSplit.setAmount(-amount)
incSplit.setParameter(AbstractTxn.TAG_INVST_SPLIT_TYPE, AbstractTxn.TAG_INVST_SPLIT_INC)
txn.setInvestTxnType(InvestTxnType.BUY_XFER)
txn.addSplit(incSplit)
The "action" and "transfer" fields have been updated properly. However, the "Cash Balance" remains as the original value. I had to highlight the transaction, and press "enter" key, then "Cash Balance" changed to "0". What did I do wrong to prevent "Cash Balance" from updating automatically?
Thanks
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
1 Posted by Stuart Beesley ... on 30 May, 2022 04:53 AM
I haven’t debugged your code, but a BuyXfr would need an account/category to transfer the cash in/out. I don’t see you setting this in the above. So I can properly understand what you are trying to do, can you highlight a transaction, right click, show record details. Can you do this before and after a successful change and post those details?
2 Posted by Stuart Beesley ... on 30 May, 2022 05:13 AM
Also, I'm a bit confused as why you are setting the "invest.splittype" parameter to "inc" when you said this is/was a Buy transaction? For example, here is a Buy and a BuyXfr
Buy that reduces cash balance
BuyXfr that does not reduce cash balance, but transfers the cash in from elsewhere
A BuyXfr will always have an additional split...So, please can you post the txn details (screenshots of before and after and the show record details before and after)?
Tip, the com.infinitekind.moneydance.model.InvestFields class might be your friend here:
This might also help:
3 Posted by H Yi on 30 May, 2022 05:42 AM
Stuart,
Thanks a lot for your feedback. The BuyX should be the correct type as transactions are stock shares directly from the employer.
Here is the original "Buy" transaction:
After running the scriptAfter pressing "enter"
Besides ts changes, which are irrelevant for the discussion. It seems that script did just its job somewhat correctly, except newly added split transaction didn't get recognized until "enter" is pressed from keyboard. What could cause that?
4 Posted by Stuart Beesley ... on 30 May, 2022 06:06 AM
Sorry, but the script is not working... You are not adding the new split line...
5 Posted by Stuart Beesley ... on 30 May, 2022 06:10 AM
All your script is actually doing is changing this:
invest.txntype: buyx
When you then go to press enter, MD knows the data is wrong and is doing all the work...
Your script needs to add a splitTxn for the actual xfr (and not change the buy split). Best best is to use the InvestFields class
6 Posted by Stuart Beesley ... on 30 May, 2022 06:25 AM
.
7 Posted by Stuart Beesley ... on 30 May, 2022 06:33 AM
.
8 Posted by Stuart Beesley ... on 30 May, 2022 06:46 AM
Last update:.....Do something like this (not tested) on the ParentTxn:
I presume you already have a reference to THE_ACCOUNT_OBJECT_FOR_TXFR_ACCT_HERE (uuid: 58100c4b-d4d5-4db8-adaf-ed1a256cdfe2)? If not, and if the xfr account never changes, then for now you can do once this at the beginning before you iterate the txns:
and use:
9 Posted by H Yi on 30 May, 2022 11:48 AM
Stuart,
Thanks a lot for the suggestion. Your code works like a charm!
10 Posted by Stuart Beesley ... on 30 May, 2022 12:15 PM
Kool! 👍 😃
The InvestFields is a very useful and easy class to use and handles the complexity of the underlying record format….
11 Posted by H Yi on 30 May, 2022 02:21 PM
Thanks for the tip! You're a lifesaver!