Purchase Order Status inconsistent when creating via REST API

Hey everyone :wave:

Again, I am hoping for some feedback and help because I find it hard to understand how ERPNext actually works and what might cause this inconsistency.

In this current scenario, I want to work with ERPNext only by using the REST API and I want to create Purchase Orders.

This is how I create my Purchase Orders:

po_endpoint = f'{erpnext_url}/api/resource/Purchase Order'
po_data = {
    "supplier": "Primary Processor ABC",
    "docstatus": 1,
    "items": [
        {
            "item_code": "BB",
            "custom_content": "MAT-ABS-5mm",
            "schedule_date": schedule_date,
            "qty": 1.0,
        },
    ]
}

All good, I get HTTP 200 and a Purchase Order id: po_id :white_check_mark:

But now it gets weird. When I look at the list view, the POs I create have the status To Receive and Bill, as expected, right?

But after I queried them again via the REST API, I noticed that their status was actually still Draft. Moreover, if I switched to the Report View, the status was displayed as Draft.

What could be the reason for this?

I am already using "docstatus";1 when creating the po.

Can you make a GET API call for the document created and check the status returned in the response?

I have a feeling somehow db.commit is not executed when you create a document using POST method even if you send docstatus = 1 , you may have to update the status using UPDATE method in subsequent call if required.

Hi @tgraupne:

Don’t pass status.

After doc creation use this

POST
https://yoursite.com/api/resource/Purchase%20Order/PUR-ORD-2024-00012

{
    "run_method": "submit"
}

Check this video for further reference:

Hope this helps.

1 Like

Thank you for your feedback @Manan_Shah.

Can you make a GET API call for the document created and check the status returned in the response?

After creating the Purchase Order like described about, I checked the status right away and it was Draft.

Thereafter, I did the following:

response = login_session.put(
  f"{po_endpoint}/{po_id}",
  json={"docstatus": 1}
)

Status was still Draft and docstatus stayed 1.

Thereafter, I tried the following:

response = login_session.put(
  f"{po_endpoint}/{po_id}",
  json={"status": 'To Receive and Bill'}
)

This was not possible and I got an HTTP 417.

@avc thank you for your feedback as well. I will watch the video after sending the message. I already tried your suggestion. I executed the following POST request:

response = login_session.post(
  f"{po_endpoint}/{po_id}",
  json={"run_method": "submit"}
)

I got back an HTTP 200. However, a subsequent GET request as well as the Report View in ERPNext still show status Draft. :confused:

Hi:

It’s working well …
Which version are you using?

@avc I am using

frappe "version": "14.64.0"
erpnext "version": "14.61.3"

I was wondering if I could try the whitelisted function:

@frappe.whitelist()
def update_status(status, name):
	po = frappe.get_doc("Purchase Order", name)
	po.update_status(status)
	po.update_delivered_qty_in_sales_order()

However, I don’t even know what would be the correct status.

I would assume that there is some kind of an ENUM somewhere, explicitly defining the states a Purchase Order can be in but I wasn’t able to find it in the erpnext source code.

Manually updating the status or a Purchase Order worked by using the whitelisted method:

update_status_method_endpoint = f'{erpnext_url}/api/method/erpnext.buying.doctype.purchase_order.purchase_order.update_status'

pr_data = {
    "status": "To Receive and Bill",
    "name": po_id
}

response = login_session.post(update_status_method_endpoint, json=pr_data)

assert response is not None
assert response.status_code == 200

However, I still don’t understand the inconsistent status visualization in List View and Report View. Moreover, I was expecting that Purchase Orders would be submitted correctly on HTTP POST by providing docstatus:1 during creation, at least this is what the community has tough me.

I created a bug report on GitHub:

Note that status is different that docstatus
Use submit method as suggested above.

Hey @avc , thanks again for your feedback.

Note that status is different that docstatus
Use submit method as suggested above.

I am aware of this, thank you :slightly_smiling_face:

And, I already tried suggestion regarding:

POST
https://yoursite.com/api/resource/Purchase%20Order/PUR-ORD-2024-00012
{
“run_method”: “submit”
}

Unfortunately, I was not helping in my case.

Moreover, regardless of the correct follow up method to the initial POST request to create the Purchase Order, the main question still is, why are List View and Report View showing different things in their respective Status column?

Hi @tgraupne:

Before submit:
status = “Draft”
docstatus = 0

After submit,
status = “To Receive and Bill”
docstatus = 1

It’s your requirement, right?
Working in list view as well report view …


Tried on v15, maybe v14 issue …
BTW … if you are connecting from Python, explore this:

That “shouldn’t” be possible. :thinking:

Are you able to query the SQL tables themselves, and pull the values for 'status' and 'docstatus'?

What you’re describing almost feels like a SQL rollback. Something like:

  • Send the HTTP Post
  • Web server replies with 200
  • Web server fails to execute a SQL commit.
  • Data reverts to original state before the call.