Simple Jython example to create a new account

boschen's Avatar

boschen

03 Mar, 2016 08:11 PM

I have successfully created some Jython scripts to import data from csv files and add to existing accounts in my MD2015 MoneyDance file. I now have a need to create new sub-accounts that may not exist (specifically a sub-account of a parent liability account) and am looking for a simple example from that part of the code to help me see how to implement this.

Any pointers on this is greatly appreciated. In general, having some more example code in the API documentation will be helpful to newer developers like me!

  1. 1 Posted by boschen on 03 Mar, 2016 09:57 PM

    boschen's Avatar

    I think I might have figured it out-does this look right as best practice? It does add the new accounts.

     The following is for a top level liability account under root:

    from com.infinitekind.moneydance.model import ParentTxn, SplitTxn, AbstractTxn, Account

    root= moneydance.getRootAccount()
    book= moneydance.getCurrentAccountBook()

    new_account = Account.makeAccount(book, root.AccountType.LIABILITY, root)
    new_account.setAccountName('Test Name Here')

    and to put a sub-account under this one:

    new_sub = Account.makeAccount(book, root.AccountType.LIABILITY, new_account)
    new_sub.setAccountName('Sub Name Here')

  2. 2 Posted by Ian L on 07 Mar, 2016 01:39 PM

    Ian L's Avatar

    Hi boschen,

    What you're doing should work fine. The only thing you should add is a call to .syncItem() on each account after it's been fully created. So, something like this:

    root= moneydance.getRootAccount() 
    book= moneydance.getCurrentAccountBook()
    
    new_account = Account.makeAccount(book, root.AccountType.LIABILITY, root) 
    new_account.setAccountName('Test Name Here')
    new_account.syncItem()
    
    new_sub = Account.makeAccount(book, root.AccountType.LIABILITY, new_account) 
    new_sub.setAccountName('Sub Name Here')
    new_sub.syncItem()
    

    That will ensure the accounts automatically show up in the account hierarchy immediately, instead of not showing until a restart.

    Ian
    Infinite Kind Support

  3. 3 Posted by boschen on 07 Mar, 2016 06:07 PM

    boschen's Avatar

    Perfect, thanks Ian.

  4. boschen closed this discussion on 08 Mar, 2016 04:38 AM.

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