Split transactions
Hi guys, Having trouble creating/manipulating split transactions in Java. In Jython things seem to be easier.
Using Moneydance 2024.4 5253
Sample code:
```java public ParentTxn createTransaction(AccountBook book,
Account parentAccount, int date, List splits) {
ParentTxn newTxn = new ParentTxn(book);
newTxn.setDateInt(date);
newTxn.setTaxDateInt(date);
newTxn.setAccount(parentAccount);
newTxn.setDescription("Test Transaction");
newTxn.setStatus(AbstractTxn.STATUS_UNRECONCILED);
for (SplitData s : splits) {
SplitTxn txnSplit = new SplitTxn(newTxn);
// METHOD SEQUENCE UNDER REVIEW:
txnSplit.setAmount(s.amount);
txnSplit.setParentAmount(1.0, -s.amount); // Requirement for balance sync
txnSplit.setAccount(s.targetAccount); // Assign account last
newTxn.addSplit(txnSplit);
}
book.getTransactionSet().addNewTxn(newTxn);
newTxn.syncItem();
return newTxn;
}
Test 1: Simple Multi-Leg Split - PASS
Scenario: A paycheck deposit with taxes and insurance. * Target (Parent):Checking* Split 1:Salary(Income) | Amount:-3500.00* Split 2:Taxes(Expense) | Amount:+800.00* Split 3:Insurance(Expense) | Amount:+200.00* Expected Result: Parent register shows +$2,500.00.
Test 2: Split Receipt - PASS
Scenario: Single withdrawal split into two expense categories. * Target (Parent):Cash* Split 1:Groceries| Amount:100.00* Split 2:Hardware| Amount:25.50* Expected Result: Parent register shows -$125.50.
Test 3: Bank Transfer - FAIL
Scenario: Moving money between two bank accounts using a split. * Target (Parent):Checking* Split 1:Savings(Bank Account) | Amount:100.00* Expected Result: Parent shows -$100.00, Savings shows +$100.00. * Observation: Results in $0.00 parent value
Test 4: Updating Existing Splits - FAIL
Scenario: Replacing splits on an existing transaction. 1. Usetxn.removeSplit(0)to clear an existing split. 2. Add new splits using the logic in the factory above. * Expected Result: Parent register reflects the sum of new splits. * Observation: FAIL. The parent register value resets to $0.00. Even callingsyncItem()orTransactionSet.recalcBalances()does not restore the derived parent total.
Test 5: Zero-Sum Adjustment - PASS
Scenario: Internal reallocation between categories. * Target (Parent):Cash* Split 1:Misc Expense| Amount:+20.00* Split 2:Other Income| Amount:-20.00* Expected Result: Parent register shows $0.00. * Observation: Pass.
Any help appreciated. 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 15 Jan, 2026 06:05 AM
Firstly, don't call:
book.getTransactionSet().addNewTxn(newTxn);just call:
newTxn.syncItem();as otherwise you are (behind the scenes) calling
syncItem()twiceSecondly, Jython and Java usage of the API is identical in the sense of actual code and how you use it, so this cannot be your issue. Unless the way you store items in Jython is somehow different to how you are storing in Java (e.g. using an object by reference and accidentally reusing data from elsewhere).
To the main point. I think your code is fighting itself.
setParentAmount()simply calls:setAmount(-Math.round(newRate * newParentAmount), newRate, newParentAmount)- so I expect yoursetParentAmount()call is overwriting your previoussetAmount()call.Do this:
call
txnSplit.setAccount()first.next (only) call this (assuming single currency and rate of 1.0:
txnSplit.setAmount(newSplitAmount: Long, newParentAmount: Long)(or if you want longhand:setAmount(newSplitAmount, 1.0, -newParentAmount))lastly call
syncItem()PS - I can't see how you are calling
txnSplit.setAmount(s.amount);was that a typo - I cannot see a signature for only one parameter.Let me know how it goes?
2 Posted by Jim on 15 Jan, 2026 06:19 PM
Hi, still struggling here.
So I've had this jython code which has worked well for a while although I don't think it's as you recommend as it does add and sync.
When I have this code in Java, it always creates a transaction with a $0 value.
All of these variations below give me a $0 value actually
3 Posted by Stuart Beesley ... on 16 Jan, 2026 08:35 AM
Jim. Have you tried my suggestion? Can you try please and let me know the results.
For each split.
Set account first
Only call txnSplit.setAmount(newSplitAmount: Long, newParentAmount: Long)
Then Sync item.
Does this work or not?
If not. Please provide.
The raw data for one of these that’s failing
Add debug messages into the split loop to dump the values being used for the splits
Perhaps print each split and txn to console.
4 Posted by Stuart Beesley ... on 17 Jan, 2026 09:57 AM
As well as my suggestion and sending the types and raw data to look at.
One difference I have found between Jython and Java, is that if there is an overloaded Java function which can take both integers, or long values, then it can sometimes get the wrong overload. For example when using DateRange(), I have to pass the value wrapped in a Java.lang.Integer(value). This is the only thing I can think of that could be otherwise messing up with the call that would be different from Jython. My gut says try my solution (and sending me the raw data requested)
5 Posted by Jim on 21 Jan, 2026 04:17 PM
Hi, sorry for the radio silence. Back at it Still having issues.
I tried this function:
returns:
Thoughts?
6 Posted by Stuart Beesley ... on 21 Jan, 2026 06:14 PM
That was fun. You make have hit a bug..
s.setDescription("");// or anythings.setAmount(val, val);// note the sign - as the parent value gets flippedLet me know?
7 Posted by Jim on 21 Jan, 2026 06:52 PM
I'm not clear what you mean on the firstpoint. The code I pasted has this:
Did you mean something else?
8 Posted by Stuart Beesley ... on 21 Jan, 2026 09:47 PM
Exactly as I said
s.setDescription(“”)Ie on the split. It’s a bug. With no description the split self destructs.
9 Posted by Jim on 22 Jan, 2026 04:48 AM
ah. on the spilt!.
Well that fixed it! Ha! Never would have guessed.
Thanks for your help!
10 Posted by Stuart Beesley ... on 22 Jan, 2026 08:11 AM
👍
It certainly wasn’t obvious.
11 Posted by Stuart Beesley ... on 22 Jan, 2026 08:43 AM
Also note that in your Jython code you were calling:
txnSplit.setDescription(()So as mentioned, Jython and Java should operate the same (ish)
12 Posted by Jim on 23 Jan, 2026 01:37 AM
Yup. Thanks.
Another related question. Do you have any advice on when, if ever, we should be using isDirty and .txnModified?
Thanks
13 Posted by Stuart Beesley ... on 23 Jan, 2026 06:57 AM
Never. I certainly never have.
As far as I know they are for internal usage.
Why?
14 Posted by Jim on 23 Jan, 2026 03:52 PM
Just curious. I see them in the API, so wanted to confirm. I've never used them, but was wondering if I was missing something.
15 Posted by Stuart Beesley ... on 23 Jan, 2026 04:50 PM
Yes, a rabbit hole - don't go there if you want a life!
16 Posted by Stuart Beesley ... on 23 Jan, 2026 04:51 PM
PS - I have been informed that the "bug" I reported above has been fixed for the next release
(not support, just a fellow user)