I have copied page 30044 “APIV2 - Sales Order Lines” and created my own custom API page (also the sales order page). I can successfully create an order with lines from Postman. I have added additional fields to read the Reserved Qty on the Sales Line.
If I create sales line in BC manually for an item, it automatically creates the reservation based on the Reserve flag for the item being set to ‘Always’. If I read this sales line with my API, it shows the reserved quantity in Postman correctly.
However, if I try to set the same item from Postman, it creates the sales line perfectly but does not create a reservation on the line. Why not?
The code to run the auto reservation logic is located on the sales line page. Which is why it works for manual entry but not for the API. You will need to add a modification to execute this logic when using the API. Perhaps as an event subscription.
Thanks for your inputs. I can see a ton of reservation stuff happening on page 46 “Sales Order Subform”. However, as far as I can see the onvalidate of the Quantity field on the sales line table does the work already? If I am missing the mark, please can you point me to exactly where on the subform the work is being done?
This is the code you need to run after creating the sale line. This is normally called from the OnValidate of the Quantity textbox. The “AutoReserve” is a function on table 37.
page 46 “Sales Order Subform”
{
.
.
.
protected procedure QuantityOnAfterValidate()
begin
OnBeforeQuantityOnAfterValidate(Rec, xRec);
if Type = Type::Item then begin
CurrPage.SaveRecord();
case Reserve of
Reserve::Always:
AutoReserve();
end;
end;
OnAfterQuantityOnAfterValidate(Rec, xRec);
end;
if (SalesLine.Reserve = SalesLine.Reserve::Always) and (SalesLine."Outstanding Qty. (Base)" <> 0) then
SalesLine.AutoReserve();
Funny thing is when I call the post command to create the sales lines from Postman, it states that I need to have the Shipment Date filled in (due to a Testfield() in AutoReserve()):
"error": {
"code": "Internal_ServerError",
"message": "Shipment Date must be filled in when a quantity is reserved in Sales Line Document Type='Order',Document No.='SO-1000275',Line No.='10000'. CorrelationId: cd803a39-eea4-45c0-bcd6-b6b3002b1070."
}
However, we do fill in the shipment date on the sales line:
Are you saving the sales line before calling AutoReserve? Notice the code I sent you does a SaveRecord first. Also an API basically functions as a delayed insert. Meaning all fields a populated (and validated) before the record is inserted. Thus any validation that depends on the record being in the database will fail.
[EventSubscriber(ObjectType::Table, Database::"Sales Line", 'OnAfterInsertEvent', '', false, false)]
local procedure OnAfterInsertSalesLine(var Rec: Record "Sales Line")
begin
if Rec.IsTemporary then
exit;
if (Rec.Type = Rec.Type::Item) and (Rec.Reserve = Rec.Reserve::Always) then
Rec.AutoReserve();
end;
is that code getting run? I can tell you that AutoReserve is the function that needs to be called to create the reservation. Also it should be call as Rec.AutoReserve(FALSE). The FALSE suppresses the reservation page.
You may need to debug to verify if the code is running or why it’s not doign what it should.
So, the AutoReserve function takes no parameters.
If need be I can suppress the reservation page with the ‘Skip Manual Reservation’ flag on the S&R page.
Created custom API page (Sales Order Lines) by copying page 30044. Successfully create orders with lines from Postman. Added fields to read Reserved Qty on Sales Line.
Manually creating sales line in BC auto-reserves based on Reserve flag. Reading sales line with API shows reserved quantity correctly.
Setting same item from Postman creates sales line but doesn’t create reservation on the line.