Try ERPNext Buy Support Partners Foundation

How to get a "Naming Series" through the REST API?

If I place an API call to get the “Naming Series” DocType…

curl GET 'http://myhost/api/resource/DocType?filters=[["name", "like", "Na%"]]&fields=["name", "module"]' \
      -H 'Authorization: token 71d8.....a11af2:4eb.....319f'

… I get back …

{
    "data": [
        {
            "name": "Naming Series",
            "module": "Setup"
        }
    ]
}

However, if I try to list all Naming Series like this …

curl GET 'http://myhost/api/resource/Naming Series' \
      -H 'Authorization: token 71d8.....a11af2:4eb.....319f'

… or like this …

curl GET 'http://myhost/api/method/frappe.client.get_list' \
      -H 'Authorization: token 71d8.....a11af2:4eb.....319f'
      -H 'Content-Type: application/json' \
      --data-raw '{   "doctype": "Naming Series"   }'

I get the error…

Traceback (most recent call last):
  File "/home/erpdev/frappe-bench/apps/frappe/frappe/app.py", line 62, in application response = frappe.api.handle()
  File "/home/erpdev/frappe-bench/apps/frappe/frappe/api.py", line 118, in handle "data":  frappe.call(frappe.client.get_list,
  File "/home/erpdev/frappe-bench/apps/frappe/frappe/__init__.py", line 1055, in call return fn(*args, **newargs)
  File "/home/erpdev/frappe-bench/apps/frappe/frappe/client.py", line 34, in get_list return frappe.get_list(doctype, fields=fields, filters=filters, order_by=order_by,
  File "/home/erpdev/frappe-bench/apps/frappe/frappe/__init__.py", line 1291, in get_list return frappe.model.db_query.DatabaseQuery(doctype).execute(None, *args, **kwargs)
  File "/home/erpdev/frappe-bench/apps/frappe/frappe/model/db_query.py", line 96, in execute result = self.build_and_run()
  File "/home/erpdev/frappe-bench/apps/frappe/frappe/model/db_query.py", line 110, in build_and_run args = self.prepare_args()
  File "/home/erpdev/frappe-bench/apps/frappe/frappe/model/db_query.py", line 136, in prepare_args self.set_optional_columns()
  File "/home/erpdev/frappe-bench/apps/frappe/frappe/model/db_query.py", line 298, in set_optional_columns columns = get_table_columns(self.doctype)
  File "/home/erpdev/frappe-bench/apps/frappe/frappe/model/meta.py", line 49, in get_table_columns return frappe.db.get_table_columns(doctype)
  File "/home/erpdev/frappe-bench/apps/frappe/frappe/database/database.py", line 865, in get_table_columns raise self.TableMissingError('DocType', doctype)
pymysql.err.ProgrammingError: ('DocType', 'Naming Series')

Question

Is managing “Naming Series” through the API prohibited?

Naming Series is a Single Type.

Try /api/resource/Naming%20Series/Naming%20Series

I was looking for a way to add a new Naming Series through the REST API or RPC API.

I tried several versions of what you suggest.

curl -s 'http://einvoice/api/resource/Naming Series/Naming Series' \
     -H 'Authorization: token 71d....af2:4eb....19f' \
| jq -r .

which returns:

{
  "data": {
    "name": "Naming Series",
    "owner": "Administrator",
    "creation": "2020-05-13 06:17:14.473835",
    "modified": "2020-05-13 06:17:14.473835",
    "modified_by": "Administrator",
    "idx": "0",
    "docstatus": 0,
    "user_must_always_select": 0,
    "current_value": 0,
    "doctype": "Naming Series"
  }
}

So I tried :

curl -s 'http://einvoice/api/resource/Naming Series/Webhook' \
     -H 'Authorization: token 71d....af2:4eb....19f' \
| jq -r .

which also returned …

{
  "data": {
    "name": "Naming Series",
    "owner": "Administrator",
    "creation": "2020-05-13 06:17:14.473835",
    "modified": "2020-05-13 06:17:14.473835",
    "modified_by": "Administrator",
    "idx": "0",
    "docstatus": 0,
    "user_must_always_select": 0,
    "current_value": 0,
    "doctype": "Naming Series"
  }
}

So I then tried …

curl -s 'http://einvoice/api/resource/Naming Series/Potato Salad' \
     -H 'Authorization: token 71d....af2:4eb....19f' \
| jq -r .

… and …

curl -s 'http://einvoice/api/resource/Naming Series/Extra Strength Excedrin' \
     -H 'Authorization: token 71d....af2:4eb....19f' \
| jq -r .

both of which also returned …

{
  "data": {
    "name": "Naming Series",
    "owner": "Administrator",
    "creation": "2020-05-13 06:17:14.473835",
    "modified": "2020-05-13 06:17:14.473835",
    "modified_by": "Administrator",
    "idx": "0",
    "docstatus": 0,
    "user_must_always_select": 0,
    "current_value": 0,
    "doctype": "Naming Series"
  }
}

This is very interesting, but isn’t getting me what I need.

You’ll have to check js source to know right api calls (frappe.call()). It’s combination of ui and api.

:mask:

Oh boy! Ok.

If we’re down to a “read the code” issue then this has to go on the back burner with a bunch of other things while I deal with critical path stuff … and I’m sorry to say I’ll be asking numerous more questions about those today :face_with_raised_eyebrow:

As I stumble around in the dark, just being shown what’s me being dumb and what’s the edge of a cliff is always appreciated. Thanks for your guidance – yet again.

To get naming series of Webhook

curl -s -X POST "http://einvoice/api/method/runserverobj" \
    -H 'Content-Type: application/json' \
    -H "Authorization: token 71d....af2:4eb....19f" \
    --data-raw '{ "method": "get_options", "docs": "{\"name\":\"Naming Series\",\"doctype\":\"Naming Series\",\"select_doc_for_series\":\"Webhook\",\"modified\":\"2020-05-12 13:44:39.025598\"}"}'

To update naming series of Webhook

curl -s -X POST "http://einvoice/api/method/runserverobj" \
    -H 'Content-Type: application/json' \
    -H "Authorization: token 71d....af2:4eb....19f" \
    --data-raw '{ "method": "update_series", "docs": "{\"name\":\"Naming Series\",\"doctype\":\"Naming Series\",\"select_doc_for_series\":\"Webhook\",\"set_options\":\"\\nHOOK-.####\\nWHK-.####\",\"modified\":\"2020-05-12 13:44:39.025598\"}"}'

Note: modified date field is required internally. Get modified date of Naming Series before making this call.

Holy Crap!

How’d you figure that out?

Amazing. Thanks.

He wrote it. :rofl: I think.

A core coder who also offers sound advice in the forums is a rare and beautiful creature. :blush:

1 Like

No! I didn’t write it. I just deduced the naming_series.js and naming_series.py and tried few things on ReST API.

That’s how I figured out modified field requirement for the call. Trial and error.

Use the source!

2 Likes

I’m slowly getting into the source but there’s still a lot of user facing stuff that takes some learning.

Sorry thought you were a huge contributor to the REST. my bad!

Like I said, I had to put this on the back burner. But …

Here’s a script for anyone who’d like to give it a try:

export YOUR_HOST="http://YOURHOST";
export YOUR_TOKEN="60e..KEY..115:5a4..SECRET..97a";
export YOUR_SERIES="Webhook";
export YOUR_TEMPLATE="WHK-.####";

Get modification date…

export REST_ENDPOINT="${YOUR_HOST}/api/resource";
export RPC_ENDPOINT="${YOUR_HOST}/api/method";
export DATE_MODIFIED=$(curl -s "${REST_ENDPOINT}/Naming Series/Naming Series" -H "Authorization: token ${YOUR_TOKEN}" | jq -r .data.modified);

Get current series formatting templates for Webhook …

cat << NSEOF > /dev/shm/SeriesSpec.json;
{
    "method": "get_options",
    "docs": "{\"name\":\"Naming Series\",\"doctype\":\"Naming Series\",\"select_doc_for_series\":\"Webhook\",\"modified\":\"${DATE_MODIFIED}\"}"
}
NSEOF

cat /dev/shm/SeriesSpec.json;

curl -sX POST "${RPC_ENDPOINT}/runserverobj" \
  -H "Authorization: token ${YOUR_TOKEN}" \
  -H "Content-Type: application/json" \
  -d @/dev/shm/SeriesSpec.json > /dev/shm/OldSeries.json;

export OLD_TEMPLATES=$(cat /dev/shm/OldSeries.json | jq -r .message);
echo -e "
Existing templates:
${OLD_TEMPLATES}";

Update Webhook series formatting templates …

cat << UPEOF > /dev/shm/SeriesUpdate.json;
{
     "method": "update_series",
     "docs": "{\"name\":\"Naming Series\",\"doctype\":\"Naming Series\",\"select_doc_for_series\":\"Webhook\",\"set_options\":\"\\\nHOOK-.####\\\nWHK-.####\",\"modified\":\"${DATE_MODIFIED}\"}"
}
UPEOF

cat /dev/shm/SeriesUpdate.json;

curl -sX POST "${RPC_ENDPOINT}/runserverobj" \
  -H "Authorization: token ${YOUR_TOKEN}" \
  -H "Content-Type: application/json" \
  -d @/dev/shm/SeriesUpdate.json | jq -r .;

Get revised series formatting templates …

curl -sX POST "${RPC_ENDPOINT}/runserverobj" \
  -H "Authorization: token ${YOUR_TOKEN}" \
  -H "Content-Type: application/json" \
  -d @/dev/shm/SeriesSpec.json > /dev/shm/NewSeries.json;

cat /dev/shm/NewSeries.json | jq -r .message;

export REVISED_TEMPLATES=$(cat /dev/shm/NewSeries.json | jq -r .message);
echo -e "
Prior templates: \n${OLD_TEMPLATES}

Updated templates: \n${REVISED_TEMPLATES}";

Update 2020/06/05 ==> To update the current value of an existing naming series see the forum topic How to set a "Naming Series" sequence start number through the RPC API?