Try ERPNext Buy Support Partners Foundation

How to set a "Naming Series" sequence start number through the RPC API?

I had asked previously about …

… and @revant_one found me what I thought I needed.

It turns out only to be part of what I needed. :roll_eyes:

Taking purchase orders as an example i used the “desk” to set the starting sequence number for naming series PUR-ORD-.YYYY.- to 987.

I then created a purchase order and found that it correctly assigned it a document number of PUR-ORD-2020-00988

I then ran my API curl command to examine the registered internal storage of that naming series…

curl -sX POST '${RPC_ENDPOINT}/runserverobj' \
    -H "Authorization: token ${YOUR_TOKEN}" \
    -H 'Content-Type: application/json' \
    -d '{
           "method": "get_options",
           "docs": "{\"name\":\"Naming Series\",\"doctype\":\"Naming Series\",\"select_doc_for_series\":\"Purchase Order\",\"modified\":\"2020-05-20 21:26:31.389303\"}"
        }' | jq -r .;

The result is:

{
  "docs": [
    {
      "name": "Naming Series",
      "modified": "2020-05-20 21:26:31.389303",
      "idx": 0,
      "docstatus": 0,
      "select_doc_for_series": "Purchase Order",
      "user_must_always_select": 0,
      "current_value": 0,
      "doctype": "Naming Series"
    }
  ],
  "message": "PUR-ORD-.YYYY.-"
}

It seems logical that current_value should be 987, but it isn’t, and I don’t understand why not?

My actual requirement is to be able to specify some sequence starting numbers through the API, but the command that updates the possible series format templates for a DocType doesn’t appear to allow setting a start number at all:

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 .;

All you seem to be able to do is define the formatting templates with “set_options” …

"set_options": "\nHOOK-.####\nWHK-.####"

Is it possible to set the start number for a series by means of an RPC? If so, how?

Where might I find the code for the two RPC commands get_options and update_series?

Check frappe.call from this js file

It’s doing the api call internally, you’ll find different “method” available

Server side, the code is in following py file

Check class method

1 Like

Excellent! :100:

So the method I need is this one from naming_series.py

        def update_series_start(self):
                if self.prefix:
                        prefix = self.parse_naming_series()
                        self.insert_series(prefix)
                        frappe.db.sql("update `tabSeries` set current = %s where name = %s",
                                (self.current_value, prefix))
                        msgprint(_("Series Updated Successfully"))
                else:
                        msgprint(_("Please select prefix first"))

.

It shows that the request package must include two required fields from the naming_series.json page descriptor: prefix and current_value.

So I can check the naming series current values with SQL:

MariaDB [_a25a7b22fce4a558]> select * from tabSeries;
+---------------------+---------+
| name                | current |
+---------------------+---------+
| ACC-PAY-2020-       |       1 |
| HOOK-               |       1 |
| MAT-UOM-CNV-        |       2 |
| PATCHLOG            |     919 |
| PUR-ORD-2020-       |       4 |
+---------------------+---------+
10 rows in set (0.001 sec)

MariaDB [_a25a7b22fce4a558]> 

Running the curl command to update the current value of the “Payment Entry” naming series formatting template …

cat << SSEOF > /dev/shm/SeriesStartUpdate.json;
{
     "method": "update_series_start",
     "docs": "{\"doctype\":\"Naming Series\",\"prefix\":\"ACC-PAY-.YYYY.-\",\"current_value\":999,\"modified\":\"2020-05-20 21:26:31.389303\"}"
}
SSEOF

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

… returns:

{
  "docs": [
    {
      "name": "Naming Series",
      "modified": "2020-05-20 21:26:31.389303",
      "idx": 0,
      "docstatus": 0,
      "select_doc_for_series": "Payment Entry",
      "user_must_always_select": 0,
      "prefix": "ACC-PAY-.YYYY.-",
      "current_value": 999,
      "doctype": "Naming Series"
    }
  ],
  "_server_messages": "[\"{\\\"message\\\": \\\"Series Updated Successfully\\\"}\"]"
}

Rechecking in MariaDb:

MariaDB [_a25a7b22fce4a558]> select * from tabSeries;
+---------------------+---------+
| name                | current |
+---------------------+---------+
| ACC-PAY-2020-       |     999 |
| HOOK-               |       1 |
| MAT-UOM-CNV-        |       2 |
| PATCHLOG            |     919 |
| PUR-ORD-2020-       |       4 |
+---------------------+---------+
10 rows in set (0.001 sec)

MariaDB [_a25a7b22fce4a558]> 

@revant_one Always grateful for your deeply knowledgeable assistance!

(not sure why, but it is not offering me the “solution” button on your answer. Sorry.)

1 Like