How to call "Chart of Accounts Importer" through RPC API?

I have successfully replaced the default chart of accounts with my customer’s accountant’s chart of accounts, adding elements for ERPNext required fields (eg; Expenses Included In Valuation)

Nevertheless, the way things are going, I foresee that I will be modifying frequently the spreadsheet from which I generate the CSV file.

I have many scripts for inserting data already and I’d like to add chart of accounts replacement to the library.

Is there an API call for the “Chart of Accounts Importer”?

Where can I find an up-to-date comprehensive list of all permitted RPC calls?

1 Like

Big thanks!! to @revant_one for his whitelist spider tool : ReST API: List of whitelisted endpoints

In the related gist I found these endpoints …

'erpnext.accounts.doctype.chart_of_accounts_importer.chart_of_accounts_importer.validate_company(company)',
'erpnext.accounts.doctype.chart_of_accounts_importer.chart_of_accounts_importer.import_coa(file_name, company)',
'erpnext.accounts.doctype.chart_of_accounts_importer.chart_of_accounts_importer.get_coa(doctype, parent, is_root=False, file_name=None)',
'erpnext.accounts.doctype.chart_of_accounts_importer.chart_of_accounts_importer.download_template()',
'erpnext.accounts.doctype.chart_of_accounts_importer.chart_of_accounts_importer.validate_accounts(file_name)',

… but I don’t know how to use them.

So, with a big proviso, this actually works :

curl --location --request POST 'https://mysite.me/api/method/erpnext.accounts.doctype.chart_of_accounts_importer.chart_of_accounts_importer.import_coa' \
--header 'Authorization: token d05a0fake2563f8:464cshit0942fca' \
--header 'Content-Type: application/json' \
--data-raw '{
    "file_name": "/private/files/AccountChartOfAccountsTemplate.csv",
    "company": "My Company."
}'

The proviso is that the file must already have been “attached” to the “chart_of_accounts_importer” doctype.

Can anyone tell me how to do that with curl?

curl -X POST \
  http://<base-url>/api/method/upload_file \
  -H 'Accept: application/json' \
  -H 'Authorization: token xxxx:yyyy' \
  -F file=@/path/to/file/file.png

https://frappe.io/docs/user/en/api/rest#file-uploads

1 Like

Precisely what I was looking for!

Thanks so much!

Cool fact for the chronically curious …

The API response string will be something like :

{
  "message": {
    "name": "c9c714494a",
    "file_name": "file.csv",
    "is_private": 0,
    "file_size": 10673,
    "file_url": "/public/files/file.csv",
    "content_hash": "c7b8f220e2ee4a6ec38c5057ca9afa69",
    "doctype": "File"
  }
}

Go ahead and say it!

“So what?”

Well, the curious thing is that if you copy file.csv to file2.csv and run it again with the new file …

curl -X POST \
  http://<base-url>/api/method/upload_file \
  -H 'Accept: application/json' \
  -H 'Authorization: token xxxx:yyyy' \
  -F file=@/path/to/file/file2.csv

… the response will be …

{
  "message": {
    "name": "c9c714494a",
    "file_name": "file2.csv",
    "is_private": 0,
    "file_size": 10673,
    "file_url": "/public/files/file.csv",
    "content_hash": "c7b8f220e2ee4a6ec38c5057ca9afa69",
    "doctype": "File"
  }
}

Rather than creating a new file with same content hash it just uses the newer name internally.

This means that if you try to call the Chart of Accounts Importer function import_coa()

http://mysite.me/api/method/erpnext.accounts.doctype.chart_of_accounts_importer.chart_of_accounts_importer.import_coa

… with the new file name

--data-raw '{
    "file_name": "/public/files/file2.csv",
    "company": "My Company"
}'

… you’ll get a - file not found - error!

So here below is the solution.

Step 1

Add your file to your site’s “File List”:

curl --location -s \
  -o /dev/shm/upLoadChartOfAccounts.json \
  --request POST "https://mysite.me/api/method/upload_file" \
  --header 'Authorization: token xxxx:yyyy' \
  --header "Accept: application/json" \
  --form "file=@/path/to/file/AccountChartOfAccountsTemplate.csv" \
  --form "is_private=1";

Notes:

  • –location : follow redirects (of Nginx?)
  • -s : suppress progress bar
  • -o : dump response packet to temporary file
  • strings are in double quotes to allow variable expansion in linux shell scripts
  • –form “is_private=1” : specify storing under ./sites/mysite/private/files

Step 2

Tell the importer to use that file for “My Company”:

curl --location -s \
  -o /dev/shm/installChartOfAccounts.json \
  --request POST "https://mysite.me/api/method/erpnext.accounts.doctype.chart_of_accounts_importer.chart_of_accounts_importer.import_coa" \
  --header 'Authorization: token xxxx:yyyy' \
  --header "Accept: application/json" \
  --header "Content-Type: application/json" \
  --data-raw "{
      \"file_name\": \"/private/files/AccountChartOfAccountsTemplate.csv\",
      \"company\": \"My Company\"
  }";

Notes:

  • strings are in double quotes to allow variable expansion in linux shell scripts, so internal double quotes need to be escaped "abc \"def\" ghi"

More notes:

  • You cannot have two account names with the same name (one of them will be quietly forgotten and the other may appear under the wrong parent)
  • You cannot have an account parent that does not exactly match a group account name. (the error message is no help at all if you make that mistake)
2 Likes