tag:infinitekind.tenderapp.com,2009-01-14:/discussions/moneydance-development/44-programmatically-creating-new-transactions-via-pythonInfinite Kind: Discussion 2015-03-31T16:12:16Ztag:infinitekind.tenderapp.com,2009-01-14:Comment/47547122011-01-15T17:57:19Z2011-01-15T17:57:20ZProgrammatically creating new transactions via Python<div><p>Several people seem to have asked how to programmatically create
a new transaction, but none of the posts have been answered. Can
someone please explain how this works? I basically want to create
new transactions in a Credit Card account via the Python
interface.</p></div>Johntag:infinitekind.tenderapp.com,2009-01-14:Comment/47547122011-01-16T14:33:55Z2011-01-16T14:33:56ZProgrammatically creating new transactions via Python<div><p>Hi John,</p>
<p>I agree with you, the developer documentation could be much
better. I have written a CSV importer for my Bankstatement using
the python interface. It took some time but in the end i figured
out. Even with matching of existing transactions. I am not really
happy with it but, it works somehow. :-) See a quick extract
below.</p>
<p>I hope this helps.</p>
<p>regards,</p>
<p>Alain</p>
<pre>
<code>account = findAccountByAccountNum(ra,accountnum)
ra = moneydance.getRootAccount()
transactionId = ra.getTransactionSet().getTransactionCount() + 1
txn = ParentTxn(parrentT['date'], parrentT['date'], parrentT['date'], "", act, parrentT['desc'], "memo", transactionId, ParentTxn.STATUS_UNRECONCILED )
txn.setFiTxnId(1,parrentT['FiTxnId']) #use Protocoll ID 1 = OFX
txn.addSplit(splitTxn)
ra.getTransactionSet().addNewTxn(txn)
ra.refreshAccountBalances()
def findAccountByAccountNum(acc,accountNum):
subAccounts = acc.getSubAccounts()
for account in subAccounts:
bankId = account.getParameter('ofx_bank_id')
if accountNum == bankId.translate(all, nodigs):
return account
else:
if account.getSubAccountCount() > 0:
return findAccountByAccountNum(account,accountNum)</code>
</pre></div>alani