I've been having many problems inserting a Purchase Order using the API because it often responds with errors which are vague or don't identify the cause correctly. We have quite a few customizations for the site involved, so to narrow down the issue, I created a new clean Acumatica ERP installation (v17.210.0034) to work with, with no endpoint extensions, and installed the SalesDemo data in both.
Even with clean installation sites like this, I'm still getting vague errors when trying to insert a PO. For example, I followed the steps below, using Postman to call the API:
- Login: POST to url = "http://localhost/Acu172100034/entity/auth/login, with JSON credentials in the body. Response = success.
- GET a PO: url = http://localhost/Acu172100034/entity/Default/17.200.001/PurchaseOrder/RO/PO000696?$expand=Details,ShippingInstructions. Response = JSON data for the PO
- I copied the JSON in the GET response above and pasted it into a new PUT request body. I removed all "id", "rowNumber", "custom" and "files" fields. I removed the "OrderNbr" field in the header and detail rows since it is an auto-number field for a PO.
- I attempt to insert a new PO using the modified JSON with a PUT: url = http://localhost/Acu172100034/entity/Default/17.200.001/PurchaseOrder,
The error response includes: "PX.Data.PXException: Error: 'Branch' cannot be empty.\r\nError: 'Ship To' cannot be empty.\r\nError: 'Location' cannot be found in the system.\r\n ---> PX.Data.PXOuterException: Error: Inserting 'Purchase Order' record raised at least one error. Please review the errors."
There is a BranchID specified on the PO Line, there is a Location specified on the PO header, and there is a ShipTo specified on the ShippingInstructions. The Location does exist. This error is obviously vague and misleading. Only by trial and error did I determine that I needed to change Hold from true to false to get past this error. I assume this is because there's some validation or rule which disallows an insert or update while on hold? After changing Hold to false and attempting an insert with a PUT again, I then get an error: "PX.Data.PXException: Error: An error occurred during processing of the field InventoryID: Object reference not set to an instance of an object.. ---> System.NullReferenceException: Object reference not set to an instance of an object." and the call stack includes POOrderEntry.POLine_ExpenseAcctID_FieldDefaulting and POOrderEntry.POLine_LineType_FieldUpdated event handlers.
There is an InventoryID on the PO Line, and the inventory item exists, so it looks like these event handlers are throwing errors for some reason when LineType is being set, but I'm guessing. Any ideas how to get this simple API insert to work?
Update (2/23/19): I modified the JSON for insert a bit more by changing the detail item Completed to "false", and the PO header Hold to "true", and the Status to "On Hold". The error mentioned above now changes to "Error: 'UOM' cannot be empty.\r\nError: 'Account' cannot be empty.\r\nError: 'Sub.' cannot be empty.". The UOM is not empty, Account is empty, and SubAccount is empty; but Account and SubAccount are not required fields for a PO Line.